A JCanvas

Hi all.
A simple question for a difficult answer (maybe). What is the best way to create a JCanvas? I mean in java.awt package there is the Canvas object but it is a Heavyweight object and it isn’t a good idea to mix Lightweight objects with HW objs (do not respect the z-order). How can I do? I tried to create a new one which extends JComponent and implements some methods for the creation of a BufferStrategy, but I got some problems to show images (they blink).

I show you what I’ve done:


public class JCanvas extends JComponent 
	[INNER CLASSES]

	private static final long serialVersionUID = 123456789L;
	private static final Dimension MINIMUM_SIZE = new Dimension(320, 200);
	
	transient ComponentPeer peer;
	private static VolatileImage vImage;
	private BufferStrategy strategy = null;
	
	public JCanvas() {
		super();
	}
		
	public void initStrategy() {
		if (this.getMinimumSize().height >= MINIMUM_SIZE.height && this.getMinimumSize().width >= MINIMUM_SIZE.width) {
			this.createBufferStrategy(2);
			this.strategy = this.getBufferStrategy();
		}
	}
	
    public BufferStrategy getBufferStrategy() {
        if (this.strategy == null) {
            createBufferStrategy(1);
        }
        return this.strategy;
    }
		
	public Graphics getBufferedGraphics() {
		return this.strategy != null ? this.strategy.getDrawGraphics() : this.getGraphics();
	}
	
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
	}
	
	void createBufferStrategy(int numBuffers) {
		BufferCapabilities bufferCaps;
		if (numBuffers > 1) {
			// Try to create a page-flipping strategy
			bufferCaps = new BufferCapabilities(
							new	ImageCapabilities(true), 
							new ImageCapabilities(true),
							BufferCapabilities.FlipContents.UNDEFINED);
			try {
				createBufferStrategy(numBuffers, bufferCaps);
				return;	// Success
			} catch	(AWTException e) {
				// Failed
			}
		}
		// Try a blitting (but still accelerated) strategy
		bufferCaps = new BufferCapabilities(
						new	ImageCapabilities(true), 
						new ImageCapabilities(true), null);
		try {
			createBufferStrategy(numBuffers, bufferCaps);
			return;	// Success
		} catch	(AWTException e) {
			// Failed
		}
		// Try an unaccelerated	blitting strategy
		bufferCaps = new BufferCapabilities(
						new	ImageCapabilities(false), 
						new ImageCapabilities(false), null);
		try {
			createBufferStrategy(numBuffers, bufferCaps);
			return;	// Success
		} catch	(AWTException e) {
			// Failed
		}
		// Code	should never reach here	(an	unaccelerated blitting
		// strategy	should always work)
		throw new InternalError("Could not create a	buffer strategy");
	}
	
	public void createBufferStrategy(int numBuffers, BufferCapabilities caps) 
	throws AWTException
	{
		// Check arguments
		if (numBuffers < 1) {
			throw new IllegalArgumentException("Number of buffers must be at least 1");
		}
		if (caps ==	null) {
			throw new IllegalArgumentException("No capabilities	specified");
		}
		// Destroy old buffers
		if (this.strategy instanceof FlipBufferStrategy) {
			((FlipBufferStrategy) this.strategy).destroyBuffers();
		}
		if (numBuffers == 1) {
			this.strategy = new SingleBufferStrategy(caps);
		} else {
			// assert numBuffers > 1;
			if (caps.isPageFlipping()) {
				this.strategy = new FlipBufferStrategy(numBuffers,	caps);
			} else {
				this.strategy = new BltBufferStrategy(numBuffers, caps);
			}
		}
	}
	
	protected void destroyBuffers() {
		if (peer !=	null) {
			peer.destroyBuffers();
		} else {
			throw new IllegalStateException("Component must	have a valid peer");
		}
	}
}

Use JPanel.

Uuh… well I tried to use JPanel and it does not work with me, but it doesn’t matter, I found the bug in my class… ;D How can I check if a component has a peer or not? I mean when I create for example a JPanel obj and I add it in the pane of a JFrame obj, there is a way to check the ComponentPeer of JPanel?

I though the only Swing classes with peers where JFrame JDialog and euhm…