Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Java Export JPanel contents to a png

Posted on 2013-12-20
9
Medium Priority
?
1,074 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
Technology Partners: 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 2000 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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…
Learn how to set up basic frames and paths in Prezi and understand the open space that Prezi allows you to create presentations in.
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…
Suggested Courses

610 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