Course Links

Exercises

Resources

External

This lab will give you practice on three things:

  1. Writing simple classes in Java, with appropriate constructors, accessors, and manipulators
  2. Classes with inheritance
  3. Basics of window graphics

This will help to prepare you for the next homework assignment, which is also graphics-based and requires you to write several classes from scratch.

If you plan to develop and run code on aurora over an ssh connection, you must set up and run an X Window server app on your laptop in order to run graphics programs.

Starting Point

Because the lab uses classes that display themselves, you will get immediate visual feedback about how your code is functioning. However, the detailed setup for a graphical program is somewhat complex and not particularly relevant to the three goals above. Therefore we are starting you off with a working program that prepares the window environment before displaying a series of objects. You will not have to modify this program at all, except to add commands to display the additional objects that you will create. For this lab, it is not necessary to understand exactly what is happening in the prewritten code. We will cover this material at a later time.

You will also be provided with two example classes, JCircle and JColorCircle. Both are subclasses of JComponent, which makes them suitable for display in a window environment. But in terms of the fields and methods relating to the shape itself, JCircle looks more like a class written from scratch. It has a single field diameter that describes the size of the circle. It also has a constructor and a pair of accessors and manipulators. These are set up in terms of radius rather than diameter -- to show you how using the accessor/manipulator paradigm combined with private fields allows you to separate the public interface of your class (everything expressed in terms of radius) with the internal implementation (expressed in terms of diameter). There are times when this can be extremely useful.

Here are the three starter files:

Graphics in Java

One of the AWT classes that we will need to work closely with is called Graphics. This is a software representation of a drawable region (i.e., the interior of a window). By calling methods of class Graphics, you can cause things to appear in the window. For example, you can draw lines, ovals, rectangles, text, etc. The area within the window is conceptualized as a 2D plane with origin at the upper left corner and an inverted y axis (so down is positive). The program draws in the window sequentially, so the most recently drawn elements may cover up previously drawn ones. This will generally happen instantaneously, so that you will only see the final result.

Like JComponent, class Graphics is part of the package of Java classes called the Abstract Window Toolkit (AWT). You can look at the official Javadoc page for class Graphics to get an idea of what sorts of things you can do with it. We'll be learning more about other classes in the AWT soon.

To Do

Begin by compiling and running the sample code to see what it does.

> javac *.java
> java ManyWindows

Now try modifying it a bit for variety.

  1. Modify ManyWindows.java to add another JCircle window and two more JColorCircle windows, one with a predefined color and one with a color that you create. (See the Color javadoc for predefined constant colors and constructors for making your own from an RGB triplet.) Save references to all the objects you create
  2. Use the setRadius method to change the size of circle2. Does it make a difference whether you do this before or after adding it to the window? How come?
  3. Use the setColor method to change the color of one of the colored circles. Does it make a difference whether you do this before or after adding it to the window? How come?
  4. Create your own new class called JBorderCircle as a subclass of JColorCircle. This will be a colored circle with a border of a different color around the outside. You will have to give it a new field (call it borderColor) to store the color of the border, and then create an appropriate constructor, accessor, and manipulator. Then you should override the paintComponent method to add the border using the drawOval method provided by class Graphics. (Does the order matter here?)
  5. Modify ManyWindows.java to add at least one JBorderCircle window.
  6. If you have time, create a JFace class that inherits from one of the other classes in the lab. Explore the drawing functions available to you and figure out how to make it draw a face within the circle. The face should adjust with the circle dimensions, and be further controlled by one or more new fields that you add. (For example, eye color, eyebrow angle, degree of smile/frown, nose size or shape, number of freckles, etc.) Modify ManyWindows.java to add several face windows with different settings to show the variety. Try one example where you create the face object and then modify it in every way possible via the manipulators.

Screenshot

You should take a screenshot of your assembled set of windows once you are all finished. There are instructions on how to do this for various operating systems. Assuming you do this on your laptop, you will then need to transfer it to aurora before submitting.

To turn in

Please submit the following files as lab2:

Moving on to Assignment 2

If you have more time, you may begin work on Assignment 2. Note that the situation there is a little more complex than in this lab. Here we had a single class (say, JCircle) that was responsible both for keeping track of the circle's data (i.e., its diameter) and for drawing it in the window. In the homework assignment, as in most larger graphics programs, the class that keeps track of the data (MapGrid) is separate from the class that draws it (MapViewer). They work together, but each has a more specialized role. For the curious, they form two of the three parts of the model-view-controller software paradigm.