?
Solved

Printing out a JFrame or a JPanel

Posted on 2003-03-12
8
Medium Priority
?
907 Views
Last Modified: 2013-11-23
Hi,
Is there anyway of printing out a JFrame, or a JPanel??   Would you have to convert them to an jpeg or something, or is their an easy method in Java?
0
Comment
Question by:hello1111
[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
8 Comments
 
LVL 1

Expert Comment

by:pronane
ID: 8120580
well it depends on what exactly you are trying to do, you could just call the
ctrl alt + printscreen.
0
 
LVL 35

Expert Comment

by:girionis
ID: 8120606
0
 
LVL 35

Accepted Solution

by:
girionis earned 800 total points
ID: 8120662
0
Independent Software Vendors: 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!

 

Expert Comment

by:alanbergin
ID: 8703850
What I want to do is add a button to print out a screenshot of the two panels, each taking up half of the same A4 page.

It's not so much a problem printing as creating the images. Here's the code:

<PRE>
class MainWindow
{
   //...other code...

   public void actionPerformed(ActionEvent e)
   {
      if(e.getSource() == printButton)
      {
         Image imageOne = panelOne.createImage(panelOne.getWidth(), panelOne.getHeight());
         Image imageTwo = panelTwo.createImage(panelTwo.getWidth(), panelTwo.getHeight());
         System.out.println(imageOne.getWidth(this) + ", " + imageOne.getHeight(this));
         // The above line prints out "956, 458" which is the panels size.

         // PrintWindow displays fine when using these two sample images...
         //Image imageOne = new ImageIcon("1.jpg").getImage();
         //Image imageTwo = new ImageIcon("2.jpg").getImage();

         PrintWindow pw = new PrintWindow(imageOne, imageTwo);
         pw.setVisible(true);
         PrintUtilities.printComponent(canvas);
      }
   }
}

class PrintWindow extends JFrame
{
   private Image imageOne, imageTwo;

   PrintWindow(final Image imageOne, final Image imageTwo)
   {
      this.imageOne = imageOne;
      this.imageTwo = imageTwo;
      this.setSize(imageOne.getWidth(this), imageOne.getHeight(this)+imageTwo.getHeight(this));
      JPanel panel = new JPanel() {
         public void paint(Graphics g)
         {
            super.paint(g);
            g.drawImage(imageOne, 0, 0, this);
            g.drawImage(imageTwo, 0, imageOne.getHeight(this), this);
         }
      };
      getContentPane().add(panel);
   }
}
</PRE>

'createImage' is definitely returning *something* as it has a size and if I add in the following two lines to 'paint':
g.setColor(Color.RED); g.drawString("Hello", 50, 50);
...the word 'Hello' is drawn ONLY if those two lines come after the two calls to g.drawImage(...), so the createImage method must just be returning two big grey rectangles. Why?

 - Alan.
0
 

Expert Comment

by:alanbergin
ID: 8703856
Apologies, that post was meant for another thread.
 - Alan.
0
 

Expert Comment

by:CleanupPing
ID: 9058741
hello1111:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
LVL 35

Expert Comment

by:girionis
ID: 9714420
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

- Points to girionis

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

girionis
EE Cleanup Volunteer
0
 

Expert Comment

by:alanbergin
ID: 9733179
Hmm... the author of the question is listed as 'hello1111'. That's not me, yet I was the one who posted this question. So I can't accept anyone's answer.
 - Alan.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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 practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
Suggested Courses
Course of the Month9 days, 13 hours left to enroll

762 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