java吧 关注:1,308,447贴子:12,885,353
  • 2回复贴,共1

遇到一个问题,求大神指教。

取消只看楼主收藏回复

class PainThread extends Thread //重画线程
{
public void run() {
while(true){
repaint();
try {
Thread.sleep(5000); //这里面的参数设多少都没反应,一运行CPU就满载
break;
} catch(InterruptedException ie) {}
}
}
}


1楼2018-03-31 13:34回复
    public void paint(Graphics g) { //加载窗口
    super.paint(g);
    new PainThread().start(); //启动重画线程(就在这里调了一次这方法)
    g.drawImage(img,(int)x,(int) y, null);
    if(left){
    x-=1;
    }
    else {
    x+=1;
    }
    if (x>500-30) {
    left=true;
    }
    if(x<0){
    left=false;
    }
    }


    2楼2018-03-31 13:37
    回复
      2026-09-02 15:51:29
      广告
      不感兴趣
      开通SVIP免广告
      public class Gamefarme2 extends Frame {
      Image img = gameUtil.getImage("imges/game.png"); //加载图片
      public void launchFrame ()
      {
      setSize(600,600);
      setLocation(100,100);
      setVisible(true);
      addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
      System.exit(0);
      }
      });
      }
      private double x,y=100;
      private boolean left;
      @Override
      public void paint(Graphics g) { //加载窗口
      super.paint(g);
      new PainThread().start();//启动重画线程
      g.drawImage(img,(int)x,(int) y, null);
      if(left){
      x-=1;
      }
      else {
      x+=1;
      }
      if (x>500-30) {
      left=true;
      }
      if(x<0){
      left=false;
      }
      }
      class PainThread extends Thread//重画线程
      {
      public void run() {
      while(true){
      repaint();
      try {
      Thread.sleep(5000);
      break;
      } catch(InterruptedException ie) {}
      }
      }
      }
      public static void main(String[] args) { // 主函数
      Gamefarme2 gf= new Gamefarme2();
      gf.launchFrame();
      }
      }


      5楼2018-03-31 21:52
      回复