repaint() error. My repaint method just blinks the screen
I think I have figured out where my problem is coming from. when I comment out repaint() I get what I would normally get. I screen with a racetrack on it that I created using rectangles along with 2 cars. Obviously nothing looks like it moves because I commented out repaint(). If I run it as is my program does one of two things. It either doesn't work or it blinks about 3 to 5 times and then stops. if I comment out the sleep() the program just keeps blinking till I exit. The cars move like they are supposed to. I don't know what I'm doing wrong. I can also include the code for the actually track if you would like. It is just way to long and I think I narrowed it down to this.
private class MoveTwo extends Thread{
public void run(){
//infinite loop
while(true){
try{
repaint();//Refresh Screen
if(playerTwoSpeed<=5) playerTwoSpeed+=.2;//slow acceleration
playerTwo.y-=playerTwoSpeed;
Thread.sleep(100);//Delay
} catch(Exception e){
break;//Stop if there is an error
}
}
}
}
Here you go:
public void paint(Graphics g){
super.paint(g);
g.setColor(Color.DARK_GRAY);
g.fillRect(0, 0, WIDTH, HEIGHT);
//Turn Border green when we draw.
g.setColor(Color.GREEN);
//fill rectangles.
g.fillRect(left.x, left.y, left.width, left.height);
g.fillRect(right.x, right.y, right.width, right.height);
g.fillRect(top.x, top.y, top.width, top.height);
g.fillRect(bottom.x, bottom.y, bottom.width, bottom.height);
g.fillRect(center.x, center.y, center.width, center.height);
g.fillRect(obstacle.x, obstacle.y, obstacle.width, obstacle.height);
g.fillRect(obstacle2.x, obstacle2.y, obstacle2.width, obstacle2.height);
g.fillRect(obstacle3.x, obstacle3.y, obstacle3.width, obstacle3.height);
g.fillRect(obsta开发者_JAVA百科cle4.x, obstacle4.y, obstacle4.width, obstacle4.height);
g.fillRect(obstacle5.x, obstacle5.y, obstacle5.width, obstacle5.height);
g.setColor(Color.WHITE);//Change color to white.
g.fillRect(outerStart.x, outerStart.y, outerStart.width, outerStart.height);
g.fillRect(innerStart.x, innerStart.y, innerStart.width, innerStart.height);
g.setColor(Color.YELLOW);
g.fillRect(finish.x, finish.y, finish.width, finish.height);
//Player one is blue
g.setColor(Color.BLUE);
g.fill3DRect(playerOne.x, playerOne.y, playerOne.width, playerOne.height, true);
g.setColor(Color.RED);
g.fill3DRect(playerTwo.x, playerTwo.y, playerTwo.width, playerTwo.height, true);
}
You probably use an override of a paint method from a JFrame subclass. This leads to blinks on many OS as JFrame are heavy components. The solution is to use an override of a JComponent (typically a JPanel)'s paintComponent method. The code would be the same but would integrate much more smoothly in Swing framework.
Also, you should consider using a better control over your thread, a while truc loop is...difficult to stop. :)
You should use a boolean flag and a method to set it to true or false.
Regards,
Stéphane
You should use double-buffering in order to get smooth animations.
JComponent.setDoubleBuffered(boolean flag);
精彩评论