E-Mail Help Policies Solutions Study-Aids Syllabus Tools
Lab: Gaining Experience with Graphics


1 Instructions: Answer as many of the following questions as you can during the lab period.

You must submit your answers using Blackboard by attaching the source (i.e., .java) files. Note: Enter "Lab" in the "Comments Area".

If you are unable to complete the assignment during the lab period it is strongly recommended that you complete it on your own.

2 Getting Ready: Before going any further you should:
  1. Make a directory for this lab.
  2. Setup your development environment.
  3. Download the following files:

    usa48.txt
    Painting.class
    MapReader.java
    Map.java
    MapDriver.java

    to your working directory.

Note that the file usa48.txt contains a simplified map of the 48 coterminous states in the United States. Each state is represented as a single polygon -- islands, lakes, etc... have been omitted for simplicity.

3 Drawing Geometric Shapes: This part of the lab will help you get some experience with drawing geometric shapes.
  1. Edit the Map class.
  2. Modify the paint() method in the Map class so that it draws (i.e., strokes) all of the GeneralPath objects in the Vector named polygons.
  3. Complete the MapDriver class so that it:
    • Creates a MapReader that will read from the file named usa48.txt
    • Reads the file
    • Creates a Map object
  4. Compile and execute the MapDriver.
4 Rendering Text: This part of the lab will help gain some experience rendering text.
  1. Modify the paint() method in the Map class so that it now also renders the title "United States of America" at point 0,50 (in the current font).
  2. Compile the Map class and execute the MapDriver.
  3. Declare a variable named defaultFont that is of type Font.
  4. Modify the constructor in the Map class so that defaultFont is instantiated as an italicized "Serif" font with a size of 50.
  5. Modify the paint() method in the Map class so that it uses defaultFont.
  6. Compile the Map class and execute the MapDriver.
  7. Modify the paint() method in the Map class so that it now also renders a title as close to the top as it can be while still being completely visible. (Hint: You will need to use the Graphics.getFont() java.awt.Graphics#getFont() method, the Graphics2D.getFontRenderContext() java.awt.Graphics2D#getFontRenderContext() method, Font.getLineMetrics(java.lang.String, java.awt.font.FontRenderContext) java.awt.Font#getLineMetrics(java.lang.String, java.awt.font.FontRenderContext) and the LineMetrics.getHeight() java.awt.font.LineMetrics#getHeight() methods in order to determine the baseline.)
5 Filling Geometric Shapes: This part of the lab will help you get some experience with filling geometric shapes.
  1. Modify the paint() method in the Map class so that it fills each state in blue and strokes each state in black.
  2. Modify the paint() method in the Map class so that it creates a random fill color for each state. (Hint: Use the Random java.util.Random class.)
6 Questions to Think About:
  1. How might you ensure that no two states that border each other are filled in the same color?

  2. Suppose you wanted to be able to fill each State in a different color on the basis of some data value (e.g., red if the State voted for the Republican presidential candidate and blue if the State voted for the Democratic presidential candidate). What kind of collection would you want to use for the data?

  3. Continuing with the example above, what kind of collection would you want to use for the State boundaries?

7 More Experience with Collections: If you'd like to get some more experience with collections, try implementing your answers to the last two "questions to think about". The following file contains the number of persons that were fatally injured in motor vehicle crashes in 2003:

Modify the classes above so that States with 0-100 fatalities are filled in green, 101-500 fatalities are filled in yellow, 501-1000 fatalities are filled in orange, and more than 1001 fatalities are filled in red.

Copyright 2006