Solved

Java Export JPanel contents to a png

Posted on 2013-12-20
9
927 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Keep your audience engaged and get the most out of your next presentation with these quick Prezi tips.
Many programs have tried to outwit PowerPoint in terms of technology and skill. These programs, however, still lack several characteristics that PowerPoint has possessed from the start. Here's why PowerPoint replacements won't entirely work for desi…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
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.

760 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