home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-12-14 | 7.8 KB | 295 lines |
- import java.applet.Applet;
- import java.awt.*;
- import java.awt.event.*;
- import java.io.*;
- import java.util.StringTokenizer;
-
- /**
- * class describing a shape
- */
- class Shape {
-
- /**
- * constants for the shape type
- */
- static final int rectType = 1;
- static final int ovalType = 2;
- static final int arcType = 3;
- static final int polyType = 4;
-
- /*
- * the shape type
- */
- int type;
-
- /*
- * color for this shape
- */
- Color color;
- static final int MaxPoints = 10;
-
- /*
- * arrays of x and y points for this shape
- */
- int xp[] = new int[MaxPoints];
- int yp[] = new int[MaxPoints];
-
- /*
- * the number of points in this shape
- */
- int npoints;
-
- /**
- * shape constructor
- * saves parameters
- * @param tp - shape type
- * @param n - number of points
- * @param pts[] - array of endpoints
- * @param c - color of the shape
- */
- public Shape (int tp, int n, int pts[], Color c) {
-
- int i;
- type = tp;
- color = c;
- npoints = n < MaxPoints ? n : MaxPoints;
- if (type == polyType) {
- npoints >>= 1;
- for (i=0; i<npoints; i+=1) {
- xp[i] = pts[i << 1];
- yp[i] = pts[(i << 1) +1];
- }
- } else {
- for (i=0; i<npoints; i+=1)
- xp[i] = pts[i];
- }
- }
-
- /**
- * draw the shape
- * @param g - destination graphics object
- */
- void paint (Graphics g) {
-
- g.setColor (color);
- switch (type) {
-
- case rectType:
- g.drawRect (xp[0], xp[1], xp[2], xp[3]);
- break;
-
- case ovalType:
- g.drawOval (xp[0], xp[1], xp[2], xp[3]);
- break;
-
- case arcType:
- g.drawArc (xp[0], xp[1], xp[2], xp[3], xp[4], xp[5]);
- break;
-
- case polyType:
- g.drawPolygon (xp, yp, npoints);
- break;
- }
- }
- }
-
- /**
- * application class proper
- */
- public class DrawApp extends Panel {
-
- /*
- * the maximum number of shapes allowed
- */
- static final int MaxShapes = 25;
-
- /*
- * nshapes - the number of shapes read in
- * nlines - the line number in the input file
- */
- static int nshapes, nlines = 0;
-
- /*
- * array of instances of class shape
- */
- static Shape shapes[] = new Shape[MaxShapes];
-
- /**
- * invoke paint() method for each shape
- * @param g - destination graphics object
- */
- public void paint (Graphics g) {
-
- int i;
- for (i=0; i<nshapes; i+=1)
- shapes[i].paint (g);
- }
-
- /**
- * application entry point
- * @param args - command-line arguments
- */
- public static void main (String args[]) {
-
- String buf;
- FileInputStream fs=null;
- int i, type = 0;
-
- if (args.length != 1) {
- System.out.println ("usage: java DrawApp <file>");
- System.exit (1);
- }
-
- /*
- * Try to open the file specified by args[0]
- */
- try {
- fs = new FileInputStream (args[0]);
- } catch (Exception e) {
- System.out.println (e);
- System.exit (1);
- }
-
- /*
- * Create a DataInputStream BufferedReader
- * associated with FileInputStream fs
- */
- BufferedReader ds = new BufferedReader(new InputStreamReader(fs));
- String token;
- Color color = Color.white;
- int pts[] = new int[2 * Shape.MaxPoints];
-
- /*
- * loop until end of file or error
- * read a line and parse it
- */
- while (true) {
- try {
- buf = ds.readLine (); // read 1 line
- if (buf == null) break;
- } catch (IOException e) {
- System.out.println (e);
- break;
- }
- nlines += 1;
- StringTokenizer st = new StringTokenizer (buf);
- token = st.nextToken ();
- if (token.equals ("white")) {
- color = Color.white;
- token = st.nextToken ();
- } else if (token.equals ("lightgray")) {
- color = Color.lightGray;
- token = st.nextToken ();
- } else if (token.equals ("gray")) {
- color = Color.gray;
- token = st.nextToken ();
- } else if (token.equals ("darkgray")) {
- color = Color.darkGray;
- token = st.nextToken ();
- } else if (token.equals ("black")) {
- color = Color.black;
- token = st.nextToken ();
- } else if (token.equals ("red")) {
- color = Color.red;
- token = st.nextToken ();
- } else if (token.equals ("pink")) {
- color = Color.pink;
- token = st.nextToken ();
- } else if (token.equals ("orange")) {
- color = Color.orange;
- token = st.nextToken ();
- } else if (token.equals ("yellow")) {
- color = Color.yellow;
- token = st.nextToken ();
- } else if (token.equals ("green")) {
- color = Color.green;
- token = st.nextToken ();
- } else if (token.equals ("magenta")) {
- color = Color.magenta;
- token = st.nextToken ();
- } else if (token.equals ("cyan")) {
- color = Color.cyan;
- token = st.nextToken ();
- } else if (token.equals ("blue")) {
- color = Color.blue;
- token = st.nextToken ();
- } else {
- System.out.println ("Unknown color: "+token);
- System.out.println ("line "+nlines);
- System.exit (1);
- }
-
- int npoints = 0;
- if (token.equals ("rect")) {
- npoints = getInt (st, pts, 4);
- type = Shape.rectType;
- } else if (token.equals ("oval")) {
- npoints = getInt (st, pts, 4);
- type = Shape.ovalType;
- } else if (token.equals ("arc")) {
- npoints = getInt (st, pts, 6);
- type = Shape.arcType;
- } else if (token.equals ("poly")) {
- npoints = getInt (st, pts, Shape.MaxPoints);
- type = Shape.polyType;
- } else {
- System.out.println ("Unknown shape: "+token);
- System.out.println ("line "+nlines);
- System.exit (1);
- }
- shapes[nshapes++] = new Shape (type, npoints, pts, color);
- }
- /*
- * close can throw an exception also; catch it for completeness
- */
- try {
- fs.close ();
- } catch (IOException e) {
- System.out.println (e);
- }
-
- Frame f = new Frame ("Drawing shapes");
- DrawApp drawApp = new DrawApp ();
-
- f.setSize(410, 430);
- f.addWindowListener(new WindowCloser());
- f.add ("Center", drawApp);
- f.show ();
- }
-
- /**
- * parse points
- * @param st - StringTokenizer for current line
- * @param pts[] - array of points to be returned
- * @param nmax - maximum number of points to accept
- */
- static int getInt (StringTokenizer st, int pts[], int nmax) {
-
- int i;
- String token;
-
- for (i=0; i<nmax; i+=1) {
- if (st.hasMoreTokens () == false) break;
- token = st.nextToken ();
- try {
- pts[i] = Integer.valueOf (token).intValue ();
- } catch (NumberFormatException e) {
- System.out.println (e);
- System.out.println ("line "+nlines);
- System.exit (1);
- }
- }
- return i;
- }
- }
- class WindowCloser extends WindowAdapter
- {
- public void windowClosing(WindowEvent e)
- {
- Window win = e.getWindow();
- win.setVisible(false);
- win.dispose();
- System.exit(0);
- }//windowClosing
- }//class WindowCloser
-