/* * Dispatcher.java * * Skeleton of Taxicab Dispatcher Applet * * This was created by implementing HW1 and then deleting much * of the code. The purpose of the skeleton is to demonstrate * a few techniques that you could use, including some of the * event handling. This is not guaranteed to compile any more! * */ import java.applet.*; import java.awt.*; import java.awt.event.*; import java.lang.*; // Define a private class to define a Street, which is just // a line on the map. It simply has x1, y1 & x2, y2 values // representing the end points of the street (line). // class Street { public int x1; public int y1; public int x2; public int y2; public Street (int X1, int Y1, int X2, int Y2) { x1 = X1; x2 = X2; y1 = Y1; y2 = Y2; } } public class CabDispatcher extends java.applet.Applet implements MouseListener, MouseMotionListener { // Define the street map. It's just an array of lines. // To define more streets, just add another line to the // static definition below. Note that the first 4 entries // define the border - they draw horizontal lines from // XMIN, YMIN to XMAX, YMIN & from XMIN, YMAX to XMAX, YMAX // and vertical lines from XMIN, YMIN to XMIN, YMAX & from // XMAX, YMIN to XMAX, YMAX. // To keep streets in the legal area, x & y should always // be in the range XMIN - XMAX & YMIN - YMAX respectively. private static final Street [] map = {new Street (Cab.XMIN, Cab.YMIN, Cab.XMAX, Cab.YMIN), // Top (north) border street new Street (Cab.XMIN, Cab.YMAX, Cab.XMAX, Cab.YMAX), // Bottom (south) border street new Street (Cab.XMIN, Cab.YMIN, Cab.XMIN, Cab.YMAX), // Left (west) border street new Street (Cab.XMAX, Cab.YMIN, Cab.XMAX, Cab.YMAX), // Right (east) border street new Street (Cab.XMIN, Cab.YMIN, Cab.XMAX/2, Cab.YMAX/2), new Street (Cab.XMAX/2, Cab.YMAX/2, Cab.XMAX, Cab.YMAX/2), new Street (Cab.XMAX/2, Cab.YMAX/2, Cab.XMAX/2, Cab.YMAX) }; // Define constant for how many cabs we will model; private static final int NUMBER_OF_CABS = 12; // Define an array of cabs Cab [] cabs = new Cab [NUMBER_OF_CABS]; // The current mouse-selected cab (if any) private Cab pick; // Define the widgits here ... /** Initialization method that will be called after the applet is loaded * into the browser. */ public void init () { // Start with no mouse selected cab. pick = null; // Make the applet the listener for the mouse events. addMouseListener(this); addMouseMotionListener(this); // .. the rest of init has been deleted .. } public void paint(Graphics g) { // Draw the map (deleted) // draw the cabs - first fill in the rectangle, // then the label // .. deleted .. } // End paint // some private methods deleted here.. public void mouseClicked(MouseEvent e) { for (int i = 0; i < NUMBER_OF_CABS; i++) { // if the cab contains the mouse position, // then show the cab's state / status if (cabs[i].rectangle.contains(e.getPoint())) { cabSelect.select(i); showCabState(i); } } // End for } // Unimplemented methods (we ignore enter / exit events) public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } // Overridden methods from MouseListener interface public void mousePressed(MouseEvent e) { // attempt to pick up a cab (its rectangle) if (pick == null) { for (int i = 0; i < NUMBER_OF_CABS; i++) { // if the cab contains the mouse position, // make it the selected cab if (cabs[i].rectangle.contains(e.getPoint())) { pick = cabs[i]; return; } } // End for } } // End mousePressed public void mouseReleased(MouseEvent e) { // release the pick rectangle and repaint the scene pick = null; repaint(); } // Overridden method from MouseMotionListener interface public void mouseDragged(MouseEvent e) { // if we have a picked rectangle, set its position to the // current mouse position and repaint if (pick != null) { pick.setXLoc(e.getX()); pick.setYLoc(e.getY()); pick.rectangle.x = pick.getXLoc(); pick.rectangle.y = pick.getYLoc(); pick.rectangle.width = Cab.CABWIDTH; pick.rectangle.height = Cab.CABHEIGHT; repaint(); } } // End mouseDragged // Unimplemented method (we ignore mouse move events) public void mouseMoved(MouseEvent e) { } // other event handlers deleted ..