2010-09-13 226 views
1

我試圖編輯我的定時器,以便每25次重繪()稱爲定時器啓動速度減半。所以前25次是500。那麼下一個25倍的250;等等。在Java定時器中更改延遲

兩個'EASY對於有經驗的'問題:

1)爲什麼Eclipse的讓我做靜態變量(或不編譯)?

2)該程序似乎沒有達到的功能,我把速度減半,並設置延遲到新的速度。這是爲什麼?我如何解決它?

public class MovingCircle extends JFrame implements ActionListener { 

    Ellipse2D.Double myEllipse; 
    Rectangle2D.Double backgroundRectangle; 
    private static int paintCount = 0; 
    private static int speed = 500; 

    public MovingCircle() { 

     //Make the ellipse at the starting position 
     myEllipse = new Ellipse2D.Double(30, 30, 20, 20); 

     //Make the background rectangle to "erase" the screen 
     backgroundRectangle = new Rectangle2D.Double(0, 0, 400, 300); 
    } 

    public static void main(String[] args) { 

     MovingCircle b = new MovingCircle(); 
     b.setSize(400, 300); 
     b.setVisible(true); 
     b.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     Timer t = new Timer(500, b); 
     t.start(); 

     if(paintCount % 25 == 0) { 

      t.setDelay((int)(speed/2)); 
      speed = (int)(speed/2); 
      System.out.println(speed); 
     } 
    } 

    public void actionPerformed(ActionEvent ae) { 

     //This will be called by the Timer 
     myEllipse.setFrame(myEllipse.getX()+1, myEllipse.getY()+1, myEllipse.getWidth(), myEllipse.getHeight()); 
     //Move 1 x-pixel and 1 y-pixel every 50 milliseconds^
     repaint(); 
    } 

    public void paint(Graphics g) { 

     paintCount++;  // Incremenets by one for every repaint(). 
     System.out.println(paintCount); 
     int isPaintTen = (int)(paintCount/10); // Divid current count by 10. 
     Graphics2D g2 = (Graphics2D)g; 

     if((isPaintTen % 2) == 0){  // Take modulus to set if #/10 is odd or even. 

      g2.setColor(Color.YELLOW); 
      g2.fill(backgroundRectangle); 
      g2.setColor(Color.RED); 
      g2.draw(myEllipse); 
     } 

     else if((isPaintTen % 2) == 1) { 

      g2.setColor(Color.RED); 
      g2.fill(backgroundRectangle); 
      g2.setColor(Color.YELLOW); 
      g2.draw(myEllipse); 
     } 
    } 

}

+1

這將是更容易爲「有經驗」,以幫助你,如果你願意花時間來修復你的代碼示例的縮進所以我們可以不用花括號來閱讀它。 – Lee 2010-09-13 07:07:47

回答

1
  1. 在你的榜樣,paintCountspeed有,因爲你使用的是他們沒有一個實例,從方法中,main(),它本身就是靜態是靜態的。爲了避免使它們變爲靜態,您可以將它們引用爲b.paintCountb.speed

  2. 修改計時器的代碼需要進入paint()方法。這意味着你的Timer實例需要成爲一個實例變量,你應該在構造函數中創建並啓動計時器。順便提一下,這些更改還要求paintCountspeed也被設置爲「非靜態」。

您應該結束了,像這樣:

public class MovingCircle extends JFrame implements ActionListener{ 
    Ellipse2D.Double myEllipse; 
    Rectangle2D.Double backgroundRectangle; 
    private int paintCount = 0; 
    private int speed = 500; 
    private Timer tmr; 

    public MovingCircle() { 
     //Make the ellipse at the starting position 
     myEllipse = new Ellipse2D.Double(30, 30, 20, 20); 

     //Make the background rectangle to "erase" the screen 
     backgroundRectangle = new Rectangle2D.Double(0, 0, 400, 300); 

     this.tmr = new Timer(500, this); 
     tmr.start(); 
    } 

    public static void main(String[] args) { 
     MovingCircle b = new MovingCircle(); 
     b.setSize(400, 300); 
     b.setVisible(true); 
     b.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    } 

    public void actionPerformed(ActionEvent ae) { 
     //This will be called by the Timer 
     myEllipse.setFrame(myEllipse.getX()+1, myEllipse.getY()+1, myEllipse.getWidth(), myEllipse.getHeight()); //Move 1 x-pixel and 1 y-pixel every 50 milliseconds 
     repaint(); 
    } 

    public void paint(Graphics g) { 
     paintCount++;  // Incremenets by one for every repaint(). 
     System.out.println(paintCount); 

     if(paintCount % 25 == 0){ 
      tmr.setDelay((int)(speed/2)); 
      speed = (int)(speed/2); 
      System.out.println(speed); 
     } 

     int isPaintTen = (int)(paintCount/10); // Divid current count by 10. 
     Graphics2D g2 = (Graphics2D)g; 
     if((isPaintTen % 2) == 0){  // Take modulus to set if #/10 is odd or even. 
      g2.setColor(Color.YELLOW); 
      g2.fill(backgroundRectangle); 
      g2.setColor(Color.RED); 
      g2.draw(myEllipse); 

     } else if((isPaintTen % 2) == 1) { 
      g2.setColor(Color.RED); 
      g2.fill(backgroundRectangle); 
      g2.setColor(Color.YELLOW); 
      g2.draw(myEllipse);  
     } 
    } 
} 
+0

非常感謝! – user445755 2010-09-13 07:12:43

0
  1. 因爲你在main方法,它是靜態的,直接使用它們。
  2. 我沒有看到這樣做的方法,但我在主方法中看到了一段代碼。這可能與paintCount % 25 == 0從來都不相符。調試它,或者使用一些println語句來查看paintCount的值是否超過前50-100個調用。這可能會給你你的答案。