Archived

This topic is now archived and is closed to further replies.

[java] AWT Double Buffering

This topic is 5073 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

This is what I try to use for double buffering in AWT:
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;

public class GameFrame extends Frame {
private Image offscreen;

public GameFrame() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}

public void paint(Graphics g) {
if(offscreen == null) {
offscreen = createImage(getSize().width, getSize().height);
}
Graphics og = offscreen.getGraphics();
og.setClip(0, 0, getSize().width, getSize().height);
og.clearRect(0, 0, getSize().width, getSize().height);
super.paint(og);
og.dispose();
g.drawImage(offscreen, 0, 0, this);
}

public void invalidate() {
super.invalidate();
offscreen = null;
}

public void update(Graphics g) {
paint(g);
}
}

I then create a frame of this kind in another class and continually call repaint() on it. The problem is that it won't heed repaint(). I have to minimize and maximize to see the changes. What's the problem here? [edited by - robinei on March 22, 2004 8:46:41 PM]

Share on other sites
Why don''t you just use the java.awt.image.BufferStrategy class? It handles both double, triple and n buffering....

Share on other sites
I seem to remember something, way back in the mists of time, about the AWT disabling calls to the old-style interface functions (like repaint(), mouseMove(), keyPressed() etc) if you use any of the new event listener architecture. That was specifically with applets (and I spent a good couple of hours trying to figure out why I wasn''t getting keypresses), but may apply here too.

Share on other sites
I got the BufferStrategy thing to work on the Frame, but when I added a fullsize Panel to the Frame and used the strategy to draw that, everything was apparently drawn beneath the panel...
Is there a way to get double buffering to work with *everything* I add to the Frame?

(A Panel does not have the createBufferStrategy(2) capability.)

Share on other sites
The problem with AWT is that it depends on the platforms windowing system. That means your operating system is drawing the buttons etc, not you. The operating system doesn''t care about double buffering set up in Java.

***
For Java games and Java related resources, go to http://www.javaengines.dk
***

Share on other sites
Thats really not enough code to tell whats going on. Do you have a continuous thread of execution calling repaint()? You can force drawing by setting ignoreRepaint(true) and using getGraphics() to get the GC to draw on.

You might try using a Canvas - its a subclass of Component so it may work better in a frame. Beware of mixing Swing components with AWT components though. Most AWT components are 'heavy weights' ie widgets from the native os; where as swing components are 'lightweight' ie drawn by java on a heavyweight container.

You could just subclass a JPanel and draw on that.

[edited by - nonnus29 on April 2, 2004 10:16:42 PM]