Reputation: 57
I found a usefull code but i don't understand two things from it. The first one is with if (page > 0)
. What does that mean ? And also here where it should print job.print();
. Why is it calling this method without any parameter and not this one i created earlier ? : public int print(Graphics g, PageFormat pf, int page)
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.print.*;
public class HelloWorldPrinter implements Printable, ActionListener {
public int print(Graphics g, PageFormat pf, int page) throws
PrinterException {
if (page > 0) { //Here
return NO_SUCH_PAGE;
}
//this one
System.out.println(pf.getWidth());
Graphics2D g2d = (Graphics2D)g;
g2d.translate(pf.getImageableX(), pf.getImageableY());
g.drawString("Hello world!", 100, 100);
return PAGE_EXISTS;
}
public void actionPerformed(ActionEvent e) {
PrinterJob job = PrinterJob.getPrinterJob();
job.setPrintable(this);
boolean ok = job.printDialog();
if (ok) {
try {
job.print(); //Here
} catch (PrinterException ex) {
}
}
}
public static void main(String args[]) {
UIManager.put("swing.boldMetal", Boolean.FALSE);
JFrame f = new JFrame("Hello World Printer");
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
});
JButton printButton = new JButton("Print Hello World");
printButton.addActionListener(new HelloWorldPrinter());
f.add("Center", printButton);
f.pack();
f.setVisible(true);
}
}
Upvotes: 1
Views: 3048
Reputation: 35011
Q1: if (page > 0)
. What does that mean ? - This makes sure only the first page is printed. Because you are using a Printable
, rather than a Pageable
, it doesn't know how many pages there are to print.
Q2: And also here where it should print job.print(); - It should call job.print where you want to send the print job to the printer. In your case, on the button click
Q3: Why is it calling this method without any parameter and not this one i created earlier ? - These are two different classes and objects. You are calling print() on a PrinterJob
, which, calls back your Printable
print method to print the page. On the line: job.setPrintable(this);
that's how it knows to call back the print method you specified
Upvotes: 0
Reputation: 8348
The first one is with if (page > 0) . What does that mean ?
Printing can consist of multiple pages - the PrinterJob
does not know how many pages to print, so it will call the print
method of your Printable
implementation until it is told to do so (eg NO_SUCH_PAGE
is returned). One of the parameters passed to your implementation of the print
(eg page
) is incremented for every page, and indicates the current page number being printed. As a result, this conditional limits the printing to a single page.
And also here where it should print job.print(); . Why is it calling this method without any parameter and not this one i created earlier ?
You first call print
on the PrinterJob
, PrinterJob
then creates the variables which are then passed to your print
implementation (sometimes multiple times, depending upon the number of pages) - so you are indirectly calling your Printable
implementation (eg the 3 parameter print
method).
Oracle has a great printing tutorial worth reading in depth that might further answer questions you have.
Upvotes: 2