Note that there are some explanatory texts on larger screens.

plurals
  1. POwhen is the print method of printable interface called in this code and ...?
    text
    copied!<pre><code>// Program to print simple text on a Printer import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.awt.print.PrinterException; import java.awt.print.*; class Printer extends JPanel implements Printable { JButton print; Printer() { buildGUI(); hookUpEvents(); } public void buildGUI() { JFrame fr = new JFrame("Program to Print on a Printer"); JPanel p = new JPanel(); print = new JButton("Print"); setPreferredSize( new Dimension ( 200,200 ) ); p.setBackground( Color.black ); fr.add(p); p.add( print , BorderLayout.CENTER ); fr.pack(); fr.setVisible( true ); } public void hookUpEvents() { print.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent ae ) { PrinterJob job = PrinterJob.getPrinterJob(); job.setPrintable( new Printer() ); boolean doPrint = job.printDialog(); if( doPrint ) { try { job.print(); } catch( PrinterException exc) { System.out.println( exc ); } } else { System.out.println("You cancelled the print"); } } }); } public int print( Graphics g , PageFormat pf , int pageIndex) throws PrinterException{ Graphics2D g2d = (Graphics2D)g; g2d.translate(pf.getImageableX(), pf.getImageableY()); g.drawString( "testing..." , 100 , 100 ); return PAGE_EXISTS; } public static void main( String args[] ) { new Printer(); } } </code></pre> <p>After the command <code>java Printer</code> the output is :</p> <p><img src="https://i.stack.imgur.com/pjN4H.jpg" alt="enter image description here"></p> <p>But as i click print , along with the following window :</p> <p><img src="https://i.stack.imgur.com/gkkBQ.jpg" alt="enter image description here"></p> <p>i again get the former window. Why does this happen ?</p> <p><strong>When does the <code>print</code> method of <code>Printable interface</code> get called in this program?</strong></p> <p><strong>Why does my window size come small when i have set =200,200.</strong></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload