2012-06-15 1 views
2

내 게임에서 점수를 매기기 위해 스톱워치를 만들고 싶습니다. 변수가 있다고하자. int sec = 0. 게임이 시작될 때 g.drawString이 애플릿에 시간을 그리기를 원한다. 예를 들어 매 초마다 sec는 1 씩 증가합니다.타이머를 사용하여 Java Applet의 스톱워치로 계산

g.drawString (Integer.toString (sec), 40, 400)을 1 씩 증가시키고 매초마다 그려 넣으려면 어떻게해야합니까?

감사합니다.

편집 :

내가 증가하고 ActionListener를 사용하고 거기에 g.drawString으로 바꾸어 화면에 표시하는 방법을 알아 냈어요하지만 서로의 ontop을 인쇄합니다. g.drawString을 paint 메서드에 넣고 ActionListener에 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'의 상태를 알기 위해'boolean variable '을 정의해야합니다. timerObject.isRunning() 메소드를 사용하면 간단히 얻을 수 있습니다. 비록 +1 나머지 :-) –

+0

대단히 고마워,이게 나를 많이 도와 줄 :) –