CSCI 201 Lab 8 -- Using Classes

Getting ready

If a directory called c:\files\Lab08 presently exists on your computer, delete it.

Download a self-extracting archive of the Lab 8 project and store it in the C:\files directory. In Windows Explorer, double-click on the file and extract the project folder to the c:\files directory. This should create the new directory: C:\files\Lab08. Check to make sure that this directory exist before continuing.

Using the Triangle class

Start up JCreator by double-clicking on the ".jcw" file (i.e., Lab08.jcw). Notice that the project contains two .java files: Lab08.java and Triangle.java. Notice also that Lab08.java is an applet and there is an accompanying .html file, Lab08.html. Your first job is to draw triangles in the applet using the Triangle class.

Start by looking at Triangle.java and reading its documentation (i.e., the comments). Next, take a look at the applet, Lab08.java. Note that both the applet (Lab08.java) and the Triangle class are part of the same package; this means that you can use the Triangle class in the applet without importing it (you will see more on this in Chapter 5). Also note that the applet, in its incomplete state, contains one instance variable, a reference variable named "triangle." You will want to use that variable when you create the first Triangle object.

Unlike our previous applets, this applet also contains two methods: init() and paint(). The init() method is called when the applet is first loaded in the browser; it can be used to create the objects used in the display. The paint() method, as we know from previous labs, is used to "paint" the applet display. As can be seen on page 211 of your text, there are many other methods that can be used to perform specific tasks in an applet.

Now that you are familiar with the setup, create three Triangle objects in your applet and draw them to produce a display similar to that shown below. As shown below, the triangles should be arranged to look like the top of a pine tree.


Lab instructor check-off 1

Show your instructor the output of your program. Your display should contain 3 triangles positioned to form a tree top.


Adding a New Class

Now that you are good at drawing the top of a tree, try drawing a complete tree by adding a tree trunk using the fillRect() method of the Graphics object in the applet's Paint() method. (The fillRect() method is described on page 96 of your text). Be sure to make the tree trunk grey as shown below.

Now that you can make a tree, why not make a Tree class so that it would be easy to create a whole forest of trees. Add a new class to your project and call it Tree. Make the Tree class "public", and include it in the same package as the other classes in this project (i.e., the package "edu.unca.cs.csci201.triangle"). You will also need to import the java.awt package which contains the Graphics class (i.e., you will need the line, "import java.awt.*").

Now, copy the contents of the Lab08 class into your Tree class and change the names of the 2 methods. Make the init() method be the Tree class constructor, and change the name of paint() to draw(). Just making these simple changes should create a Tree class that can be used to draw a single tree. (You can create more that one Tree object, but each tree will look the same and be in the same location). Try it---modify the Lab08 applet to create a Tree object and then draw it!


Lab instructor check-off 2

Show your instructor your display of the Tree object.


Enhancing the Tree Class

What you would really like to be able to do is draw multiple Tree objects and allow each object to be positioned at a different location and have a different color (other improvements could also be considered). In order to do that, we have to introduce additional state variables (i.e., instance variables) to the Tree class allowing us to give a unique position (i.e., x, y coordinate), and color to each Tree object. These variables must then be used to position the triangles and rectangle that form the tree, as well as color the triangles.

Make the changes described above to the Tree class, and then draw three trees similar to those shown below.


Lab instructor check-off 3

Show your instructor your display of 3 trees.