更改Java计时器的延迟

时间:2010-09-13 06:24:24

标签: java swing static timer

我正在尝试编辑我的计时器,以便每25次重绘()被称为计时器点火速度减半。所以前25次是500次;然后接下来25次250;等等。

两个'易于体验'的问题:

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 );  
        }
   }

}

2 个答案:

答案 0 :(得分:1)

  1. 在您的示例中,paintCountspeed必须是静态的,因为您在没有实例的情况下使用它们,来自方法main(),它本身是静态的。为避免将其设为静态,您可以将其引用为b.paintCountb.speed

  2. 修改计时器的代码需要移至paint()方法。这意味着你的Timer实例需要成为一个实例变量,你应该在构造函数中创建并启动计时器。顺便说一句,这些更改还要求paintCountspeed也可以“非静态”。

  3. 你应该得到这样的东西:

    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 );    
            }
        }
    }
    

答案 1 :(得分:0)

  1. 因为您直接在main方法中使用它们,这是静态的。
  2. 我没有看到这样做的方法,但我在main方法中看到了一段代码。它可能与paintCount % 25 == 0永远不存在有关。调试它,或者放一些println语句来查看paintCount在前50-100次调用中的值。这可能会给你答案。