File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes How to store what the JPanel draw Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to store what the JPanel draw" Watch "How to store what the JPanel draw" New topic
Author

How to store what the JPanel draw

Wei Chen
Greenhorn

Joined: Apr 04, 2008
Posts: 3
In my code( below ), I used Vectors which store the line point to resume the lines drawed before. But when filling or storing the color of the line, it will be hard to realize. Maybe using BufferedImage can solve it, but how to use?

I find a way to solve it now. In the constructor create a BufferedImage the same size as the JPanel, then use a Graphics2D object created by the image to paint the background of the BufferedImage to white. If to store the imformation to the image, using the Graphics2D obejct of the image to draw immediately. Then paint the image in the paint() or paintComponent().

<----initialize a BufferedImage object-->
BufferedImage infoStored = new BufferedImage( int width, int height,
BufferedImage.TYPE_INT_RGB );
Graphics2D g2d = infoStored.createImage();
g2d.setBackground( Color.WHITE );
g2d.clearRect( 0, 0, width, height ); //paint to white


<-- paint image -->
g.drawImage( infoStored, 0, 0, this );
[ April 08, 2008: Message edited by: Wei Chen ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40060
    
  28
Welcome to the Ranch.

You obviously missed the naming policy when you logged in: first name-space-last name. Please correct your displayed name to match.

No, we can't show you an example of what would work; you aren't going to learn anything from that. Rather, you show us what you have got and tell us what you think is going wrong. Please post what you have got, once you get the name thing sorted out.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> But after minimize the window, whatever drawed disappeared.

generally indicates you are using getGraphics(), which is the wrong way to do painting in swing

read the tutorial, and follow the examples
http://java.sun.com/docs/books/tutorial/uiswing/painting/index.html
Wei Chen
Greenhorn

Joined: Apr 04, 2008
Posts: 3
Originally posted by Michael Dunn:

generally indicates you are using getGraphics(), which is the wrong way to do painting in swing

read the tutorial, and follow the examples
http://java.sun.com/docs/books/tutorial/uiswing/painting/index.html


Thank you for your answer, now I put out my code, it does not useful.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to store what the JPanel draw