wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes Swing to Image conversion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Swing to Image conversion" Watch "Swing to Image conversion" New topic
Author

Swing to Image conversion

Vani Shastri
Ranch Hand

Joined: Aug 17, 2006
Posts: 52
Hi all,

I am new to this topic of converting swing into an image.

1. Why should we convert a swing into an image?
2. How to implement the same?

Thank you.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
not sure exactly what you're asking, but the easiest way to display an image
in swing is to use a JLabel and it's icon property

JLabel label = new JLabel(new ImageIcon("test.gif"));
Vani Shastri
Ranch Hand

Joined: Aug 17, 2006
Posts: 52
Hi Micheal,

My doubt was- how to save a JFrame as a jpeg/gif image?

Thanks
Vani Shastri
Ranch Hand

Joined: Aug 17, 2006
Posts: 52
Hi once again,

finally i found it :

BufferedImage image = new BufferedImage(w, h,
BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = image.createGraphics();
comp.paint(g2);
g2.dispose();
ImageIO.write(image, "jpeg", new File("example.jpeg"));

Thanks for the help offered.
Vani Shastri
Ranch Hand

Joined: Aug 17, 2006
Posts: 52
another doubt



The destination of jpeg image that is created is by default where its class file is present.
How to save the jpeg at the required destination ( i dont want to mention the path in the class)? just the same way how we get the dialog box when we save any other file.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
use a JFileChooser's showSaveDialog()
Vani Shastri
Ranch Hand

Joined: Aug 17, 2006
Posts: 52
Hi Michael,

Yeah it worked !!! Thanks.
 
 
subject: Swing to Image conversion