At what point do we pass the arguments to the print method of the printerJob class?

0

I forgot to mention that what I want is to UNDERSTAND how the passage of arguments works.

This code is used to print a ticket (in a thermal printer with a width of 80mm) and works correctly does not have errors.

My question is: At what point is it that the arguments are passed to the print method of the printerJob class.

        printerJob pj = PrinterJob.getPrinterJob();
        PageFormat pf = pj.defaultPage();
        Paper paper = new Paper();

        paper.setImageableArea(10, 0, 200, 100);
        pf.setPaper(paper);

        pj.setPrintable(new Impresora(), pf);


          try {

            pj.print();
          } catch (PrinterException e) {
            System.out.println(e);
          }

On the line:

pj.print();

the method print is used but we do not pass any argument to it.

In another class that implements printable is where you have the method print which is what we call in the line of code that shows them above, and where it is seen that it is necessary to pass arguments but we do not, because as it is shown only we put " pj.prit(); " without arguments.

The Impresora class is where I have the print method and it is as shown below:

  class Impresora implements Printable 
  {
  public int print(Graphics g, PageFormat pf, int pageIndex) {
    if (pageIndex != 0)
      return NO_SUCH_PAGE;
    Date fecha = new Date();
    Graphics2D g2 = (Graphics2D) g;
    g2.setFont(new Font("Serif", Font.PLAIN, 10));
    g2.setPaint(Color.black);

    g2.drawString("Fecha "+fecha.getDate()+"-"+(fecha.getMonth()+1)+"-"+(fecha.getYear()+1900)+"   Hora "+fecha.getHours()+":"+fecha.getMinutes(), 10, 20);

    Rectangle2D outline = new Rectangle2D.Double(pf.getImageableX(), pf.getImageableY(), pf.getImageableWidth(), pf.getImageableHeight());
    g2.draw(outline);
    return PAGE_EXISTS;

}
}

At what point the arguments are passed to the method:

public int print(Graphics g, PageFormat pf, int pageIndex)
    
asked by Josue 21.11.2017 в 17:58
source

1 answer

0

When you call

pj.print();

You are calling when printing the PrinterJob class, that is, you never call

public int print(Graphics g, PageFormat pf, int pageIndex)

since it is a method of the Printer class and you are calling PrinterJob

    
answered by 21.11.2017 в 18:19