Java Reference
In-Depth Information
Example 12−3: HardcopyWriter.java (continued)
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
Search WWH ::




Custom Search