Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Printing out a JFrame or a JPanel

Posted on 2003-03-12
8
Medium Priority
?
909 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
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month10 days, 18 hours left to enroll

572 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