[英]Changing Delay in Java Timer
I'm trying to edit my timer so that every 25 times repaint() is called the timer firing speed cuts in half. 我正在尝试编辑计时器,以便每25次repaint()被称为计时器触发速度减半。 So the first 25 times it's 500;
所以前25次是500; then the next 25 times its 250;
然后是其250的下25倍; and so on.
等等。
Two 'EASY FOR THE EXPERIENCED' questions: 两个“易于体验”的问题:
1) Why is Eclipse making me make the variables static (or otherwise not compiling)? 1)为什么Eclipse使我使变量成为静态变量(否则不进行编译)?
2) The program doesn't seem to reach the function where I divide the speed in half and set the delay to that new speed. 2)该程序似乎没有达到我将速度减半并将延迟设置为该新速度的功能。 Why is that?
这是为什么? How do I fix it?
我如何解决它?
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 );
}
}
} }
In your example, paintCount
and speed
have to be static because you are using them without an instance, from within a method, main()
, which is itself static. 在您的示例中,
paintCount
和speed
必须是静态的,因为从方法main()
本身是静态的,因为您没有实例使用它们。 To avoid having to make them static, you could have referenced them as b.paintCount
and b.speed
. 为了避免使它们成为静态,可以将它们引用为
b.paintCount
和b.speed
。
The code that modifies your timer needs to move into your paint()
method. 修改计时器的代码需要移到
paint()
方法中。 This means your Timer instance will need to become an instance variable, and you should probably create and start the timer within the constructor. 这意味着您的Timer实例将需要成为实例变量,并且您可能应该在构造函数中创建并启动计时器。 Incidentally, these changes also require that
paintCount
and speed
also be made "non-static". 顺便提及,这些更改还要求
paintCount
和speed
也必须设置为“非静态”。
You should end up with something like this: 您应该以如下形式结束:
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 );
}
}
}
main
method, which is static. main
方法(静态方法)中使用它们。 paintCount % 25 == 0
never being true. paintCount % 25 == 0
永远都不成立有关。 Debug it, or put some println statements to see what the value of paintCount is over the first 50-100 calls.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.