Lab 2: Classes with Graphics
Due: Thursday, Feb. 4, 11:59pm
This lab will give you practice with four things:
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.
Starting Point
Because this 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 goals above. Therefore we are starting you off with a working program that prepares the window environment before displaying a series of objects: CircleApp.java. 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. JCircle 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 for 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.
TODO
These steps correspond to the TODO list in CircleApp.java.
Screenshot
You should take a screenshot (named screenshot.png) of your assembled set of windows once you are finished. Here is an example:
To turn in
Please submit the following files as lab2:
If you have more time, you may begin work on Homework 2.