[java] AWT Double Buffering

Started by
4 comments, last by robinei 20 years ago
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() {
		addWindowListener(new WindowAdapter() {
			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]
Advertisement
Why don''t you just use the java.awt.image.BufferStrategy class? It handles both double, triple and n buffering....
garazdawi - 'I put the laughter back in slaughter'
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.
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.)
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
***

Developer journal: Multiplayer RPG dev diary

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]

This topic is closed to new replies.

Advertisement