Solved

Java Export JPanel contents to a png

Posted on 2013-12-20
9
977 Views
Last Modified: 2013-12-21
I have my own customized JPanel (MyJpanel extends JPanel) that has been added to a JFrame. I've been adding graphics and images to this JPanel from within its paintComponent method.
I need to export the contents of my JPanel to a png file. I know it goes something like this:

ImageIO.write(rendImage, "png", new File( "screenshot.png" ) );

...but I don't know how to get the rendImage. How do I specify the contents?
0
Comment
Question by:MarkLoveExEx
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 39732293
Create an instance of BufferedImage the same size as your panel. Get its Graphics ('g'). Call your panel's paint method, passing 'g' as the argument
0
 

Author Comment

by:MarkLoveExEx
ID: 39732337
I'm still not quite getting it. Where should I put the BufferedImage line? Should it be outside of my panel, or within the paintComponent of my JPanel? How do I "get its graphics"?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39732372
Not in paintComponent, but it doesn't much matter otherwise

How do i "get its graphics"?

http://docs.oracle.com/javase/7/docs/api/java/awt/image/BufferedImage.html#getGraphics()
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:MarkLoveExEx
ID: 39732497
Perhaps some code will help:
            BufferedImage im = new BufferedImage(572, 665, BufferedImage.TYPE_INT_RGB);
            im.getGraphics(JPanel_TOP.paint(g));   ** doesn't know what g is **
            JPanel_TOP.paint(im.getGraphics());      
            try {
                  ImageIO.write(im, "png", new File("screenshot.png"));
            } catch (Exception e) {
            }
            
            add(JPanel_TOP);

Here's my panel:
public class MyJpanel extends JPanel
{
   // draw shapes with Java 2D API
   public void paintComponent( Graphics g )
   {
      super.paintComponent( g ); // call superclass's paintComponent
 
      setBackground(Color.white);
      Graphics2D g2d = ( Graphics2D ) g; // cast g to Graphics2D

      // draw 2D rectangle in red  
     
      g2d.setPaint( Color.red );                            
      g2d.setStroke( new BasicStroke( 5.0f ) );            
      g2d.draw(new Rectangle2D.Double(80,30,100,100));
      g2d.fill(new Rectangle2D.Double(185,135,80,80));


     // draw 2D lines in green and yellow              
      g2d.setPaint( Color.BLACK );                      
      g2d.draw( new Line2D.Double( 395, 30, 320, 150 ) );

      String filename = "D:/practice/noaa_logo.png";
      try {
            Image image = ImageIO.read(new File(filename));
            g2d.drawImage(image, 0, 0, null);
      } catch (Exception e) {
      }
      String filename2 = "D:/practice/nws_logo.png";
      try {
            Image image = ImageIO.read(new File(filename2));
            g2d.drawImage(image,480, 0, null);
      } catch (Exception e) {
      }
      String filename3 = "D:/practice/spatial_plot_0_0_0.png";
      try {
            Image image = ImageIO.read(new File(filename3));
            g2d.drawImage(image,0, 75, null);
      } catch (Exception e) {
      }
      String filename4 = "D:/practice/spatial_plot_0_1_0.png";
      try {
            Image image = ImageIO.read(new File(filename4));
            g2d.drawImage(image,0, 475, null);
      } catch (Exception e) {
      }
      String filename5 = "D:/practice/legend_status.png";
      try {
            Image image = ImageIO.read(new File(filename5));
            g2d.drawImage(image,380, 435, null);
      } catch (Exception e) {
      }
     
     
     
   }
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39732598
 im.getGraphics(JPanel_TOP.paint(g));   ** doesn't know what g is **

Open in new window

Delete that. Otherwise you should be ok
0
 

Author Comment

by:MarkLoveExEx
ID: 39732630
All I get is a png with all black.
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 39732689
        } catch (Exception e) {
        }

Open in new window

http://technojeeves.com/joomla/index.php/free/135-ignoring-exceptions-is-dangerous

Try attached
FB.java
MyJpanel.java
0
 

Author Closing Comment

by:MarkLoveExEx
ID: 39733161
Thank you!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39733489
:)
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
throw exception 21 68
JavaFX TableView not displaying correctly 3 108
Fast way to search item into Java Array (Rhino compatible) 2 39
DTD and JAVA versions 1 33
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Learn the basics of inputting and editing your text components in Prezi. We will cover how to set styles, position, and group your text components. In your Prezi editor, click anywhere on the canvas to add text: A flashing cursor informs you that yo…
It’s easy to embed any of your public Prezi presentations on your website or social network to share with others. Learn how simple it is in this tutorial.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question