• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Saving a Jpanel as a JPEG

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You paint the panel to the image and then drawImage the image to itself? That can't be right. Get rid of this line
 
Nick Austin
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 22862
132
Eclipse IDE Spring TypeScript Quarkus Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 }).
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
- 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
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
close enough, changes/comments in the code

 
Nick Austin
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22862
132
Eclipse IDE Spring TypeScript Quarkus Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm moving this to our GUI forum.
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic