Solved

printing in Java

Posted on 1998-09-01
5
217 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
  • 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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 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…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now