This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Saving a Jpanel as a JPEG Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Saving a Jpanel as a JPEG" Watch "Saving a Jpanel as a JPEG" New topic
Author

Saving a Jpanel as a JPEG

Nick Austin
Greenhorn

Joined: Mar 20, 2010
Posts: 12
Hi Guys

I m currently writing an application which is my own version of MS Paint

My main functionality is there: drawing rectangles, lines, text etc...

The problem i have is that when attempting to save the file as a JPEG, the file outputted only displays the background from my JPanel, not anything i draw ontop of it

The code i am using is as follows;

drawing my rect onto my JPanel



Saving the JPanel as image



As i mentioned, the file saves OK, and if i change the background colour, the file will just have the background colour, with none of the pictures draw on top of it, any help will be appricated greatly

Thanks Nick Austin

Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

You paint the panel to the image and then drawImage the image to itself? That can't be right. Get rid of this line


luck, db
There are no new questions, but there may be new answers.
Nick Austin
Greenhorn

Joined: Mar 20, 2010
Posts: 12
Thanks for your reply

i have taken the line out, the code now reads



Still only saves the image as the background, not with any shapes drawn on top of it, any ideas on this?

Thanks alot Nick
Nick Austin
Greenhorn

Joined: Mar 20, 2010
Posts: 12
Ive tried everything, but all i can save is the background, WHY WHY WHY ??? is the forground (ie, anything i draw on my panel) not saved when i atempt to save the image. It makes no sense what so ever, if its drawing it to screen, why is it not saving the image as a whole ???


Please any help would do, ive spent all most 48 hours trying to fix this with no luck
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
To help us better see what is not working for you, I suggest that you create a small compilable and runnable program that we can use on our boxes at home to test and analyze and isolate the problem, an SSCCE

Luck
Nick Austin
Greenhorn

Joined: Mar 20, 2010
Posts: 12
thanks for the reply pete, i have made a scaled down version of my program for viewing, hopefully you can see where i am going wrong with the saving of the panel and any images drawn, rather than just a blank document being saved each time.

The code is long as it contains all my buttons / menus etc.

- Long meaningless code removed -

Hopefully from this you may be able to help

Thank again

Nick
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

That's not an SSCCE. Short, perhaps. self contained, probably. Correct, definitely not. There are over 100 errors when I try to compile that piece of code (after adding the missing trailing }).


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Mike Daly
Greenhorn

Joined: Nov 05, 2009
Posts: 24
The errors are all from missing declarations of type for the Components he adds in the initComponents() method. it compiles after that but good luck trying to sort through the resulting confusion, Hardly any of the functionality is implemented.
Nick Austin
Greenhorn

Joined: Mar 20, 2010
Posts: 12
sorry for the confusion guys

the only functionality i have implemented is to draw a line onto my panel, then when the butto is pressed save the panel (now with line on)

the problem i keep having is that the saved file only saves the bkground and no line, or any other image when i draw a rect / oval

the output is a blank image file, with the colour of the bkground...

thank you all for looking at it though
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
Graphics g = jPanel3.getGraphics();//wrong

do your painting the correct way and the code works fine - image of panel with drawn line is saved
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Nick Austin wrote:thanks for the reply pete, i have made a scaled down version of my program for viewing, hopefully you can see where i am going wrong with the saving of the panel and any images drawn, rather than just a blank document being saved each time.

The code is long as it contains all my buttons / menus etc.

Ouch! Please post shorter but compilable code.
Nick Austin
Greenhorn

Joined: Mar 20, 2010
Posts: 12
- Graphics g = jPanel3.getGraphics();//wrong

This may be wrong but any hints / tips to point me in the right direction would be very much appriciated

I have been staring at my screen for so long i cant get my head around it, it has compltly stumped me

Thanks again
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
start here

http://java.sun.com/docs/books/tutorial/uiswing/painting/index.html

had you posted a small compilable program, I could have added the extra for you.
you didn't, so I can't.

all you needed to post was a frame with panel and button, and the 2 listeners (totals < 70 lines)
Nick Austin
Greenhorn

Joined: Mar 20, 2010
Posts: 12
i do apologise for bing a bit slow tody, has been a very long weekend, i have now scaled down the sample prog, from this i hope you can make sense (ignoring the generated code for layout)



sorry for all the confusion, i do hope you can aid me in solving this problem

Thanks Nick
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
close enough, changes/comments in the code

Nick Austin
Greenhorn

Joined: Mar 20, 2010
Posts: 12
Mate your a ledge, have finally got an image saved that matches that of the image on screen. Thanks for all your help.

You are going to hate me for asking this next question but, with the code provided the ability is there to draw a line, then when you draw another the first is removed.

I beileve this is because of this line.



when i remove the line of code, it allows me to draw multiple lines, but when i attempt to save now, i get a black screen with the last line drawn??

any tips to point me i nthe right direction?

Thanks again Nick
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

I'm moving this to our GUI forum.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2155
    
    7
with the code provided the ability is there to draw a line, then when you draw another the first is removed.


Custom Painting Approaches.

While you are at it you may want to check out the ScreenImage class, which should answer your previous question.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
maybe this is already in the link provided (haven't read the link),
but these would be the steps.

create a class (say) Line, that has 4 x int parameters in its constructor
the parameters are the x,y of mousePressed and of mouseReleased



a class field Arraylist, call it (say) lines
java.util.List<Line> lines = new java.util.ArrayList<Line>();

in mouseReleased you add to 'lines' a new Line(...)


in paintComponent you iterate 'lines', drawing each line
Nick Austin
Greenhorn

Joined: Mar 20, 2010
Posts: 12
Once again many many thanks for the code, this has helped me immensly and i can now draw multiple lines on the screen and save the image as a Jpeg. Now one last question (honest) which i cannot for the life of me get my head around.

I have creaated my own rectangle class to allow the user to draw multiple rectangles.

I have also added a button that when clicked the status changes to rectangle, to allow them to draw that shape. My next problem encountered is that when the status is switched to rectangle, the lines are removed and the rectangles are only drawn, im sure its to do with calling the repaint method in the right place... but again cannot get my head around this..

any hints tips would be very much appriciated, the example of my code is as follows



Thanks again Nick
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
unlikely this is your problem
if (status == "line")

normally would be written as
if (status.equals("line"))

but why have the if()'s if you want all drawn?
you would need to iterate both 'lines' and 'rects'
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2155
    
    7
maybe this is already in the link provided (haven't read the link),


Multiple (working) solutions are provided. The one you suggested and one that makes solving the second question easier. Unless you understand the difference between the two, you don't know which solution is better to use.

But as usual, unless code is posted in the posting the OP doesn't bother to take a look.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Saving a Jpanel as a JPEG
 
Similar Threads
Why the jpg image produced by servlet is so poor ?
Drawing with mouse
Is this possible to do?
Image smoothing
Swing: Slow paint