• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 228
  • Last Modified:

printing in Java

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
harbinger072198
Asked:
harbinger072198
  • 2
  • 2
1 Solution
 
aziz061097Commented:
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
 
harbinger072198Author Commented:
I tried it and it didn't work. Arrggghh..
0
 
aziz061097Commented:
What did not work ? Printing of gifs or any components at all ?
0
 
sailwindCommented:
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
 
harbinger072198Author Commented:
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

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!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now