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. So the first 25 times it's 500; then the next 25 times its 250; and so on.
Two 'EASY FOR THE EXPERIENCED' questions:
1) Why 开发者_开发百科is Eclipse making me make the variables static (or otherwise not compiling)?
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. 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
andspeed
have to be static because you are using them without an instance, from within a method,main()
, which is itself static. To avoid having to make them static, you could have referenced them asb.paintCount
andb.speed
.The code that modifies your timer needs to move into your
paint()
method. This means your Timer instance will need to become an instance variable, and you should probably create and start the timer within the constructor. Incidentally, these changes also require thatpaintCount
andspeed
also be made "non-static".
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 );
}
}
}
- Because you're using them directly in the
main
method, which is static. - I don't see a method that does this, but I see a block of code in your main method. It probably has to do with the
paintCount % 25 == 0
never being true. Debug it, or put some println statements to see what the value of paintCount is over the first 50-100 calls. This will likely give you your answer.
精彩评论