Working my way arround repaint() in Java

I’m planning to write a simple spaceshooter. I have read that the repaint() method is only a request, and it doesn’t execute every time it’s called. I believe I’m noticing the effects of this, as my spaceship tends to lag ever so slightly when I’m moving it. Currently I’m simply drawing my ship in a a JPanel’s paintComponent() method, and keep calling repaint() on regular intervals (my panel’s also Runnable). Seeing as repaint() may potentially screw me over, I’m trying to find a way to work arround it, however I’ve ran out of ideas. The code I have so far:

private void renderGraphics() {
    if (MyImage == null) {
        MyImage = new BufferedImage(getPreferredSize().width,
                getPreferredSize().height, BufferedImage.TYPE_INT_RGB);
    MyGraphics = MyImage.getGraphics();
    MyGraphics.fillRect(0, 0, getPreferredSize().width, getPreferredSize().height);
    MyGraphics.drawImage(ship.getImage(), ship.getCurrentX(), ship.getCurrentY(), null);       

The idea was to create my own graphics, and then make the JPanel draw it, and keep calling this instead of repaint() in my run() method, however I have no idea how to do that. I’d appriciate any input on the matter.


There are multiple ways to approach this.

The best is probably to use BufferStrategy and draw to that, of which I have included a code snippet that should work for you.

You can take this one step further and abandon Swing altogether, just using Frame/BufferStrategy. There is a fully working example (from which the code snippet was taken and adapted) in my question here:

AWT custom rendering – capture smooth resizes and eliminate resize flicker

Anyway, here is an implementation BufferStrategy that you should be able to just drop in:

// you should be extending JFrame
public void addNotify() {

private synchronized void render() {
    BufferStrategy strategy = getBufferStrategy();
    if (strategy==null) return;
    sizeChanged = false;
    // Render single frame
    do {
        // The following loop ensures that the contents of the drawing buffer
        // are consistent in case the underlying surface was recreated
        do {
            MyGraphics draw = strategy.getDrawGraphics();
            draw.fillRect(0, 0, getPreferredSize().width, getPreferredSize().height);
            draw.drawImage(ship.getImage(), ship.getCurrentX(), ship.getCurrentY(), null);

            // Repeat the rendering if the drawing buffer contents 
            // were restored
        } while (strategy.contentsRestored());

        // Display the buffer;

        // Repeat the rendering if the drawing buffer was lost
    } while (strategy.contentsLost());

Leave a Reply

Your email address will not be published. Required fields are marked *