• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to store what the JPanel draw

 
Wei Chen
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> 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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic