error de repintado (). Mi método de repintado hace parpadear la pantalla

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

Aqui tienes:

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(obstacle4.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);

}

preguntado el 27 de agosto de 11 a las 17:08

Please send you paint method as well. -

And make sure that your paint() implementation calls super.paint()? -

Probably not the source of your problem here, but: Is playerTowSpeed y/o playerTwo.y accessed by other threads? If so you deber synchronize your updates to them (or Java will not guarantee that the other threads will vez be able to see your updates). -

Your problem description is unclear: "doesn't work"? blank screen? frozen? "blink"? with the graphics as they should be? -

so my answer is good I think. Use an override of JComponent, not JFrame. -

2 Respuestas

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.

Saludos,
Stéphane

Respondido 29 ago 11, 04:08

You should use double-buffering in order to get smooth animations.

JComponent.setDoubleBuffered(boolean flag);

Respondido 27 ago 11, 21:08

In Swing it's set to true by default anyway. - toto2

JFrame.isDoubleBuffered() returns false. - Ammar

@toto2: True for JPanel, pero no para JComponent. - basurero

Anyway, as pointed out by Snicolas, you never paint in a JFrame. - toto2

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.