gpt4 book ai didi

java - 更改 Java 计时器中的延迟

转载 作者:行者123 更新时间:2023-12-01 16:01:43 24 4
gpt4 key购买 nike

我正在尝试编辑我的计时器,以便每调用 25 次 repaint() ,计时器触发速度就会减半。所以前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 );
}
}

}

最佳答案

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

关于java - 更改 Java 计时器中的延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3698197/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com