Solved

printing in Java

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

Title # Comments Views Activity
by zero exception 10 79
maven disable workspace resolution 1 67
add projects t working set in maven 2 62
Tagging and Merging on Branch 1 65
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
The viewer will learn how to implement Singleton Design Pattern in Java.
This video teaches viewers about errors in exception handling.
Suggested Courses

740 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