CIS191-s12/Project 13

From MCIS Wiki
Jump to: navigation, search

This is due on Wednesday, April 25.


Create a with a large panel, a JLabel on the top, and three buttons on the right. Set the preferred size of the panel to be [1000, 650]. Name the buttons "Load Map", "Show Map", and "Directions". Make sure all of these have a reasonable name. You will need to add event handlers for the buttons. In the "Load Map" button, use the following code to use a file dialog:

      int retval = fileChooser.showOpenDialog(this);
      if (retval == JFileChooser.APPROVE_OPTION) {
           File file = fileChooser.getSelectedFile();
           // Place a call to your file reader here.

Place the following in your Main:

public class MyPanel extends JPanel {
       public void paint(Graphics g) {
           // Place code to draw the map here

You need to change the "Custom Creation Code" of the panel to "new MyPanel()"

A Point Class

Create a new class, Point, that corresponds to locations on the map. This class contains two ints: x and y. This class needs methods that do the following:

  • toString, as usual, to print the point in (x,y) format.
  • Calculate the screen X coordinate of the point. For the given map, this is twice the x coordinate on the map.
  • Calculate the screen Y coordinate of the point. Set this to 1.3 times the map y coordinate minus 30 (these formulas are needed to correctly scale the map onto the panel).
  • Determine whether a point represented by integers x and y is within 5 pixels of the screen coordinate associated with a point.

Old Code

Reuse the City and Road classes from the previous project. Change the City class to use a Point instead of having x and y. Add another static method to the City class that clears out the arraylist of cities.

Place the file reading code in a separate class. In this class, put a method named "readMap" which takes a File argument and loads the file into the list of cities. Clear out any existing map when reading in a new file.

Take the report printer and place it in the handler for the "show map" function. That's all you need to do for this button.

Ignore the "Directions" button for now.

Display Code

The paint method for the panel needs to draw the map. For each road, draw a red line from the city at the start to the city at the end. Remember to convert the position of the cities to pixel coordinates. Use the drawLine method in the Graphics class for this.

Once the lines are drawn, place a black circle that is 5px wide at the location of each city. Just above and right of this circle use drawString to put the name of the town on the map. Use two instance variables to select up to two cities for special coloring: one for the start of the trip and the other for the end. These are set by the mouse click event. Select distinctive colors for the start and end cities - all others should be black.

Set an appropriate font before drawing text - I used the following:

 g.setFont(new java.awt.Font("Tahoma", 1, 11));

You need to do


any time the picture changes.

Map Loading

The code to choose your map file is provided in the handout. All you need to add is a call to your file loader and a line to set the status with a message indicate how many cities were loaded in.

Mouse Clicks

Keep two variables in your code to represent the start and end of a trip. When the mouse is clicked on the map, search for a city that is near the point at which the mouse is clicked. If a city is near the mouse, set a variable for the trip end to that city and set the trip beginning to the previous trip end. Draw the names of two cities selected at start and end in a distinctive color.

The Map

The map of Colorado is here: