Computer Science 120
Introduction to Programming
Spring 2011, Siena College
DragAllRoadsArrayList Demo
A working demo of DragAllRoadsArrayList will appear below. Click inside the applet to interact with it.
DragAllRoadsArrayList BlueJ Project
Click here to download a BlueJ project for DragAllRoadsArrayList.
DragAllRoadsArrayList Source Code
The Java source code for DragAllRoadsArrayList is below. Click on a file name to download it.
import objectdraw.*; import java.awt.*; import java.util.ArrayList; /* $Id: DragAllRoadsArrayList.java 1585 2011-03-31 04:00:23Z terescoj $ */ /** * Example DragAllRoadsArrayList: draw "road segments" at mouse press * points, but drag any existing segment instead if it happens to * contain the mouse press point. * * This version uses an ArrayList to store the segments. * * @author Jim Teresco * Siena College, CSIS 120, Spring 2011 * */ public class DragAllRoadsArrayList extends WindowController { // the road segments in our drawing private ArrayList<RoadSegment> segments; // the road segment currently being dragged, if any private RoadSegment selectedSegment; // last mouse point and boolean flag to support dragging private boolean dragging; private Location lastMouse; /** * Set up: just initialize the array of segments * and set other instance variables. */ public void begin() { segments = new ArrayList<RoadSegment>(); selectedSegment = null; dragging = false; lastMouse = null; } /** Draw a road segment at the mouse press point * unless the most recent road drawn was at that * point, in which case we begin a drag operation. * * @param point the Location of the mouse press */ public void onMousePress(Location point) { // see if we've pressed the mouse on an existing road segment for (int segmentNum = 0; segmentNum < segments.size(); segmentNum++) { if (segments.get(segmentNum).contains(point)) { selectedSegment = segments.get(segmentNum); dragging = true; lastMouse = point; } } // if dragging is still false, we create a new segment if (!dragging) { // create the new segment and insert it into arraylist segments.add(new RoadSegment(point, canvas)); dragging = false; } } /** Perform dragging operation if we are dragging anything * * @param point the Location of the mouse press */ public void onMouseDrag(Location point) { if (dragging) { selectedSegment.move(point.getX() - lastMouse.getX(), point.getY() - lastMouse.getY()); lastMouse = point; } } /** Complete dragging operation if we are dragging anything * * @param point the Location of the mouse press */ public void onMouseRelease(Location point) { if (dragging) { selectedSegment.move(point.getX() - lastMouse.getX(), point.getY() - lastMouse.getY()); dragging = false; } } }
import objectdraw.*; import java.awt.*; /* $Id: RoadSegment.java 1585 2011-03-31 04:00:23Z terescoj $ */ /** * Class to draw a simple road segment at a given point * on the given canvas, with contains and move capabilities. * * @author Jim Teresco * Siena College, CSIS 120, Spring 2011 */ public class RoadSegment { // constant parameters defining the sizes for the road segment private static final int ROAD_WIDTH = 50; private static final int ROAD_LENGTH = 200; private static final int STRIPE_WIDTH = 3; private static final int STRIPE_LENGTH = 20; private static final int STRIPE_SPACING = 20; private static final int SIDE_STRIPE_INSET = 2; // default colors private static final Color ROAD_COLOR = new Color(32, 32, 32); private static final Color SIDE_LINE_COLOR = Color.white; private static final Color CENTER_LINE_COLOR = Color.yellow; // the graphical components of our road segment private FilledRect pavement; private FilledRect sideStripe1, sideStripe2; private FilledRect[] centerStripes; /** * Construct a new RoadSegment * * @param point the Location of the upper left corner of the segment * @param canvas the DrawingCanvas on which to draw */ public RoadSegment(Location point, DrawingCanvas canvas) { // the pavement pavement = new FilledRect(point, ROAD_WIDTH, ROAD_LENGTH, canvas); pavement.setColor(ROAD_COLOR); // side stripes sideStripe1 = new FilledRect(point.getX() + SIDE_STRIPE_INSET, point.getY(), STRIPE_WIDTH, ROAD_LENGTH, canvas); sideStripe1.setColor(SIDE_LINE_COLOR); sideStripe2 = new FilledRect(point.getX() + ROAD_WIDTH - SIDE_STRIPE_INSET - STRIPE_WIDTH, point.getY(), STRIPE_WIDTH, ROAD_LENGTH, canvas); sideStripe2.setColor(SIDE_LINE_COLOR); // center stripes, draw from bottom to top double nextStripeY = point.getY() + ROAD_LENGTH - STRIPE_LENGTH; double stripeX = point.getX() + ROAD_WIDTH/2 - STRIPE_WIDTH/2; // construct an array large enough to hold our stripes int numStripes = ROAD_LENGTH/(STRIPE_LENGTH + STRIPE_SPACING); centerStripes = new FilledRect[numStripes]; for (int stripeNum = 0; stripeNum < numStripes; stripeNum++) { centerStripes[stripeNum] = new FilledRect(stripeX, nextStripeY, STRIPE_WIDTH, STRIPE_LENGTH, canvas); centerStripes[stripeNum].setColor(CENTER_LINE_COLOR); nextStripeY = nextStripeY - STRIPE_LENGTH - STRIPE_SPACING; } } /** * Return if the given location is within the graphical area covered by this * road segment * * @param point the Location to check for containment * @return whether the given Location is in this road segment */ public boolean contains(Location point) { return pavement.contains(point); } /** * Move the road segment by the given x and y offsets. * * @param dx the amount to move in the x coordinate * @param dy the amount to move in the y coordinate * */ public void move(double dx, double dy) { // move each component by these offsets pavement.move(dx, dy); sideStripe1.move(dx, dy); sideStripe2.move(dx, dy); for (int stripeNum = 0; stripeNum < centerStripes.length; stripeNum++) { centerStripes[stripeNum].move(dx, dy); } } }