Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Java中初始化计时器?_Java_Timer - Fatal编程技术网

如何在Java中初始化计时器?

如何在Java中初始化计时器?,java,timer,Java,Timer,我正在用Java创建一个需要帧动画的游戏程序,并且需要每隔40毫秒重新绘制帧。然而,我不太确定如何让计时器处理这些重绘。我试过这样的方法: Timer timer = new Timer(null); 但我想我需要在初始化器中设置更新时间,对吗?我该如何做呢?当程序启动时,如何让计时器运行呢?还是为了那件事而逃跑?我知道这不可能是对的。非常感谢您的帮助 Timer timer = new Timer(50, this); timer.start(); “50”是计时器关闭的频率,以毫秒为单位

我正在用Java创建一个需要帧动画的游戏程序,并且需要每隔40毫秒重新绘制帧。然而,我不太确定如何让计时器处理这些重绘。我试过这样的方法:

Timer timer = new Timer(null);
但我想我需要在初始化器中设置更新时间,对吗?我该如何做呢?当程序启动时,如何让计时器运行呢?还是为了那件事而逃跑?我知道这不可能是对的。非常感谢您的帮助

Timer timer = new Timer(50, this);
timer.start();
“50”是计时器关闭的频率,以毫秒为单位。 …这将调用actionListener

public void actionPerformed(ActionEvent e){
    //do stuff here
}
…然后停止计时器

timer.stop();
例如:

class MyPanel extends JPanel implements ActionListener{
    public Timer time1;
        public MyPanel() {
        time1 = new Timer(40,this);
        setBorder(BorderFactory.createLineBorder(Color.black));
     }
    public void actionPerformed (ActionEvent e){
            //do stuff here
            repaint();
    }
        public void paintComponent(Graphics g) {
        super.paintComponent(g);
        //your paint method
    }  
}
其中delay是整数值,以毫秒为单位显示计时器的延迟 当你想过去的时候,停止计时器

  t.stop();

您需要构造带有延迟的计时器和自定义动作侦听器

class TimerAction implements ActionListener{

    boolean isItTimeForTic = true;

   @Override public void actionPerformed(ActionEvent e) {
      //this is the place to define logic which will be invoked after every DELAY ms
   }
}
然后你就这样使用它:

int delay = 1; //you set your delay here 
Timer timer = new Timer(delay, new TimerAction());
timer.start();

如果您想让计时器重新绘制帧,可以修改计时器以引用帧,然后在actionPerformed中调用其方法。

如果您正在创建游戏,则必须实现runnable。因此,我建议您执行类似操作

//1
    private boolean running = true;
    private long fps = 0;
  //  private static final int MAX_CPS = 50;
   // private static final long MS_PER_FRAME = 1000 / MAX_CPS;
    private static final long MS_PER_FRAME = 40;//in your case it is 40//cps is 25

    public void run(){
        while(running){
            long cycleStartTime = System.currentTimeMillis();

            gameUpdate();//for updating your game logic
            repaint();//for stuff like painting

            long timeSinceStart = (cycleStartTime - System.currentTimeMillis());
            if(timeSinceStart < MS_PER_FRAME){
                try{
                    fps = MS_PER_FRAME - timeSinceStart;
                    Thread.sleep(fps);
                }catch(InterruptedException e){
                    System.err.println("InterruptedException in run "+e);
                }catch(Exception e){
                    System.err.println("Exception in run "+e);
                }
            }//closig if
        }//closing while
    }//closing run


//Like in your case you want to paynt after constant time you can do somehting like
//2

  //  private static final int MAX_CPS = 25;
    private static final long MS_PER_FRAME = 40
    public void run(){
        while(running){
            gameUpdate();//for updating your game logic
            repaint();//for stuff like painting
                try{
                    Thread.sleep(MS_PER_FRAME);
                }catch(InterruptedException e){
                    System.err.println("InterruptedException in run "+e);
                }catch(Exception e){
                    System.err.println("Exception in run "+e);
                }
            }//closig if
        }//closing while
    }//closing run
//1
私有布尔运行=真;
专用长fps=0;
//专用静态最终int MAX_CPS=50;
//专用静态最终长MS_/帧=1000/最大CP;
私有静态最终长MS_/_帧=40//在你的情况下是40//cps是25
公开募捐{
(跑步时){
long cycleStartTime=System.currentTimeMillis();
gameUpdate();//用于更新游戏逻辑
repaint();//用于绘画之类的东西
long-TimesRunStart=(cycleStartTime-System.currentTimeMillis());
if(时间连续开始<每帧毫秒){
试一试{
fps=每帧毫秒数-TimesRunStart;
线程睡眠(fps);
}捕捉(中断异常e){
System.err.println(“运行中的中断异常”+e);
}捕获(例外e){
System.err.println(“运行中异常”+e);
}
}//closig if
}//关门时
}//结束运行
//在你的情况下,你想在固定的时间后付款,你可以做如下事情
//2
//专用静态最终int MAX_CPS=25;
专用静态最终长MS_/_帧=40
公开募捐{
(跑步时){
gameUpdate();//用于更新游戏逻辑
repaint();//用于绘画之类的东西
试一试{
线程睡眠(每帧毫秒);
}捕捉(中断异常e){
System.err.println(“运行中的中断异常”+e);
}捕获(例外e){
System.err.println(“运行中异常”+e);
}
}//closig if
}//关门时
}//结束运行
我建议使用1,因为它为游戏提供了平滑度

(假设您谈论的是Swing)。
//1
    private boolean running = true;
    private long fps = 0;
  //  private static final int MAX_CPS = 50;
   // private static final long MS_PER_FRAME = 1000 / MAX_CPS;
    private static final long MS_PER_FRAME = 40;//in your case it is 40//cps is 25

    public void run(){
        while(running){
            long cycleStartTime = System.currentTimeMillis();

            gameUpdate();//for updating your game logic
            repaint();//for stuff like painting

            long timeSinceStart = (cycleStartTime - System.currentTimeMillis());
            if(timeSinceStart < MS_PER_FRAME){
                try{
                    fps = MS_PER_FRAME - timeSinceStart;
                    Thread.sleep(fps);
                }catch(InterruptedException e){
                    System.err.println("InterruptedException in run "+e);
                }catch(Exception e){
                    System.err.println("Exception in run "+e);
                }
            }//closig if
        }//closing while
    }//closing run


//Like in your case you want to paynt after constant time you can do somehting like
//2

  //  private static final int MAX_CPS = 25;
    private static final long MS_PER_FRAME = 40
    public void run(){
        while(running){
            gameUpdate();//for updating your game logic
            repaint();//for stuff like painting
                try{
                    Thread.sleep(MS_PER_FRAME);
                }catch(InterruptedException e){
                    System.err.println("InterruptedException in run "+e);
                }catch(Exception e){
                    System.err.println("Exception in run "+e);
                }
            }//closig if
        }//closing while
    }//closing run