my dog learned polymorphism
The moose likes Swing / AWT / SWT and the fly likes Overwriting on a jpg in a Jpanel; Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Overwriting on a jpg in a Jpanel;" Watch "Overwriting on a jpg in a Jpanel;" New topic

Overwriting on a jpg in a Jpanel;

Tau Slater

Joined: Jan 30, 2009
Posts: 12
I can write my JPanel to an image, and read that image and dislpay it to the JPanel, but can't draw more or "add" to that image when it's loaded. Do I need to use a bufferedimage or will a plain "image" be ok? I assume this is GUI related as the images load and save ok. Here's what happens ideally:

1. List of items chosen.
2. Picture drawn in DrawingPanel (extends JPanel) and saved by list index
3. When the list is loaded again, the picture comes up. User can draw on it and save it again if neccessary.
4. Picture saved again.

The trouble happens when the picture is loaded up a second time. I can either write pictures the the hdd, or read them and display them... Can't do the last step

How image is stored:

The paintComponent method in PaintPanel: (where the picture is drawn - PaintPanel is a nested class, in a JFrame.)

I have a feeling the paint method isn't set up to write over an existing picture? I know the jpg quality is going to deteriorate as well; I just want to be able to re-write over a jpg when it's loaded up and save the altered image.
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
Tau Slater

Joined: Jan 30, 2009
Posts: 12
Thanks a lot Craig, I notice you're using a png file as well. I'd noticed some compression artefacts in my jpg when saved, as I'd gone with the default encoding settings and not played around with it on that front, but I think I'll play around with the formats and see what I can do.

Thanks again
Tau Slater

Joined: Jan 30, 2009
Posts: 12
HAHA! Victory is mine! I've used some of your code Craig, but the important thing is I understand what was going on and why it didn't work. I *was* loading up the image but not creating a graphics context from it, so I used to be able to load an image and se eit, or draw one and not edit it... Fixed now - the heart of it was:

Notably, I also didn't see this as I was at first drawing with a white colour on a white background. Sometimes even the simple things can catch you out eh?... On an asides, drawing and painting in Java is a real pain isn't it? Or is it just me? I mean I whacked a program together like this in C# on visual studio in half an hour... Oh well. I'm hardly a coding guru anyway. Thanks again for your help Craig
I agree. Here's the link:
subject: Overwriting on a jpg in a Jpanel;
jQuery in Action, 3rd edition