2012-06-15 102 views
2

我想创建一个秒表,以便为我的游戏打分。可以说我有一个变量:int sec = 0.当游戏开始时,我想要一个g.drawString将时间绘制到小程序。因此,例如每秒,秒将增加1.在Java Applet中使用计时器计为秒表

我该如何去做g.drawString(Integer.toString(sec),40,400)增加1并绘制每秒?

谢谢。

编辑:

我已经想通了如何增加它,并通过使用的ActionListener并把g.drawString在那里它输出到屏幕上,但它打印ontop的对方。如果我将g.drawString放入paint方法中,并且只在ActionListener中将sec递增1,则会出现闪烁。我应该使用双缓冲?如果是的话,我该怎么做呢?

+1

相反图纸,这种情况下,很容易与'JLabel' –

+0

的帮助下做到这一点,请看看这个相关[示例](http://stackoverflow.com/questions/9907583/set-dynamic-jlabel-text-in-a-jdialog-by-timer/9908342#9908342) –

+0

@ user1457836:接受答案 –

回答

5
import java.awt.event.*; 
import javax.swing.*; 

public class StopWatch extends JLabel 
      implements MouseListener, ActionListener { 

    private long startTime; // Start time of stopwatch. 
          // (Time is measured in milliseconds.) 

    private boolean running; // True when the stopwatch is running. 

    private Timer timer; // A timer that will generate events 
         // while the stopwatch is running 

    public StopWatch() { 
     // Constructor. 
     super(" Click to start timer. ", JLabel.CENTER); 
     addMouseListener(this); 
    } 

    public void actionPerformed(ActionEvent evt) { 
      // This will be called when an event from the 
      // timer is received. It just sets the stopwatch 
      // to show the amount of time that it has been running. 
      // Time is rounded down to the nearest second. 
     long time = (System.currentTimeMillis() - startTime)/1000; 
     setText("Running: " + time + " seconds"); 
    } 

    public void mousePressed(MouseEvent evt) { 
      // React when user presses the mouse by 
      // starting or stopping the stopwatch. Also start 
      // or stop the timer. 
     if (running == false) { 
      // Record the time and start the stopwatch. 
     running = true; 
     startTime = evt.getWhen(); // Time when mouse was clicked. 
     setText("Running: 0 seconds"); 
     if (timer == null) { 
      timer = new Timer(100,this); 
      timer.start(); 
     } 
     else 
      timer.restart(); 
     } 
     else { 
      // Stop the stopwatch. Compute the elapsed time since the 
      // stopwatch was started and display it. 
     timer.stop(); 
     running = false; 
     long endTime = evt.getWhen(); 
     double seconds = (endTime - startTime)/1000.0; 
     setText("Time: " + seconds + " sec."); 
     } 
    } 

    public void mouseReleased(MouseEvent evt) { } 
    public void mouseClicked(MouseEvent evt) { } 
    public void mouseEntered(MouseEvent evt) { } 
    public void mouseExited(MouseEvent evt) { } 

} // end StopWatchRunner 

一个小的小程序测试组件:

/* 
    A trivial applet that tests the StopWatchRunner component. 
    The applet just creates and shows a StopWatchRunner. 
*/ 


import java.awt.*; 
import javax.swing.*; 

public class Test1 extends JApplet { 

    public void init() { 

     StopWatch watch = new StopWatch(); 
     watch.setFont(new Font("SansSerif", Font.BOLD, 24)); 
     watch.setBackground(Color.white); 
     watch.setForeground(new Color(180,0,0)); 
     watch.setOpaque(true); 
     getContentPane().add(watch, BorderLayout.CENTER); 

    } 

} 
+0

您不真的必须定义任何'布尔变量'来知道'Timer'的状态,因为你可以通过使用它的方法'timerObject.isRunning()'来获得它。虽然+1其余:-) –

+0

非常感谢,这将帮助我出去很多:) –