GeeCON Prague 2014*
The moose likes Swing / AWT / SWT and the fly likes drawing to the file with GDC Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "drawing to the file with GDC" Watch "drawing to the file with GDC" New topic
Author

drawing to the file with GDC

Johny Wyenski
Greenhorn

Joined: Sep 11, 2009
Posts: 21
Dear all,

I'd like to create a picture in a memory, then save it to the file and show preview (after creating in memory) on the screen.

I have started in such a way that from Oracle documentation I gave fragment of the code to my menu:



As far as I understand, now I have got the picture created in the memory. Then I need to show in in the window or to save to the file (I'm interested in both). How can I do it?

I also wonder how to e.g. draw rectangle or write text. I was doing it in such a way:



but I don't know how to apply it to pix[].

Regards!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

First issue, the saving. You can use JFileChooser to show a save dialog for you. After the user hits Save, the showSaveDialog method will return JFileChooser.APPROVE_OPTION. If this is the return value, get the selected File, and use ImageIO.write to save the image to a file.

As for the second issue, that's going to be tricky with direct image manipulation. Fortunately, BufferedImage can help you out:
Although I drew your original image, you can also use BufferedImage's setRGB method to set each pixel directly. That way you don't need the original image at all.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Johny Wyenski
Greenhorn

Joined: Sep 11, 2009
Posts: 21
Thank you!

I have the following code (this is one of menu options):



The difficulty for me is range of variables parent (and, in analogical way, width and height):

parent is not public in java.awt.Component; cannot be accessed from outside package


and using drawRect and drawString in the context of the code above.

Regards!
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4572
    
    5

Johny, please BeForthrightWhenCrossPostingToOtherSites.
http://www.java-forums.org/new-java/52121-draw-file-gdc.html


luck, db
There are no new questions, but there may be new answers.
Johny Wyenski
Greenhorn

Joined: Sep 11, 2009
Posts: 21
OK, so I'm going to post a new message on the other forum. And thank you again Rob Spoor for your very useful answer. In the future I'm going to write links to the other forum in case of crossposting or even avoid crossposting at all.
Regards!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

You're welcome
 
GeeCON Prague 2014
 
subject: drawing to the file with GDC