import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import java.awt.Graphics2D.*; import java.awt.geom.Line2D; /** * GridGIS creates a window and adds buttons to it, which allow the user * to choose which map they would like to view. The user will click on * the button and GIS tells Grid to create and draw a map of this area. * The map must be in the form *.asc to work in this class. To switch the * map options, change "STRING_1", "STRING_2", or "STRING_3" to a different * file name (less the ".asc"). * * @Elizabeth French * @04/04/08 */ public class GridGIS extends JFrame implements MouseListener { private static final int WINDOW_SIZE = 400; //Strings for file names attached to the buttons private static final String STRING_1 = "test1", STRING_2 = "set1", STRING_3 = "test2", //The file type of the maps FILE_END = ".asc"; private Grid grid; //JPanel with available maps and a "clear" button private JPanel mapOptions; private JButton map1, map2, map3, clear; //Is there a map in the window? private boolean mapped = false; /** Constructer will create the window and place 3 map options and one * "clear" option (to clear the window) buttons in the window. It will * also add listeners to the buttons and set itself as the listener */ public GridGIS() { super("My Grid"); // declare JButtons with map names map1 = new JButton(STRING_1); map2 = new JButton(STRING_2); map3 = new JButton(STRING_3); clear = new JButton("Clear"); // declare JPanel with options for the map to draw mapOptions = new JPanel(); // add JButtons to mapOptions mapOptions.add(map1); mapOptions.add(map2); mapOptions.add(map3); mapOptions.add(clear); // calls on method to add listeners to buttons addMapListeners(); // add mapOptions panel to frame getContentPane().add("South", mapOptions); //exit on close setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); addMouseListener(this); //Set size, allowing for extra height b/c of mapOption panel setSize(WINDOW_SIZE,WINDOW_SIZE + mapOptions.getHeight()); System.out.println("You must press 'Clear' after a map is drawn" + " if you would like to draw a different map"); } /** Method to add listeners to the buttons. Listeners will check to * see if a map is already drawn. If not, then pressing on a map * button will draw that map. If so, then pressing on "clear" will * remove the map, allowing you to create another * Maps will correspond to files with same names as the buttons, ending * in the FILE_END as the file type * @param none * @return void */ public void addMapListeners() { map1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { //If the window is clear, make the map if (!mapped) makeGrid(STRING_1.concat(FILE_END)); } }); map2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { //If the window is clear, make the map if (!mapped) makeGrid(STRING_2.concat(FILE_END)); } }); map3.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { //If the window is clear, make the map if (!mapped) makeGrid(STRING_3.concat(FILE_END)); } }); clear.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { //If there is a map, then clear it if (mapped) { repaint(); mapped = false; } } }); } /** Method to create a grid using the file name of the parameter given * @param name the file name of the grid to create * @return void */ public void makeGrid(String name) { Graphics2D g2 = (Graphics2D)getGraphics(); //Create and draw grid with file name and give the window size grid = new Grid(name, WINDOW_SIZE); grid.drawGrid(g2); //Redraw the buttons mapOptions.repaint(); mapped = true; } //Empty, but must comply with MouseListener interface public void mousePressed(MouseEvent e) {} public void mouseClicked(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} /** Main method creates the GIS which will allow the user to choose * which map (of three choices) they want * @param args * @return void */ public static void main(String[] args) { GridGIS gis = new GridGIS(); } }