Solved

printing in Java

Posted on 1998-09-01
5
222 Views
Last Modified: 2010-03-30
Is there a way to printer print a gif
file in Java? Or am I limited to what is
defined in the paint() method? Also, can
I print the components that is added to the
frame? Can I print anything other than frames?

Thanks for the help.
0
Comment
Question by:harbinger072198
[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
  • 2
  • 2
5 Comments
 
LVL 2

Expert Comment

by:aziz061097
ID: 1223180
I had answered this question earlier , I am repeating the answer hope this helps

Printing a graphics object needs a reference to a "PrintGraphics" object.

Getting a reference to the PrintGraphics can be done by the following steps

1. Obtaining the reference to the Frame
2. Obtaining the refernce to the default toolkit
3.Invoke the toolkit's getPrintJob() method, passing it as a parameter the Frame

4. on the print job object now call the getGraphics() method which returns a reference to the PrintGraphics

You can either pass reference to a PrintGraphics object to a component's print() or printAll() method. Printing the graphics can be done by calling the graphic's dispose() method which flushes it to the printer, after the printing is done call the end() method

//PrintGraphics.java

import java.awt.*;
import java.awt.event.*;
import java.util.Properties;


public class PrintGraphics extends Frame {

Button printButton = new Button ("print ...");
Label printLabel = new Label("print this label");
TextField printTextField =new TextField("print this textfield");
//constructor
PrintGraphics(){
setLayout(new FlowLayout());
add(printButton);
add(printLabel);
add(printTextField);

printButton.addActionListener (new ActionListener() {
    public void actionPerformed(ActionEvent e){
//printApp(Printgraphics.this);
        printApp(printButton);
        printApp(printLabel);
        printApp(printTextField);
    }
});
}

public static void main(String args[]){
PrintGraphics PG = new PrintGraphics() ;
PG.setSize(300,200);
PG.setVisible(true);
}
static Frame getFrame(Component x) {
  while((x= x.getParent()) != null){
      if (x instanceof Frame)
        return (Frame) x;
   }
return null;
}


static void printApp(Component x) {

 Frame frame = getFrame(x);
   
// Frame frame = getFrame(printButton);

 Toolkit tk = Toolkit.getDefaultToolkit();

 if(tk != null) {
   String name = x.getName() + " Print this";

   PrintJob job1 = tk.getPrintJob(frame,name,(Properties) null);

    if(job1 != null) {
      Graphics pg = job1.getGraphics();

       if (pg != null) {
       System.out.println("The component is printed");
        x.printAll(pg);
        pg.dispose();

       pg.drawLine(20,20,60,50);
       pg.dispose();   ///test drawing a line
}

     job1.end();
     }
}
}
}
 


0
 

Author Comment

by:harbinger072198
ID: 1223181
I tried it and it didn't work. Arrggghh..
0
 
LVL 2

Expert Comment

by:aziz061097
ID: 1223182
What did not work ? Printing of gifs or any components at all ?
0
 
LVL 3

Accepted Solution

by:
sailwind earned 150 total points
ID: 1223183
Is there a way to printer print a gif  file in Java?
Or am I limited to what is defined in the paint() method? Also, can
I print the components that is added to the  frame? Can I print
anything other than frames?

----

Aziz have explained to you how the printing should be written,
I'll try to be specific and answer your question directly.

- With the printjob class, you can print just about any object
    in the frame.
- Note that the printing is limited to what the frame contains. This
  includes what's defined in the paint method, and any components
  that you add to it.
- For components added to the frame, only the ones that are currently
  being shown in the frame window will be printed, others outside the
  view range will be trimmed.
- For graphics that's drawn in the paint() method, it can be printed even
  though the window does not show them
- When you invoke printAll(), this will print the frame's paint() method, and
  recursively call the paint of all the subsequent components.
- You can print just about any awt component, but at this point, you will not
  be able to print any swing components :(.


0
 

Author Comment

by:harbinger072198
ID: 1223184
Hmm.. Alright thanks. Thanks aziz for the code, I needed some
info on what to expect for printing. Just a beginner! I'll try out your code
in the future.
0

Featured Post

Industry Leaders: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Android development question 2 76
sql import cannot be resolved jsp 3 74
Fast way to search item into Java Array (Rhino compatible) 2 37
JDBC performance 8 28
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…
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…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

763 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