1

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.setColor(Color.BLACK);
    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.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
István Kohn
  • 135
  • 8

6 Answers6

4

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() {
    super.addNotify();
    createBufferStrategy(2);
}

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.setColor(Color.BLACK);
            draw.fillRect(0, 0, getPreferredSize().width, getPreferredSize().height);
            draw.drawImage(ship.getImage(), ship.getCurrentX(), ship.getCurrentY(), null);
            draw.dispose();

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

        // Display the buffer
        strategy.show();

        // Repeat the rendering if the drawing buffer was lost
    } while (strategy.contentsLost());
}
Community
  • 1
  • 1
Charles Goodwin
  • 6,402
  • 3
  • 34
  • 63
2

any drawing will still be performed in the Swing Thread, so no matter what you try work around, it wont help.

Make sure you are not doing any lengthy calculations in the swing thread, this may be stopping repaint from being executed as soon as it needs to be executed

Leon
  • 1,141
  • 13
  • 25
2

Separate all the logic into 2 parts. Static and Dynamic. (e.g. sea and moving ship. Ship changes shape/location on a static image of sea)

Draw static content in an image once and use the image in your paintComponent(). Call dynamic parts painting after the static image.

Use setClip() to restrict repainting areas.

StanislavL
  • 56,971
  • 9
  • 68
  • 98
2

Just for code that you post here:

1/ if you want to display Image/ImageIcon, then the best and easiest way is to Use Labels

2/ as you mentioned Runnable{...}.start(); Swing is simple threaded and all output to GUI must be done on EDT; you have to look at Concurrency in Swing, result is that all output from BackGround Task(s) must be wrapped into invokeLater(), and if is there problem with perfomancie then into invokeAndWait()

3/ if you be switch (between JComponents)/add/delete/change Layout then you have to call revalidate() + repaint() as last lines in concrete code block

EDIT:

dirty hack would be paintImmediately()

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
mKorbel
  • 109,525
  • 20
  • 134
  • 319
2

Calling repaint without any arguments means that the whole panel is repainted.

If you need to repaint parts of the screen (the spaceship has moved to a different location) you should make shure that only those parts of the screen are repainted. The areas that stay the same should not be touched.

Repaint takes coordinates of a rectangle that should be repainted. When moving the ship you should know the old coordinates of the ship and the coordinates the ship should move to.

repaint( oldShipCoordinateX, oldShipCoordinateY, shipWidth, shipLength );
repaint( newShipCoordinateX, newShipCoordinateY, shipWidth, shipLength );

This is usually much faster than calling repaint() without arguments. However you have extra effort to remember the last position of the ship and must be able to calculate the new position of the ship.

See also: http://download.oracle.com/javase/tutorial/uiswing/painting/index.html - especially step 3

david
  • 852
  • 1
  • 7
  • 22
  • This seems like a good solution to save resources, however, it begs the question: is it still worth calling repaint() with parameters if I have multiple moving objects on the screen? (for example spaceship, projectiles and incoming meteors) – István Kohn Aug 01 '11 at 14:05
  • Good point. With that in mind I also think that Charles Goodwin's answer is much more appropriate for your needs. +1 for him. – david Aug 01 '11 at 18:23
1

I have read that the repaint() method is only a request, and it doesn't execute every time it's called

It consolidates multiple repaint() requests into one to be more efficient.

I believe I'm noticing the effects of this, as my spaceship tends to lag ever so slightly when I'm moving it.

Then post your SSCCE that demonstrates this problem. I suspect the problem is your code.

Regarding the solution you accepted, take a look at Charles last posting: Swing/JFrame vs AWT/Frame for rendering outside the EDT comparing Swing vs AWT solutions.

Community
  • 1
  • 1
camickr
  • 321,443
  • 19
  • 166
  • 288