Solved

printing in Java

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
thymeleaf natural templating vs JSP 2 95
Spring Framework HTTPSession management 1 33
hibernate example using maven 12 41
mysql jsp example issue 32 32
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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.

813 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

18 Experts available now in Live!

Get 1:1 Help Now