CSCI 201 Lab 6 -- Making Faces with Java

In this lab, you'll write a java applet that draws a face.

Getting ready

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

Let's start by reviewing some of the things that we learned about applets in lab 04 (the Stonehedge lab):

  1. An Applet must be embedded in an HTML document, and that HTML document must be located in the project folder.
  2. Applets do not have a main() method, their execution is controlled by the web browser.
  3. All applets are created by extending the JApplet class. Therefore, the first line of an applet class will be (where < class_name > is replace by the name you give your applet):
    
      public class < class_name > extends JApplet {
    
  4. When we write an applet, we need to define a method called paint(); the paint() method is responsible for creating the applet display.
  5. The paint() method always has a single parameter (the value in ()'s below) that is a Graphics object.
    
    	public void paint(Graphics g) {
    
    
    We can give the parameter any name we choose, in the example above it is named "g". We use methods that belong to this Graphics object to draw things in our applet display.

If any of these concepts are not clear to you look back at lab 04 and/or look at section 2.9 (page 92) in your text.

Starting Your Applet

We're going to give you a project containing an applet to start this lab. The project is stored as a self-extracting archive file. First, download the archive file by right-clicking on this link, and storing the target file in the C:\files directory. Now, using WindowExplorer, extract the Jcreator project folder by double-clicking on the lab06.exe icon in the C:\files directory. Select OK and then Unzip, respectively, from the subsequent pop-up menus.

Start up JCreator by opening the Face folder and then double-clicking on the ".jcw" file (i.e., Face.jcw). Once the project is open in JCreator, you should be able to execute the program by selecting the Execute Project option under the Build Menu of JCreator's toolbar. Run the program and look at the display created by the applet. You should see a rather abstract face with only a nose and mouth.

Enhancing Your Face

You need to modify the paint() method of the applet you were given to produce an improved face (ahhh, but if all of live were so easy). At the minimum you must add hair and eyes. On page 96 of your text, there are examples of Graphics methods (i.e., methods belonging to the Graphics class) that you can use. A more complete list of Graphics methods can be found on the java web site describing the Graphics class.

Understanding the Documentation

If you look at java web site referenced above and follow a hyper link to the detailed description of a particular method, for example the fillArc() method, the method description begins with the method header (also called the method prototype):
   public abstract void fillArc(int x,
                                int y,
                                int width,
                                int height,
                                int startAngle,
                                int arcAngle)

In your text, this same method header is written as follows:
   void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle)

Clearly, the authors omitted some of the (non-essential) information to make it easier to read.

The method header is the first line you write when you implement a method. For example, here is the header of the method main():

   public static void main(String [] args)

In order to be able to use methods based on their documentation, you need to understand the information contained in the header. Follow the hyper-links on the example below to obtain a better understanding of the information included in a method header.

   public abstract void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle)

Finally, in order to use the Graphics methods, you need to remember how the display coordinate system is setup. The origin of the coordinate system is the top-left corner of the display, with the positive x-axis running horizontally to the right, and the positive y-axis running vertically down the right side of the display. Also, distances are measured in terms of pixels. For more information, see page 41 of your text.

Lab Checkoff

This lab has more indefinite goals than the previous ones. However, it has two definite rules:

  1. You must work with a partner.
  2. You can not leave for at least one hour.

Your job is to improve the face. To provide some inspiration, here are some of the faces that students have created in the past.