Win a copy of Java Challengers this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

How to store what the JPanel draw

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Marshal
Posts: 72657
317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
To avoid criticism do nothing, say nothing, be nothing. -Elbert Hubbard. Please critique this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic