Working with Graphics

Objectives

Description

For this assignment you will be working with a Graphics object to do some drawing in an application window. You are given a working application, and you are asked to make some modifications and extensions to the source code.

You may be able to figure out how to use a Graphics object well enough to complete the homework by studying and experimenting with the source code you are given to start with, but you will probably want to do some reading about Graphics objects. Section 2.3 of our textbook is a good starting place. However, it does not give all the details of Graphics objects, e.g. it does not explain the drawArc method. The definitive source of documentation is online at http://download.oracle.com/javase/6/docs/api/. In the list of "All Classes" in the lower left hand pane, scroll down and click the link for the Graphics class. Scroll down in the main pane to the Method Summary for a brief description of everything you can do with a Graphics object. Follow the links to get full details about each method.

Exercises

  1. Create a Java project in Eclipse and add a new class called "Smile" in the "cis180" package..
  2. Paste the given source code into the Smile.java file, replacing any source code generated by Eclipse.
  3. Compile and run the application. It should draw a "sad face" in a window on the screen. Notice what happens when you resize the window.
  4. For each of the following editing exercises below, you should start out with the original version of the Java code. After each editing task, you should attempt to run the program and write down any changes you observe, or, if there are errors, the first error message you receive.

    After each step (a - i), restore the code to its original form. Note that you can "remove" a line by placing comment characters "//" at the beginning, and restore the line by deleting the comment characters.

    1. Remove the line that reads:
      import java.awt.*;
    2. Remove the word "public" from the line that reads:
      public class Smile extends Component
    3. Remove the text "extends Component" from the line that reads:
      public class Smile extends Component
    4. Change the name "Component" to "component" in the line that reads:
      public class Smile extends Component
    5. Remove the line that reads:
      private Font titleFont = new Font("SansSerif", Font.PLAIN,18);
    6. Remove the phrase "Color." from the line that reads:
      g.setColor(Color.blue);
    7. Remove the phrase "g." from the line that reads:
      g.setColor(Color.blue);
    8. Remove the "}" at the very end of the file.
    9. Change the word "public" to "private" in the line that reads:
      public void paint( Graphics g )
  5. Change the Java code so that it displays the string "Hi!" in place of the string "Make me smile!"
  6. Change the Java code so that the string "Hi" is displayed in white type on a solid green rectangle.
  7. Add pupils to the eyes. Make sure the face appears to be looking straight ahead.
  8. Change the Java code so that the application draws a "Happy Face" instead of a "Sad Face". You may have to experiment a bit to get this right.

When you are finished

  1. Turn in your answers to exercise 4, i.e. the changes and/or errors you observed in each step.
  2. Demonstrate the final version of the program after making all the modifications in exercises 5 - 8.