home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in Java / c13 / PrintDemo.java < prev    next >
Encoding:
Java Source  |  2000-05-25  |  7.7 KB  |  250 lines

  1. //: PrintDemo.java
  2. //////////////////////////////////////////////////
  3. // Copyright (c) Bruce Eckel, 1998
  4. // Source code file from the book "Thinking in Java"
  5. // All rights reserved EXCEPT as allowed by the
  6. // following statements: You can freely use this file
  7. // for your own work (personal or commercial),
  8. // including modifications and distribution in
  9. // executable form only. Permission is granted to use
  10. // this file in classroom situations, including its
  11. // use in presentation materials, as long as the book
  12. // "Thinking in Java" is cited as the source. 
  13. // Except in classroom situations, you cannot copy
  14. // and distribute this code; instead, the sole
  15. // distribution point is http://www.BruceEckel.com 
  16. // (and official mirror sites) where it is
  17. // freely available. You cannot remove this
  18. // copyright and notice. You cannot distribute
  19. // modified versions of the source code in this
  20. // package. You cannot use this file in printed
  21. // media without the express permission of the
  22. // author. Bruce Eckel makes no representation about
  23. // the suitability of this software for any purpose.
  24. // It is provided "as is" without express or implied
  25. // warranty of any kind, including any implied
  26. // warranty of merchantability, fitness for a
  27. // particular purpose or non-infringement. The entire
  28. // risk as to the quality and performance of the
  29. // software is with you. Bruce Eckel and the
  30. // publisher shall not be liable for any damages
  31. // suffered by you or any third party as a result of
  32. // using or distributing software. In no event will
  33. // Bruce Eckel or the publisher be liable for any
  34. // lost revenue, profit, or data, or for direct,
  35. // indirect, special, consequential, incidental, or
  36. // punitive damages, however caused and regardless of
  37. // the theory of liability, arising out of the use of
  38. // or inability to use software, even if Bruce Eckel
  39. // and the publisher have been advised of the
  40. // possibility of such damages. Should the software
  41. // prove defective, you assume the cost of all
  42. // necessary servicing, repair, or correction. If you
  43. // think you've found an error, please email all
  44. // modified files with clearly commented changes to:
  45. // Bruce@EckelObjects.com. (Please use the same
  46. // address for non-code errors found in the book.)
  47. /////////////////////////////////////////////////
  48.  
  49. // Printing with Java 1.1
  50. import java.awt.*;
  51. import java.awt.event.*;
  52.  
  53. public class PrintDemo extends Frame {
  54.   Button 
  55.     printText = new Button("Print Text"),
  56.     printGraphics = new Button("Print Graphics");
  57.   TextField ringNum = new TextField(3);
  58.   Choice faces = new Choice();
  59.   Graphics g = null;
  60.   Plot plot = new Plot3(); // Try different plots
  61.   Toolkit tk = Toolkit.getDefaultToolkit();
  62.   public PrintDemo() {
  63.     ringNum.setText("3");
  64.     ringNum.addTextListener(new RingL());
  65.     Panel p = new Panel();
  66.     p.setLayout(new FlowLayout());
  67.     printText.addActionListener(new TBL());
  68.     p.add(printText);
  69.     p.add(new Label("Font:"));
  70.     p.add(faces);
  71.     printGraphics.addActionListener(new GBL());
  72.     p.add(printGraphics);
  73.     p.add(new Label("Rings:"));
  74.     p.add(ringNum);
  75.     setLayout(new BorderLayout());
  76.     add(p, BorderLayout.NORTH);
  77.     add(plot, BorderLayout.CENTER);
  78.     String[] fontList = tk.getFontList();
  79.     for(int i = 0; i < fontList.length; i++)
  80.       faces.add(fontList[i]);
  81.     faces.select("Serif");
  82.   }
  83.   class PrintData {
  84.     public PrintJob pj;
  85.     public int pageWidth, pageHeight;
  86.     PrintData(String jobName) {
  87.       pj = getToolkit().getPrintJob(
  88.         PrintDemo.this, jobName, null);
  89.       if(pj != null) {
  90.         pageWidth = pj.getPageDimension().width;
  91.         pageHeight= pj.getPageDimension().height;
  92.         g = pj.getGraphics();
  93.       }
  94.     }
  95.     void end() { pj.end(); }
  96.   }
  97.   class ChangeFont {
  98.     private int stringHeight;
  99.     ChangeFont(String face, int style,int point){
  100.       if(g != null) {
  101.         g.setFont(new Font(face, style, point));
  102.         stringHeight = 
  103.           g.getFontMetrics().getHeight();
  104.       }
  105.     }
  106.     int stringWidth(String s) {
  107.       return g.getFontMetrics().stringWidth(s);
  108.     }
  109.     int stringHeight() { return stringHeight; }
  110.   }
  111.   class TBL implements ActionListener {
  112.     public void actionPerformed(ActionEvent e) {
  113.       PrintData pd = 
  114.         new PrintData("Print Text Test");
  115.       // Null means print job canceled:
  116.       if(pd == null) return;
  117.       String s = "PrintDemo";
  118.       ChangeFont cf = new ChangeFont(
  119.         faces.getSelectedItem(), Font.ITALIC,72);
  120.       g.drawString(s, 
  121.         (pd.pageWidth - cf.stringWidth(s)) / 2,
  122.         (pd.pageHeight - cf.stringHeight()) / 3);
  123.  
  124.       s = "A smaller point size";
  125.       cf = new ChangeFont(
  126.         faces.getSelectedItem(), Font.BOLD, 48);
  127.       g.drawString(s, 
  128.         (pd.pageWidth - cf.stringWidth(s)) / 2,
  129.         (int)((pd.pageHeight - 
  130.            cf.stringHeight())/1.5));
  131.       g.dispose();
  132.       pd.end();
  133.     }
  134.   }
  135.   class GBL implements ActionListener {
  136.     public void actionPerformed(ActionEvent e) {
  137.       PrintData pd = 
  138.         new PrintData("Print Graphics Test");
  139.       if(pd == null) return;
  140.       plot.print(g);
  141.       g.dispose();
  142.       pd.end();
  143.     }
  144.   }
  145.   class RingL implements TextListener {
  146.     public void textValueChanged(TextEvent e) {
  147.       int i = 1;
  148.       try {
  149.         i = Integer.parseInt(ringNum.getText());
  150.       } catch(NumberFormatException ex) {
  151.         i = 1;
  152.       }
  153.       plot.rings = i;
  154.       plot.repaint();
  155.     }
  156.   }
  157.   public static void main(String[] args) {
  158.     Frame pdemo = new PrintDemo();
  159.     pdemo.setTitle("Print Demo");
  160.     pdemo.addWindowListener(
  161.       new WindowAdapter() {
  162.         public void windowClosing(WindowEvent e) {
  163.           System.exit(0);
  164.         }
  165.       });
  166.     pdemo.setSize(500, 500);
  167.     pdemo.setVisible(true);
  168.   }
  169. }
  170.  
  171. class Plot extends Canvas {
  172.   public int rings = 3;
  173. }
  174.  
  175. class Plot1 extends Plot {
  176.   // Default print() calls paint():
  177.   public void paint(Graphics g) {
  178.     int w = getSize().width;
  179.     int h = getSize().height;
  180.     int xc = w / 2;
  181.     int yc = w / 2;
  182.     int x = 0, y = 0;
  183.     for(int i = 0; i < rings; i++) {
  184.       if(x < xc && y < yc) {
  185.         g.drawOval(x, y, w, h);
  186.         x += 10; y += 10;
  187.         w -= 20; h -= 20;
  188.       }
  189.     }
  190.   }
  191.  
  192. class Plot2 extends Plot {
  193.   // To fit the picture to the page, you must
  194.   // know whether you're printing or painting:
  195.   public void paint(Graphics g) {
  196.     int w, h;
  197.     if(g instanceof PrintGraphics) {
  198.       PrintJob pj = 
  199.         ((PrintGraphics)g).getPrintJob();
  200.       w = pj.getPageDimension().width;
  201.       h = pj.getPageDimension().height;
  202.     } 
  203.     else {
  204.       w = getSize().width;
  205.       h = getSize().height;
  206.     }
  207.     int xc = w / 2;
  208.     int yc = w / 2;
  209.     int x = 0, y = 0;
  210.     for(int i = 0; i < rings; i++) {
  211.       if(x < xc && y < yc) {
  212.         g.drawOval(x, y, w, h);
  213.         x += 10; y += 10;
  214.         w -= 20; h -= 20;
  215.       }
  216.     }
  217.   }
  218.  
  219. class Plot3 extends Plot {
  220.   // Somewhat better. Separate 
  221.   // printing from painting:
  222.   public void print(Graphics g) {
  223.     // Assume it's a PrintGraphics object:
  224.     PrintJob pj = 
  225.       ((PrintGraphics)g).getPrintJob();
  226.     int w = pj.getPageDimension().width;
  227.     int h = pj.getPageDimension().height;
  228.     doGraphics(g, w, h);
  229.   }
  230.   public void paint(Graphics g) {
  231.     int w = getSize().width;
  232.     int h = getSize().height;
  233.     doGraphics(g, w, h);
  234.   }
  235.   private void doGraphics(
  236.       Graphics g, int w, int h) {
  237.     int xc = w / 2;
  238.     int yc = w / 2;
  239.     int x = 0, y = 0;
  240.     for(int i = 0; i < rings; i++) {
  241.       if(x < xc && y < yc) {
  242.         g.drawOval(x, y, w, h);
  243.         x += 10; y += 10;
  244.         w -= 20; h -= 20;
  245.       }
  246.     }
  247.   }
  248. } ///:~