2d graphics context from component to image

tony navaratnam

Greenhorn

Posts: 14

posted 10 years ago

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

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

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???

Michael Dunn

Ranch Hand

Posts: 4632

posted 10 years ago

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

to get a solution it is far better NOT to post in multiple forums.

Craig Wood

Ranch Hand

Posts: 1535

posted 10 years ago

how do i go about creating an image file that contains the context of an component??? I answered this question in reply 3 of this thread. In it I also tried to offer/demonstrate some suggestions about scaling an image, locating the image in the component that is showing it and how to set up the component that will draw it into the image–to–save. Let's start with some problem areas where you are now:getGraphics does not work well for graphics. Why? Your approach is to show an image in a JLabel, add graphics to the label using getGraphics and then ask the label to draw itself into a BufferedImage. The component that will do the drawing of itself (JLabel) has no idea that you have obtained a reference to its graphics context and used it (the graphics context) to draw things on top of it (the component). So it will be unable to render what you have added when it draws itself into your BufferedImage. The way to do this is to build a class that will remember all that you have drawn in/on it so it can render everything into the BufferedImage. This requires that you build some state into the component such as a list or array of text strings (added by the user) and their location (in another data structure). And another list/array to hold the user–added Shapes which can then be faithfully rendered on request. To show this shortcoming of using getGraphics, add some text or shapes to your JLabel and then either minimize/restore the app or partially cover/uncover it with another app — the added graphics are not restored when the app is repainted. The key is to build your graphic class so that it can render everything you want it to display at any time. Calling getGraphics from event code adds things that will not persist through system/user–initiated repaint calls. Using a JLabel for this component presents another problem. A JLabel is non-opaque by default. So it is rendered by its next parent container (up the hierarchy) that is opaque. This can lead to trouble when trying to draw it into an image. Also, a new BufferedImage has a black background. To change this you will have to fill it with a color, maybe the background color behind your JLabel. A JPanel might make a better component for this kind of image editing. It depends on what you want to do; there are many possibilities and, therefore, many ways to get into trouble. There seems to be no need for an AlphaComposite in your image creation method. If you want some of the saved image to be non–opaque there is another way to do it. There are more things to talk about but this and the previous contribution should be enough to get you started down the right path.

tony navaratnam

Greenhorn

Posts: 14

posted 10 years ago

Thank you. I ahve experimented with that and have found it to work. now i can carry on. thank you again