Repainting a image in java

G

Guy

Hello

Ive seen tons of posts on this subject, but none of them reaaly helped
me with this particular problem. Im new to JAVA graphics but not to
java itself.

I have a JFrame with two buttons and a JPanel on it. when I push a
button a quite complicated process takes place (including database
access etc.) which results in an image drawn on the JPanel.

Now this image disappears whenever I resize the main frame. I managed
to make it draw again using repaint() and running the whole process
again each time the frame resizes, like it is said on many books and
websites. But this is very slow and resource consuming.
Is there a way to just save the image (maybe the Graphics2D object ?)
and redraw it again without all the hassle of constructing it from
scratch ?

Thank you

Guy
 
A

Andrew Thompson

Now this image disappears whenever I resize the main frame. I managed
to make it draw again using repaint() and running the whole process
again each time the frame resizes, like it is said on many books and
websites. But this is very slow and resource consuming.
Is there a way to just save the image (maybe the Graphics2D object ?)
and redraw it again without all the hassle of constructing it from
scratch ?

Write it to the Graphics of a BufferedImage.
Then g.drawImage() the result.

HTH
 
C

Chris Uppal

Guy said:
But this is very slow and resource consuming.
Is there a way to just save the image (maybe the Graphics2D object ?)
and redraw it again without all the hassle of constructing it from
scratch ?

Not really. Whatever you do will amount to a variant on the same basic idea:
save the data you need to reconstruct the graphics and use that to redraw when
the size/shape of the window changes.

You can think of "saving the data you need" in several ways, which will
correspondingly flavour your design and implementation.

+ you could create an abstract graphical description (scale independent) of
the "picture". I.e. generate a "display list" when the button is clicked, and
re-apply it to the pane whenever it changes size/aspect ratio. (The classes
implementing java.awt.Shape may help with this approach).

+ you could think of the saved data as a mere cache, used privately just to
speed up rendering. In this case you would save only the minimum that you need
and without regard to whether it forms a coherent body of information.

+ you could think of the data as a "model" (in the sense of MVC) which you
build from the database, and then the pane in your UI is a "view" (in the same
sense) that renders a way of displaying the model.

There may be other, or intermediate, ways of thinking about it too. But they
all come down to the same basic architecture -- save the data you will need
later.

-- chris
 
B

Babu Kalakrishnan

Andrew said:
Write it to the Graphics of a BufferedImage.
Then g.drawImage() the result.

I think this needs a little more clarification.

The disappearance of the image on resizing the frame indicates that the
painting of the panel is done outside its paintComponent method. In
order for the panel to maintain the image at all times, the painting
must be done inside this method. So the construction of the the image
and saving it to a BufferedImage object (as Andrew suggested) can be
done outside the paintComponent method, but the drawImage call onto the
actual component _must_ be performed inside the paintComponent method,
and should be done on the Graphics object passed to you as the argument
of the method.

BK
 
A

Andrei Kouznetsov

Ive seen tons of posts on this subject, but none of them reaaly helped
me with this particular problem. Im new to JAVA graphics but not to
java itself.

I have a JFrame with two buttons and a JPanel on it. when I push a
button a quite complicated process takes place (including database
access etc.) which results in an image drawn on the JPanel.

Now this image disappears whenever I resize the main frame. I managed
to make it draw again using repaint() and running the whole process
again each time the frame resizes, like it is said on many books and
websites. But this is very slow and resource consuming.
Is there a way to just save the image (maybe the Graphics2D object ?)
and redraw it again without all the hassle of constructing it from
scratch ?

you need something like ImagePanel:

class ImagePanel extends JPanel {

Image image;

public void setImage(Image image) {
this.image = image;
}

public void paintComponent(Graphics g) {
if(image != null) {
g.drawImage(image, 0, 0, this);
}
}

public Dimension getPreferredSize() {
int w, h;
if(image == null) {
return new Dimension(0, 0);
}
w = image.getWidth(null);
h = image.getrHeight(null);
return new Dimension(w > 0 ? w : 0, h > 0 ? h : 0);
}
}
 
T

Thomas Weidenfeller

Guy said:
Now this image disappears whenever I resize the main frame. I managed
to make it draw again using repaint() and running the whole process
again each time the frame resizes, like it is said on many books and
websites. But this is very slow and resource consuming.
Is there a way to just save the image (maybe the Graphics2D object ?)
and redraw it again without all the hassle of constructing it from
scratch ?

See the comp.lang.java.gui FAQ (just recently posted). In short,
implement a proper paintComponent() method.

/Thomas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top