Capturing and Redrawing an Image
Hi,
I'm building a small drawing application in Swing. I'm using JComponent as the drawing board,
I capture the drawing on the JComponent using the following code:
RepaintManager manager = RepaintManager.currentManager(drawBoard);
drawing = manager.getOffscreenBuffer(drawBoard, drawBoard.getWidth(), drawBoard.getHeight());
and I use the following code for redrawing the image on the JComponent
getGraphics().drawImage(drawing,0,0,drawBoard.getWidth(), drawBoard.getHeight(),drawBoard);
What happens when redrawing is that I get other parts on the GUI redrawn on the drawing board!
Any one can tell me what am I doing wrong? should I try something else?
Thanks in advance,
Ayman
Re: Capturing and Redrawing an Image
I've had similar problems. You are supposed to draw on a JComponent (such as JPanel) by overriding the
method paintComponent. I was trying to draw outside of this method by getting a graphics context with
getGraphics like you and having mixed results. I asked my teacher and he said to stay away from the
getGraphics method and try to put all my rendering code inside paintComponent. Try this
public void paintComponent(Graphics g)
{
super.paintComponent(g);
//additional drawing code here using this g
//g.drawImage(image,0,0,this);
}
if you forget to call the superclasses paintComponent you will get strange results too. I did it once
and my menus were being painted on all my panels.