FileDocCategorySizeDatePackage
PrintDemo1.javaAPI DocExample2302Sat Nov 25 12:55:10 GMT 2000None

PrintDemo1

public class PrintDemo1 extends Frame implements ActionListener
PrintDemo1 -- Print Demo Program - JDK1.1 Version Prints three pages (or so).

Fields Summary
protected static final int
NPAGES
The number of pages to print
protected int
nPages
The actual number of pages
PrintJob
pjob
The PrintJob object
Constructors Summary
PrintDemo1(int i)
Construct a PrintDemo1 with given number of pages

		nPages = i;
		setLayout(new FlowLayout());
		Button b;
		add(b = new Button("Print"));
		b.addActionListener(this);
		add(b = new Button("Quit"));
		b.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (pjob != null)	// if quit while printing!
					pjob.end();
				System.exit(0);
			}
		});
		pack();
	
PrintDemo1()
Construct a PrintDemo1 with default values

		this(NPAGES);
	
Methods Summary
public voidactionPerformed(java.awt.event.ActionEvent evt)
Action handler does the work of the printing test.

		Graphics pg = null;	// refers to current page
		System.out.println("Doing print");
		pjob = getToolkit().getPrintJob(this,
			"Printing Test", (Properties)null);
		if (pjob == null)          // User cancelled??
			return;
		Dimension pDim = pjob.getPageDimension();
		int pRes = pjob.getPageResolution();
		System.out.println("Page size " + pDim + "; Res " + pRes);
		// Print up to NPAGES
		for (int pgNum=0; pgNum<nPages; pgNum++) {
			System.out.println("Starting page # " + pgNum);
			pg = pjob.getGraphics();
			if (pg == null) 	// ??
				return;
			// setColor needed on Solaris (a bug!?)
			pg.setColor(Color.black);
			pg.setFont(new Font("Helvetica", Font.PLAIN, 48));
			pg.drawString("Hello World", 200, 200);
			pg.drawString("Page " + pgNum, 300, 300);
			System.out.println("Disposing of page " + pgNum);
			pg.dispose(); // flush page
			System.out.println("Disposed of page " + pgNum);
		}
		pjob.end();	// total end of print job.
		pjob = null;	// avoid redundant calls to pjob.end()
	
public static voidmain(java.lang.String[] av)
main program: instantiate and show.

	// refers to whole print job

	      
	     
		PrintDemo1 p;
		if (av.length>0)
			p = new PrintDemo1(Integer.parseInt(av[0]));
		else
			p = new PrintDemo1();
		p.setVisible(true);