Drawing images over a JPanel

I’m using active rendering over a JPanel implementing Runnable and I have some doubts I want share with you. Which of the following codes is better (performace, etc…):

1st one:


    BufferedImage im=loadImage(file);
    Graphics2D g = (Graphics2D) getGraphics();  

    while (threadSuspended) {
      g.drawImage(im, 0, 0, this);  
    }
   g.dispose();

2nd one:


    BufferedImage im=loadImage(file);

    while (threadSuspended) {
        Graphics2D g = (Graphics2D) getGraphics();  
      g.drawImage(im, 0, 0, this);  
        g.dispose();
    }

Do I have to flush the BufferedImage too ? when ?

Thanks in advance

Ripped out of context I think it’s hard to tell. Though the graphics creation/dispose is negligable compared to drawing the image anyway (afaik).

No, you don’t have to dispose of the BufferedImage, unless you have a specific reason to do so.

This smells a bit like “premature optimization” to me… Build you app then if you have performance problems use a profiler to investigate. It’s the only way to go. :slight_smile:

Cheers,
Mikael

Use #2. There is basically no speed difference at all (I got 750 vs 750 fps… duh), but you can run into memory troubles on mac os easily if you dont getGraphics/dispose em properly per frame (no clue why - but it’s the proper way to do so).

Do I have to flush the BufferedImage too ? when ?

No. You only need to do that with MemoryImageSource stuff.

Another question, I’m a beginner in Java2D and active rendering so I don’t know much them. I use the loop I shown you before to render an image on screen during some seconds. Why did I call drawImage inside a loop and didn’t I call drawImage once and then call to sleep ? Because when I switch apps with alt+tab and return I loose the image on screen. Is there a better way to do this, I mean, only render the image when I loose the contents on screen ? This loop is eating all my CPU.

Again , it’s hard without exact context but you should probably override paintComponent(Graphics g) in your panel instead. It gets called to repaint your jpanel.

It looks to me like you’re doing animation? If you are not you are on the wrong track my friend.

Cheers,
Mikael

Yes, I’m doing animation. I’m developing a slideshow app using a panel to show the photos and do transitions (fade, scrolling, etc…) between them. I don’t know if the paintComponent method you are suggesting is valid in this case.

It is seldom not valid. :wink:

If you override paintComponent and do your painting there you can just call repaint() on the panel from a repeating thread (that must be an EventThread, see javax.swing.Timer or SwingUtilities.invokeLater()). That repaint may not hapen immediately though, and two (or more) calls may be coalesced one. Calling paintImmediately() on the panel WILL paint it right away, wich is probably what you want, though it is considered a bit more “rude”.

Doing it this way will make the panel repaint itself OK in all circumstances. You just have to make som algorithm so the panel “knows” what to paint, which is not hard.

Cheers,
Mikael