$Revision: 1.3 $
In this lab you will use the Java API documentation to learn how to make method calls on a Graphics object in order to draw geometric shapes in a window.
In Java, to draw lines and shapes in a window, you use the a java.awt.Graphics object. Recall that Java class names can have two parts, the package name and the class name. In this case, the package name is java.awt and the class name is Graphics. The methods defined in the Graphics class allow you to draw various lines and shapes, and to set the "pen color", which defines the color that the shapes and lines will appear in.
Note that the coordinates in most computer graphics systems (including Java) are arranged like this:
The origin is in the upper left corner. X coordinates increase towards the right of the window, and y coordinates increase towards the bottom of the window.
Open a web browser, go to the course home page, and find the Java API Documentation link. You should see three browser frames. In the upper left frame, click on the java.awt package. Then, in the lower left frame, click on the Graphics class. In the main frame to the right, you should see information about the java.awt.Graphics class.
Go to the class web page, http://www.cs.vassar.edu/~cs101/hovemeyer/. From the list of labs at the bottom of the page, download the files Picture.java and PictureWindow.java. Save them in the Home directory. Start DrJava and load these files.
You can test the PictureWindow program by running the command
in the DrJava interactions window. At first, the window will be blank.
Your task is to use methods defined in the java.awt.Graphics class to draw a picture in a window. You can draw any picture you would like. For example, you could draw a snowman:
You will draw your picture by adding statements to the paint method in the Drawing class. These statements should use the Graphics object passed as a parameter to the method in order to call methods on it. For example, you could draw a filled rectangle using the fillRect method:
graphics.fillRect(10, 30, 55, 30);
This call draws a filled rectangle whose upper left corner is at the point (10, 30), whose width is 55, and whose height is 30.
The requirements for your picture are these:
The window you will draw in is approximately 600x400. You may want to sketch your picture before adding code to the paint method.
The methods whose names start with draw and fill are the ones that draw shapes.
You can change the pen color (the color that shapes and lines are drawn using) by calling the setColor method and passing one of the color constants defined in the java.awt.Color class. For example:
will set the pen color to blue. Check the API documentation for java.awt.Color for more predefined colors.
Show your picture to a lab coach or instructor.
To submit your lab, go to the CS 101 Submission Website, https://mr42.cs.vassar.edu/dropbox/cs101/upload.html, and submit the file Picture.java.