Lab 10: Java Graphics

Getting Started

  1. Create a lab10 subdirectory in your cps001 directory. Copy the files MyDrawing.java, MyDrawing.html, and awb.jar to your ~/cps001/lab10 directory. The files are located here:

    /afs/acpub.duke.edu/project/web/cps001/code/
  2. Open MyDrawing.java in xemacs and compile - without making any changes to the code just yet!

  3. View the MyDrawing applet using appletviewer. All you will see is a blank white canvas and two IntFields for entering x and y coordinates. These coordinates specify the position of your drawing on the canvas.

To Do

The following items must be implemented to get credit for lab completion.

  1. Initialize and add the two Buttons bDraw and bClear. The Buttons should be labeled "Draw" and "Clear", respectively, and perform the following actions:

  2. The Draw subroutine is declared as follows:

    public void Draw (int x, int y)

    The subroutine takes as input two ints which represent the x and y coordinates of the drawing position on the canvas.

    You may draw whatever you want, as long as it "makes sense". For example, you can draw something simple such as the flower in the example below or create a pattern of geometric shapes, but you shouldn't just put a jumble of random shapes on the canvas. You should use several different shapes (such as lines, ovals, rectangles, etc.) and colors. Make sure that when you design your drawing you use the generic x and y coordinates passed into the Draw subroutine. The Java Graphics API contains method specifications for drawing shapes. It may be easier to add shapes one at a time, compiling and testing your applet in-between, or to sketch your shapes and coordinates on paper first.

    REMEMBER: The coordinate system for Java graphics starts at the upper left corner, and goes to the right and down!

Example

Below is an example of a possible implementation of the MyDrawing applet. When the Draw button is clicked the picture is drawn at the default coordinates (0,0), the canvas appears as follows:

If the coordinates (100,100) are entered and the Draw button is clicked, a second copy of the picture is drawn:



Extra Credit

Extra credit will be awarded for exceeding the minimum requirements - for example, creating a particularly elaborate and/or intricate drawing, or implemeting one of the suggestions below.


NOTE: If you decide to do extra credit work, add a short description of what you did in the space indicated (by "Extra work:") in the MyDrawing.html file.

The example below shows what happens when you click the "Tile" Button:


You are encouraged to think up and implement your own ideas as well.

Submitting

submit_cps001 lab10/sec# MyDrawing.java MyDrawing.html
Did you put your name on your files???



Tammy Bailey
Last modified: Tue Mar 25 16:21:39 2003