Lab 6 Prelab
CPS 1 - Spring 1999 - Duke University



Graphics
In lab 6, you will be using the DrawingPad class to design several Fish classes for a Java fish tank. The DrawingPad class allows you to use several drawing tools to create graphics with Java. To the right is a 50 by 50 grid that has two shapes drawn on it using the DrawingPad class. The Java code to produce this on the DrawingPad named dp is:
dp.setcolor(color.yellow);
dp.fillOval(0,0,20,20);

dp.setcolor(color.red);
dp.fillTriangle(20,30,40,30,40,50);
In the first block of code, the color was set to yellow, and an oval was created starting at point (0,0) with a width of 20 and a height of 20. The first two parameters specify the starting point, the third parameter specifies the width, and the fourth parameter specifies the height.

In the second block of code, the color was set to red, and triangle was created with verticies at (20,30), (40,30), and (40,50). The first two parameters specify the x and y of the first vertex, the next two specify the second vertex, and the final two specify the third vertex.

The lab 6 reference sheet, which can be found at http://www.duke.edu/~pbm3/cps1/lab6ref.html, contains full documentation for the DrawingPad class.


Inheritance
There are times in programming when you want to extend something you have already written, but still keep the original. Keeping with the theme of drawing, you might want to have a class that draws a fish, and a class that draws a fish with a smile. You could have rewrite all the Java commands that draw the fish in the class that draws the fish with a smile, or you could simply inherit the code that draws the fish, then add to that the Java commands that draw the smile on the fish. The class that inherits the information is called the subclass, while the class the subclass inherits from is the superclass. To show this graphically:

Exercise
Print out the Prelab Graph Page, or use your own graph paper to draw two different geometric shapes available in the DrawingPad class and write the corresponding Java code to create them. One of the shapes has to be something other than the triangle and the oval that appear on this sheet. You will need to consult the lab 6 reference sheet to read how to create one of the other shapes (ie. rectangle, line). You do not need to be concerned with colors for this excercise.