Program 2: ShapeDrawer

  • Assigned On: 10/24/2005
  • Due On: 11/02/2005
  • Description:

    Write a java applet that reads a text file containing shape information and draws the appropriate shapes to the screen. There are four kinds of objects in the shape file; solid rectangles, solid ovals, solid arcs and lines. Each line in the input file describes a single shape.

    • A rectangle is described by five parameters: the x coordinate of the upper left corner, the y coordinate of the upper left corner, its width and height, and its color.
      rectangle 10 20 40 50 red
    • An oval is described by five parameters. The first four parameters describe a bounding box for the oval. The box itself has an x and y coordinate for the upper left corner, a width and height. The color is the final parameter.
       oval 10 20 40 50 green 
    • An arc is described by seven parameters. An arc is a portion of an oval. The first two parameters are the x and y coordinates of the oval's center. The next two parameters are the height and width of the box enclosing the oval. The next two parameters, describe which portion of the arc to draw: the first, startAngle, is the angle to start drawing from, the second, arcAngle, is the angular width of the arc. The final parameter is the color.
      arc 100 100 50 50 0 90 orange
    • A line is described by five parameters. Its starting x and y coordinates, its ending x and y coordinates, and its color.
      line 20 50 100 150 blue

    A square is a special case of a rectangle where the width and height are equal. A circle is a special case of an oval where the width and height of the bounding box are equal.

    Each line of the input file begins with the kind of shape it describes and then contains the parameter list for that shape. All numerical parameters will be integers.

    Your program should support 13 colors: black, blue, cyan, darkGray, gray, green, lightGray, magenta, orange, pink, red, white, and yellow. These colors are static members of the class Color (see page 1008).

    Here are some example input files:

    Here are the exceptional student-submitted drawings.

    Because you will be reading from a file, you will have to handle FileNotFoundExceptions. Working within the confines of the applet, you're restricted in how you may declare exception throwing. (That is you may not redefine the signature for "public void paint( Graphics g)" to also "throw FileNotFoundException"s). I am providing the following code block that you may use.
       public void paint( Graphics g )
             readInputFile( g );
          catch (FileNotFoundException fnf )
       public void readInputFile( Graphics g ) throws FileNotFoundException
          //The rest of this method and any other methods you wish to define
          //are left to you

    Because reading from files is tricky with Applets, you will run your program locally (on your computer) instead of imbedding it within a webpage. If you try to imbed your program in a webpage, you will encounter a security exception. There are ways to get an applet to read from a file located at some URL, but they are involved.

    Type the honor pledge at the top of your .java file in a comment block.

    Zip 1) your .java file and 2) your .class file 3) a screen shot of an original drawing and 4) the text file holding your original drawing into a single file. Name that file <your onion> That's the name it should have on your computer. Use blackboard to SEND your .zip file.