简体   繁体   English

如何制作计时器?

[英]How can I make a timer?

I have this code: 我有以下代码:

if (main.isGoingLeft1)
{
    g.drawImage(playerLeft1,x,y,null);
    main.isGoingLeft2 = true;
}

I tried to put a Thread.sleep in there but it stops the whole game, so that's not a good idea. 我试图在其中放置一个Thread.sleep ,但是它会停止整个游戏,所以这不是一个好主意。

I want to put a timer between drawImage and setting the boolean as true. 我想在drawImage和将boolean设置为true之间放置一个计时器。 So if you could tell me or show me how to make a timer, that would be awesome. 因此,如果您可以告诉我或告诉我如何制作计时器,那将非常棒。

You'll need to use the javax.swing.Timer for doing animations. 您需要使用javax.swing.Timer进行动画处理。

Timer t=new Timer(2,new ActionListener(){
     public void actionPerformed(ActionEvent ae)
     {
      // your animation code
     }
});
t.start();

The above timer executes every 2 milliseconds ie it fires ActionEvent every 2ms. 上面的计时器每2毫秒执行一次,即每2毫秒触发一次ActionEvent If you want to stop it you can use t.stop() method. 如果要停止它,可以使用t.stop()方法。

See javax.swing.Timer and also How to use swing timers 请参见javax.swing.Timer以及如何使用摆动计时器

Hummm, based just in that piece of your code, it seems that it won't work. Hummm,仅基于您的那部分代码,似乎无法正常工作。 To use a timer like swing timer or java util timer will require that your have threads in your program. 要使用诸如swing定时器java util定时器之类的定时器,将需要您的程序中有线程。

With threads, there'll be code running in parallel, and you can prepare some of your classes to receive a signal from the timer and then take some action. 使用线程,将有并行运行的代码,您可以准备一些类以接收来自计时器的信号,然后采取一些措施。

In the piece of code that you showed, seems that you want something to happen after some other thing, but after some delay. 在显示的这段代码中,您似乎希望某些事情发生在某些其他事情之后,但要经过一些延迟。 And you want that you program continue processing something else, but nothing seems to indicate that your code is prepared to run in parallel. 并且您希望您的程序继续处理其他内容,但是似乎没有任何迹象表明您的代码已准备好并行运行。

To do that, you'll have a thread taking care of the goingLeft1 and another to the goingLeft2, and they'll comunicate through messages, not just by setting an attribute directly. 为此,您将有一个线程负责goingLeft1,另一个负责goingLeft2,它们将通过消息进行通信,而不仅仅是直接设置属性。

yea, correctly said by java technical, you could use swing timer in swing based applications rather than using java.util.Timer.. benifit of using the swiing timer here is that you dont need to create a seperate thread to execute timer. 是的,由Java技术人员正确地说,您可以在基于Swing的应用程序中使用Swing计时器,而不是使用java.util.Timer ..在这里使用Swiing计时器的好处是您不需要创建单独的线程来执行计时器。 your code freezes for X milliseconds because it is running on the same thread on which the whole code is running. 您的代码冻结X毫秒,因为它在整个代码运行所在的同一线程上运行。 if you want to display time elapsed in seconds, you may use something like this 如果您想显示经过的时间(以秒为单位),则可以使用如下所示的内容

Timer timer = new Timer (1000,new ActionListener() 计时器计时器=新计时器(1000,新ActionListener()

{ {

public void actionPerformed(ActionEvent ae)

{

    endTime=System.currentTimeMillis();
    tempTime=(int)((endTime-startTime)/1000);
    if(tempTime>30)
    {
        timer.stop();
        displayResult();
    }
    someLabel.setText("Time left : "+(30-tempTime)+" seconds");
}

} }

this code displays time remaining from 30 seconds (30,29,28 ...1) and then it stops. 此代码显示30秒后的剩余时间(30,29,28 ... 1),然后停止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM