Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Swing / AWT / SWT and the fly likes creating an image from an modified component Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "creating an image from an modified component" Watch "creating an image from an modified component" New topic

creating an image from an modified component

tony navaratnam

Joined: Dec 27, 2006
Posts: 14
i apologise if this is in the wrong place but i am a beginer in java and that is part of the problem!!
I have been asked to produce an GUI that will let the user open annotate and then save an image from and to file.
currently i am using a jlabel as a canvas to open the image using a draw image method like so

Graphics2D g1 = (Graphics2D) jLabel1.getGraphics();
g1.drawImage(scaledBI, 0, 0, scaledWidth, scaledHeight, null);

i am then letting users add shapes to this using a simalar method. First jLabel.getGraphics to get an graphics2d variable and then this is used for drawing lines etc. to demostrate here is an exampe from the code

Graphics2D g = (Graphics2D) jLabel1.getGraphics();

all this has been fine but saving the changed contents of the jLabel has caused problems. here is the code as is

scaledHeight=500 ;
scaledWidth= 500;
int imageType = BufferedImage.TYPE_INT_RGB;

BufferedImage image = new BufferedImage(scaledWidth, scaledHeight, imageType);
Graphics2D g2d = image.createGraphics();
jLabel1.paint( g2d );

this image when saved to file and opened externally has been giving me a mostly black page with the original text that is on the jLabel on starting the application.

how do i go about creating an image file that contains the context of an component???
Nathan Pruett

Joined: Oct 18, 2000
Posts: 4121

The problem is that you're assuming that the Graphics2D reference saves it's state... it doesn't. Graphics2D (and Graphics) is just a way for components to get at the graphics context *at the moment* to paint to the screen. All *permanent* changes must go in the component's paint() method (or methods called from paint()) or must be painted to an image (such as BufferedImage). Components and images *use* Graphics2D to do this, but Graphics2D isn't where these changes get saved - it's the image or the component itself. So, when you grab the Graphics2D reference from the label and draw to it - you draw on it *right then*, but any changes to the label will draw the default label - the changes you made to the label through Graphics2D will disappear - try minimizing and restoring the frame your label's on to see. When you make the call to paint(), the label goes into it's paint() method and paints itself according to that - the temporary changes made through Graphics2D get lost.

My suggestion would be to use the label purely to display the images - don't use it to paint shapes or save the image. Use one BufferedImage for the "background" image, and use another BufferedImage with alpha set painted on top of this for drawing your shapes ("foreground" image). When you save, create another BufferedImage temporarily and first paint the background image, then paint the foreground image on top, then save the new BufferedImage.

Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Nathan Pruett

Joined: Oct 18, 2000
Posts: 4121

Closing duplicate - here is the other post.
I agree. Here's the link:
subject: creating an image from an modified component
It's not a secret anymore!