FileDocCategorySizeDatePackage
PrintDemoGfx.javaAPI DocExample2168Sun Aug 24 08:31:02 BST 2003None

PrintDemoGfx

public class PrintDemoGfx extends Object
PrintDemoGfx -- Construct and print a GfxDemoCanvas. Java 2 VERSION, using a PrinterJob.

Fields Summary
final boolean
quiet
Constructors Summary
public PrintDemoGfx(boolean q)

		quiet = q;
		final JFrame f = new JFrame("Printing Test Dummy Frame");

		// Construct the object we want to print. Contrived:
		// this object would already exist in a real program.
		final GfxDemoCanvas thing = new GfxDemoCanvas(400, 300);

		f.getContentPane().add(thing, BorderLayout.CENTER);

		JButton printButton = new JButton("Print");
		f.getContentPane().add(printButton, BorderLayout.SOUTH);

		printButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					PrinterJob pjob = PrinterJob.getPrinterJob();
					pjob.setJobName("DemoGfx - Graphics Demo Printout");
					pjob.setCopies(1);
					// Tell the print system how to print our pages.
					pjob.setPrintable(new Printable() {
						/** called from the printer system to print each page */
						public int print(Graphics pg, PageFormat pf, int pageNum) {
							if (pageNum>0)		// we only print one page
								return Printable.NO_SUCH_PAGE;	// ie., end of job

							// Now (drum roll please), ask "thing" to paint itself
							// on the printer, by calling its paint() method with 
							// a Printjob Graphics instead of a Window Graphics.
							thing.paint(pg);

							// Tell print system that the page is ready to print
							return Printable.PAGE_EXISTS;
						}
					});

					if (!quiet && pjob.printDialog() == false)	// choose printer
						return;				// user cancelled

					pjob.print();			 // Finally, do the printing.
				} catch (PrinterException pe) {
					JOptionPane.showMessageDialog(f,
						"Printer error" + pe, "Printing error",
						JOptionPane.ERROR_MESSAGE);
				}
			}
		});

		f.pack();
		f.setVisible(true);
	
Methods Summary
public static voidmain(java.lang.String[] av)
Simple demo main program.

		boolean quiet = false;
		if (av.length > 0 && av[0].startsWith("-q"))
			quiet = true;
		new PrintDemoGfx(quiet);