//: c13:PrintDemo.java // From Thinking in Java, 2nd Edition // Available at http://www.BruceEckel.com // (c) Bruce Eckel 1999 // Copyright notice in Copyright.txt // Printing with the JFC // Note: This example compiles, but doesn't run // correctly. import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class PrintDemo extends JFrame { JButton text = new JButton("Print Text"), graphics = new JButton("Print Graphics"); JTextField ringNum = new JTextField(3); JComboBox faces = new JComboBox(); Graphics g = null; Plot plot = new Plot3(); //Try different plots Toolkit tk = Toolkit.getDefaultToolkit(); public PrintDemo() { ringNum.setText("3"); ringNum.addCaretListener(new RingL()); JPanel p = new JPanel(); p.setLayout(new FlowLayout()); text.addActionListener(new TBL()); p.add(text); p.add(new Label("Font:")); p.add(faces); graphics.addActionListener(new GBL()); p.add(graphics); p.add(new Label("Rings:")); p.add(ringNum); Container cp = getContentPane(); cp.setLayout(new BorderLayout()); cp.add(p, BorderLayout.NORTH); cp.add(plot, BorderLayout.CENTER); // Toolkit.getFontList() is deprecated GraphicsEnvironment ge=GraphicsEnvironment. getLocalGraphicsEnvironment(); String[] fontList = ge. getAvailableFontFamilyNames(); for(int i = 0; i < fontList.length; i++) faces.addItem(fontList[i]); faces.setSelectedItem("Serif"); } class PrintData { public PrintJob pj; public int pageWidth, pageHeight; PrintData(String jobName) { pj = getToolkit().getPrintJob( PrintDemo.this, jobName, null); if(pj != null) { pageWidth=pj.getPageDimension().width; pageHeight=pj.getPageDimension().height; g = pj.getGraphics(); } } void end() { pj.end(); } } class ChangeFont { private int stringHeight; ChangeFont(String face, int styl, int pnt){ if (g != null) { g.setFont(new Font(face, styl, pnt)); stringHeight = g.getFontMetrics().getHeight(); } } int stringWidth(String s) { return g.getFontMetrics().stringWidth(s); } int stringHeight() { return stringHeight; } } class TBL implements ActionListener { public void actionPerformed(ActionEvent e){ PrintData pd = new PrintData("Print Text Test"); // Null means print job canceled if(pd == null) return; String s = "PrintDemo"; ChangeFont cf = new ChangeFont( faces.getSelectedItem().toString(), Font.ITALIC,72); g.drawString(s, (pd.pageWidth - cf.stringWidth(s))/2, (pd.pageHeight - cf.stringHeight())/3); s = "A smaller print size"; cf = new ChangeFont( faces.getSelectedItem().toString(), Font.BOLD,48); g.drawString(s, (pd.pageWidth - cf.stringWidth(s))/2, (int)((pd.pageHeight - cf.stringHeight())/1.5)); g.dispose(); pd.end(); } } class GBL implements ActionListener { public void actionPerformed(ActionEvent e){ PrintData pd = new PrintData("Print Graphics Test"); if(pd == null) return; plot.print(g); g.dispose(); pd.end(); } } class RingL implements CaretListener { public void caretUpdate(CaretEvent e) { int i = 1; try { i = Integer.parseInt(ringNum.getText()); } catch(NumberFormatException ex) { i = 1; } plot.rings = 1; plot.repaint(); } } public static void main(String[] args) { JFrame pdemo = new PrintDemo(); pdemo.setTitle("Print Demo"); pdemo.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e){ System.exit(0); } }); pdemo.setSize(600,600); pdemo.setVisible(true); } } class Plot extends JPanel { public int rings = 3; } class Plot1 extends Plot { // Default print() calls paint(); public void paintComponent(Graphics g) { super.paintComponent(g); int w = getSize().width; int h = getSize().height; int xc = w/2; int yc = w/2; int x = 0,y = 0; for (int i = 0; i < rings; i++) { if (x