home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / javafile / ch03 / DrawApp.java < prev    next >
Encoding:
Java Source  |  1998-12-14  |  7.8 KB  |  295 lines

  1. import java.applet.Applet;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.io.*;
  5. import java.util.StringTokenizer;
  6.  
  7. /**
  8.  * class describing a shape
  9.  */
  10. class Shape {
  11.  
  12. /**
  13.  * constants for the shape type
  14.  */
  15. static final int rectType = 1;
  16. static final int ovalType = 2;
  17. static final int arcType = 3;
  18. static final int polyType = 4;
  19.  
  20. /*
  21.  * the shape type
  22.  */
  23. int type;
  24.  
  25. /*
  26.  * color for this shape
  27.  */
  28. Color color;
  29. static final int MaxPoints = 10;
  30.  
  31. /*
  32.  * arrays of x and y points for this shape
  33.  */
  34. int xp[] = new int[MaxPoints];
  35. int yp[] = new int[MaxPoints];
  36.  
  37. /*
  38.  * the number of points in this shape
  39.  */
  40. int npoints;
  41.  
  42. /**
  43.  * shape constructor
  44.  * saves parameters
  45.  * @param tp - shape type
  46.  * @param n - number of points
  47.  * @param pts[] - array of endpoints
  48.  * @param c - color of the shape
  49.  */
  50. public Shape (int tp, int n, int pts[], Color c) {
  51.  
  52.        int i;
  53.       type = tp;
  54.       color = c;
  55.       npoints = n < MaxPoints ? n : MaxPoints;
  56.       if (type == polyType) {
  57.             npoints >>= 1;
  58.             for (i=0; i<npoints; i+=1) {
  59.                   xp[i] = pts[i << 1];
  60.                   yp[i] = pts[(i << 1) +1];
  61.             }
  62.       } else {
  63.             for (i=0; i<npoints; i+=1)
  64.                   xp[i] = pts[i];
  65.       }
  66. }
  67.  
  68. /**
  69.  * draw the shape
  70.  * @param g - destination graphics object
  71.  */
  72. void paint (Graphics g) {
  73.  
  74.       g.setColor (color);
  75.       switch (type) {
  76.  
  77.       case rectType:
  78.             g.drawRect (xp[0], xp[1], xp[2], xp[3]);
  79.             break;
  80.  
  81.       case ovalType:
  82.             g.drawOval (xp[0], xp[1], xp[2], xp[3]);
  83.             break;
  84.  
  85.       case arcType: 
  86.             g.drawArc (xp[0], xp[1], xp[2], xp[3], xp[4], xp[5]);
  87. break;
  88.  
  89.       case polyType:
  90.             g.drawPolygon (xp, yp, npoints);
  91.             break;
  92.       }
  93. }
  94. }
  95.  
  96. /**
  97.  * application class proper
  98.  */
  99. public class DrawApp extends Panel {
  100.  
  101. /*
  102.  * the maximum number of shapes allowed
  103.  */
  104. static final int MaxShapes = 25;
  105.  
  106. /*
  107.  * nshapes - the number of shapes read in
  108.  * nlines - the line number in the input file
  109.  */
  110. static int nshapes, nlines = 0;
  111.  
  112. /*
  113.  * array of instances of class shape
  114.  */
  115. static Shape shapes[] = new Shape[MaxShapes];
  116.  
  117. /**
  118.  * invoke paint() method for each shape
  119.  * @param g - destination graphics object
  120.  */
  121. public void paint (Graphics g) {
  122.  
  123.       int i;
  124.       for (i=0; i<nshapes; i+=1)
  125.             shapes[i].paint (g);
  126. }
  127.  
  128. /**
  129.  * application entry point
  130.  * @param args - command-line arguments
  131.  */
  132. public static void main (String args[]) {
  133.  
  134.       String buf;
  135.       FileInputStream fs=null;
  136.       int i, type = 0;
  137.  
  138.       if (args.length != 1) {
  139.             System.out.println ("usage: java DrawApp <file>");
  140.             System.exit (1);
  141.       }
  142.  
  143. /*
  144.  * Try to open the file specified by args[0]
  145.  */
  146.       try {
  147.             fs = new FileInputStream (args[0]);
  148.       } catch (Exception e) {
  149.             System.out.println (e);
  150.             System.exit (1);
  151.       }
  152.  
  153. /*
  154.  * Create a DataInputStream BufferedReader 
  155.  * associated with FileInputStream fs
  156. */
  157.       BufferedReader ds = new BufferedReader(new InputStreamReader(fs));
  158. String token;
  159.       Color color = Color.white;
  160.       int pts[] = new int[2 * Shape.MaxPoints];
  161.  
  162. /*
  163.  * loop until end of file or error
  164.  * read a line and parse it
  165.  */
  166.       while (true) {
  167.             try {
  168.                   buf = ds.readLine ();      // read 1 line
  169.                   if (buf == null) break;
  170.             } catch (IOException e) {
  171.                   System.out.println (e);      
  172. break;
  173.             }
  174.             nlines += 1;
  175.             StringTokenizer st = new StringTokenizer (buf); 
  176.             token = st.nextToken ();
  177.             if (token.equals ("white")) {
  178.                   color = Color.white;
  179.                   token = st.nextToken ();
  180.             } else if (token.equals ("lightgray")) {
  181.                   color = Color.lightGray;
  182.                   token = st.nextToken ();
  183.             } else if (token.equals ("gray")) {
  184.                   color = Color.gray;
  185.                   token = st.nextToken ();
  186.             } else if (token.equals ("darkgray")) {
  187.                   color = Color.darkGray;
  188.                   token = st.nextToken ();
  189.             } else if (token.equals ("black")) {
  190.                   color = Color.black;
  191.                   token = st.nextToken ();
  192.             } else if (token.equals ("red")) {
  193.                   color = Color.red;
  194.                   token = st.nextToken ();
  195.             } else if (token.equals ("pink")) {
  196.                   color = Color.pink;
  197.                   token = st.nextToken ();
  198.             } else if (token.equals ("orange")) {
  199.                   color = Color.orange;
  200.                   token = st.nextToken ();
  201.             } else if (token.equals ("yellow")) {
  202.                   color = Color.yellow;
  203.                   token = st.nextToken ();
  204.             } else if (token.equals ("green")) {
  205.                   color = Color.green;
  206.                   token = st.nextToken ();
  207.             } else if (token.equals ("magenta")) {
  208.                   color = Color.magenta;
  209.                   token = st.nextToken ();
  210.             } else if (token.equals ("cyan")) {
  211.                   color = Color.cyan;
  212.                   token = st.nextToken ();
  213.             } else if (token.equals ("blue")) {
  214.                   color = Color.blue;
  215.                   token = st.nextToken ();
  216.             } else {
  217.                   System.out.println ("Unknown color: "+token);
  218.                   System.out.println ("line "+nlines);
  219.                   System.exit (1);
  220.             }
  221.  
  222.             int npoints = 0;
  223.             if (token.equals ("rect")) {
  224.                   npoints = getInt (st, pts, 4); 
  225.                   type = Shape.rectType;
  226.             } else if (token.equals ("oval")) {
  227.                   npoints = getInt (st, pts, 4);
  228.                   type = Shape.ovalType;
  229.             } else if (token.equals ("arc")) {
  230.                   npoints = getInt (st, pts, 6);
  231.                   type = Shape.arcType;
  232.             } else if (token.equals ("poly")) {
  233.                   npoints = getInt (st, pts, Shape.MaxPoints);
  234.                   type = Shape.polyType;
  235.             } else {
  236.                   System.out.println ("Unknown shape: "+token);
  237.                   System.out.println ("line "+nlines);
  238.                   System.exit (1);
  239.             }
  240.             shapes[nshapes++] = new Shape (type, npoints, pts, color);
  241. }
  242. /*
  243.  * close can throw an exception also; catch it for completeness
  244.  */
  245.       try {
  246.             fs.close ();
  247.       } catch (IOException e) {
  248.             System.out.println (e);
  249.       }
  250.  
  251.       Frame f = new Frame ("Drawing shapes");
  252.       DrawApp drawApp = new DrawApp ();
  253.  
  254.       f.setSize(410, 430);
  255.       f.addWindowListener(new WindowCloser());
  256.       f.add ("Center", drawApp);
  257.       f.show ();
  258. }
  259.  
  260. /**
  261.  * parse points
  262.  * @param st - StringTokenizer for current line
  263.  * @param pts[] - array of points to be returned
  264.  * @param nmax - maximum number of points to accept
  265.  */
  266. static int getInt (StringTokenizer st, int pts[], int nmax) {
  267.  
  268.       int i;
  269.       String token;
  270.  
  271.       for (i=0; i<nmax; i+=1) {
  272.             if (st.hasMoreTokens () == false) break;
  273.             token = st.nextToken ();
  274.             try {
  275.                   pts[i] = Integer.valueOf (token).intValue ();
  276.             } catch (NumberFormatException e) {
  277.                   System.out.println (e);
  278.                   System.out.println ("line "+nlines);
  279.                   System.exit (1);
  280.             }
  281.       }
  282.       return i;
  283. }
  284. }
  285. class WindowCloser extends WindowAdapter
  286. {
  287.     public void windowClosing(WindowEvent e)
  288.     {
  289.         Window win = e.getWindow();
  290.         win.setVisible(false);
  291.         win.dispose();
  292.         System.exit(0);
  293.     }//windowClosing
  294. }//class WindowCloser
  295.