Solved

Java Export JPanel contents to a png

Posted on 2013-12-20
9
941 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
  • 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to parse custom JSON to POJO java 4 55
Java - Why doesn't this JFrame work 3 41
more than one jdk and one jre 1 32
map related example 6 35
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

910 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now