📄 hardcopywriter.java
字号:
synchronized (this.lock) { // Try to set a new font, but restore current one if it fails Font current = font; try { font = new Font("Monospaced", style, fontsize); } catch (Exception e) { font = current; } // If a page is pending, set the new font. Otherwise newpage() will if (page != null) page.setFont(font); } } /** End the current page. Subsequent output will be on a new page. */ public void pageBreak() { synchronized(this.lock) { newpage(); } } /** Return the number of columns of characters that fit on the page */ public int getCharactersPerLine() { return this.chars_per_line; } /** Return the number of lines that fit on a page */ public int getLinesPerPage() { return this.lines_per_page; } /** This internal method begins a new line */ protected void newline() { charnum = 0; // Reset character number to 0 linenum++; // Increment line number if (linenum >= lines_per_page) { // If we've reached the end of page page.dispose(); // send page to printer page = null; // but don't start a new page yet. } } /** This internal method begins a new page and prints the header. */ protected void newpage() { page = job.getGraphics(); // Begin the new page linenum = 0; charnum = 0; // Reset line and char number pagenum++; // Increment page number page.setFont(headerfont); // Set the header font. page.drawString(jobname, x0, headery); // Print job name left justified String s = "- " + pagenum + " -"; // Print the page # centered. int w = headermetrics.stringWidth(s); page.drawString(s, x0 + (this.width - w)/2, headery); w = headermetrics.stringWidth(time); // Print date right justified page.drawString(time, x0 + width - w, headery); // Draw a line beneath the header int y = headery + headermetrics.getDescent() + 1; page.drawLine(x0, y, x0+width, y); // Set the basic monospaced font for the rest of the page. page.setFont(font); } /** * This is the exception class that the HardcopyWriter constructor * throws when the user clicks "Cancel" in the print dialog box. **/ public static class PrintCanceledException extends Exception { public PrintCanceledException(String msg) { super(msg); } } /** * A program that prints the specified file using HardcopyWriter **/ public static class PrintFile { public static void main(String[] args) { try { if (args.length != 1) throw new IllegalArgumentException("Wrong # of arguments"); FileReader in = new FileReader(args[0]); HardcopyWriter out = null; Frame f = new Frame("PrintFile: " + args[0]); f.setSize(200, 50); f.show(); try { out = new HardcopyWriter(f, args[0], 10, .5, .5, .5, .5); } catch (HardcopyWriter.PrintCanceledException e) { System.exit(0); } f.setVisible(false); char[] buffer = new char[4096]; int numchars; while((numchars = in.read(buffer)) != -1) out.write(buffer, 0, numchars); in.close(); out.close(); } catch (Exception e) { System.err.println(e); System.err.println("Usage: " + "java HardcopyWriter$PrintFile <filename>"); System.exit(1); } System.exit(0); } } /** * A program that prints a demo page using HardcopyWriter **/ public static class Demo extends Frame implements ActionListener { /** The main method of the program. Create a test window */ public static void main(String[] args) { Frame f = new Demo(); f.show(); } // Buttons used in this program protected Button print, quit; /** Constructor for the test program's window. */ public Demo() { super("HardcopyWriter Test"); // Call frame constructor Panel p = new Panel(); // Add a panel to the frame this.add(p, "Center"); // Center it p.setFont(new Font("SansSerif", // Set a default font Font.BOLD, 18)); print = new Button("Print Test Page"); // Create a Print button quit = new Button("Quit"); // Create a Quit button print.addActionListener(this); // Specify that we'll handle quit.addActionListener(this); // button presses p.add(print); // Add the buttons to panel p.add(quit); this.pack(); // Set the frame size } /** Handle the button presses */ public void actionPerformed(ActionEvent e) { Object o = e.getSource(); if (o == quit) System.exit(0); else if (o == print) printDemoPage(); } /** Print the demo page */ public void printDemoPage() { // Create a HardcopyWriter, using a 14 point font and 3/4" margins. HardcopyWriter hw; try { hw=new HardcopyWriter(this, "Demo Page",14,.75,.75,.75,.75);} catch (HardcopyWriter.PrintCanceledException e) { return; } // Send output to it through a PrintWriter stream PrintWriter out = new PrintWriter(hw); // Figure out the size of the page int rows = hw.getLinesPerPage(), cols = hw.getCharactersPerLine(); // Mark upper left and upper-right corners out.print("+"); // upper-left corner for(int i=0;i<cols-2;i++) out.print(" "); // space over out.print("+"); // upper-right corner // Display a title hw.setFontStyle(Font.BOLD + Font.ITALIC); out.println("\n\t\tHardcopy Writer Demo Page\n\n"); // Demonstrate font styles hw.setFontStyle(Font.BOLD); out.println("Font Styles:"); int[] styles = { Font.PLAIN, Font.BOLD, Font.ITALIC, Font.ITALIC+Font.BOLD }; for(int i = 0; i < styles.length; i++) { hw.setFontStyle(styles[i]); out.println("ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz"); out.println("1234567890!@#$%^&*()[]{}<>,.?:;+-=/\\`'\"_~|"); } hw.setFontStyle(Font.PLAIN); out.println("\n"); // Demonstrate tab stops hw.setFontStyle(Font.BOLD); out.println("Tab Stops:"); hw.setFontStyle(Font.PLAIN); out.println(" 1 2 3 4 5"); out.println("012345678901234567890123456789012345678901234567890"); out.println("^\t^\t^\t^\t^\t^\t^"); out.println("\n"); // Output some information about page dimensions and resolution hw.setFontStyle(Font.BOLD); out.println("Dimensions:"); hw.setFontStyle(Font.PLAIN); out.println("\tResolution: " + hw.pagedpi + " dots per inch"); out.println("\tPage width (pixels): " + hw.pagesize.width); out.println("\tPage height (pixels): " + hw.pagesize.height); out.println("\tWidth inside margins (pixels): " + hw.width); out.println("\tHeight inside margins (pixels): " + hw.height); out.println("\tCharacters per line: " + cols); out.println("\tLines per page: " + rows); // Skip down to the bottom of the page for(int i = 0; i < rows-30; i++) out.println(); // And mark the lower left and lower right out.print("+"); // lower-left for(int i=0;i<cols-2;i++) out.print(" "); // space-over out.print("+"); // lower-right // Close the output stream, forcing the page to be printed out.close(); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -