CSC 121A Lab 2 - Spring 2005

Thursday, January 13, 2005
Due: Wednesday, January 19, 2005, 5:00PM

[Course home page] [Java API Documentation]

Answer all bulleted questions. Upload your answers and program files to the VikingWeb coursework section. Besides uploading your written answers to the VikingWeb, turn in a printed copy either in class on the due date or leave a copy in the box outside my door later on during the day. (You may turn in the printed copies later than 5PM on the due date, but the files must be uploaded to VikingWeb at or before 5PM.)

1. Graphics: Simple Drawings

One of the appealing features of Java is the ease with which it supports graphical programming. In this lab we will start with drawing lines and will learn the basics of creating a window to display drawings on the screen.

Coordinates: To begin drawing, you should understand the Java coordinate system for identifying points on the screen. By default, the upper-left corner of a window has the coordinates (0, 0). A pair of coordinates, (x, y), specifies the horizontal coordinate (x) and the vertical coordinate (y). From the upper-left corner (0, 0), the coordinates increase positively to the right and bottom of the window. Coordinate units are measured in pixels where a pixel is the monitor's smallest unit of resolution.

For a first drawing application, let us simply draw two lines from the corners. Download and compile the following class files: [DrawPanel.java] [DrawPanelTest.java]

The class DrawPanel does the actual drawing while the DrawPanelTest class creates the window in which the drawing will be displayed. DrawPanel imports the Java Graphics class which provides methods for drawing text and shapes, and the JPanel class which provides an area on which to draw.

The DrawPanel class extends JPanel in order to define an enhanced type of panel. Every JPanel has a paintComponent method which the Java system uses whenever it needs to display the panel. The paintComponent method in DrawPanel overrides the method in JPanel. The first statement in such an overridden method should usually be a call to the superclass' version of the method:

super.paintComponent( g );

If there are further methods used in the program which you are not sure what they do, ask me or look them up in the online Java API documentation (link at the top of this page).

2. Rectangles and Ovals

Download and compile the following class files. Run the ShapesTest program: [Shapes.java] [ShapesTest.java]

3. Colors and Filled Shapes

To create more interesting designs than just lines and basic shapes, the Graphics class provides features for drawing colors and filled shapes.

Colors: Colors displayed on the computer screen are specified by their red, green, and blue components, called RGB values. The RGB values have integer values between 0 and 255. The higher the value of a particular component, the brighter the particular shade will be in the final color. Java uses the Color class in the java.awt package to represent RGB-valued colors. The Color class contains a constructor of the form:

public Color( int r, int g, int b )
to create colors by specifying values for the red, green, and blue components.

Filled shapes: Filled rectangles and ovals are drawn using the fillRect and fillOval methods of the Graphics class. These methods have the same parameters as their counterparts drawRect and drawOval. Download and run the following program, which draws a smiley face in a window: [DrawSmiley.java]

4. Homework Exercises

Based on material from Deitel & Deitel, Java How to Program, 6e.

 



Last modified: Thu Jan 13 08:46:52 EST 2005