FileDocCategorySizeDatePackage
PrintFile.javaAPI DocExample2315Sat Nov 25 12:55:10 GMT 2000None

PrintFile

public class PrintFile extends Frame
PrintFile -- Print a file named onthe command line

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
PrintFile()
Construct a PrintDemo1 with given Filename

		setLayout(new FlowLayout());
		Button b;
		add(b = new Button("Cancel"));
		b.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (pjob != null)	// if quit while printing!
					pjob.end();
				System.exit(0);
			}
		});
		pack();
	
Methods Summary
public static voidmain(java.lang.String[] av)
main program: instantiate and show.

	// refers to whole print job

	      
	     
		PrintFile p = new PrintFile();
		p.setVisible(true);
		if (av.length==0)
			p.print(new InputStreamReader(System.in));
		else
			for (int i=0; i<av.length; i++)
				p.print(av[i]);
		p.setVisible(false);
		p.dispose();
		System.exit(0);
	
public voidprint(java.lang.String fn)
Print a file by name

		// open it, call the other guy 
		FileReader ifile = null;
		try {
			ifile = new FileReader(fn);
		} catch (FileNotFoundException fnf) {
			System.err.println("File not found!");
		}
		print(ifile);
	
public voidprint(java.io.Reader ifile)
Print a file by File

		BufferedReader is = new BufferedReader(ifile);
		Graphics g = 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);
		g = pjob.getGraphics();
		g.setColor(Color.black);
		g.setFont(new Font("SansSerif", Font.PLAIN, 12));
		if (g == null) 	// ??
			return;
		int y = 100;
		String line;
		try {
			while ((line = is.readLine()) != null) {
				g.drawString(line, 10, y+=18);
			}
		} catch (IOException e) {
			System.err.println(e);
		}
		// g.drawString("Page " + pgNum, 300, 300);
		g.dispose(); // flush page
		pjob.end();	// total end of print job.
		pjob = null;	// avoid redundant calls to pjob.end()