Solved

Java Export JPanel contents to a png

Posted on 2013-12-20
9
964 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
object oriented programming comparison 5 77
SHA2 certs for IIS AND Java? 2 113
Java exception bubble up 2 21
Eclipse Help Java EE 5,6,7 Documentation, why not Java EE 8 8 30
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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 explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

809 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