Computer Science 252
Problem Solving with Java

Spring 2014, The College of Saint Rose

DragAllRoadsForeach Demo

A working demo of DragAllRoadsForeach will appear below. Click inside the applet to interact with it.



DragAllRoadsForeach BlueJ Project

Click here to download a BlueJ project for DragAllRoadsForeach.


DragAllRoadsForeach Source Code

The Java source code for DragAllRoadsForeach is below. Click on a file name to download it.


DragAllRoadsForeach.java

import objectdraw.*;
import java.awt.*;
import java.util.ArrayList;

/* $Id: DragAllRoadsForeach.java 2346 2014-04-15 13:17:42Z terescoj $ */

/**
 * Example DragAllRoadsForeach: draw "road segments" at mouse
 * press points, but drag any existing segment instead
 * if it happens to contain the mouse press point, using 
 * a for each loop to traverse.
 *
 * @author Jim Teresco
 * The College of Saint Rose, CSC 252, Spring 2014
 *
 */

public class DragAllRoadsForeach 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 (RoadSegment segment : segments) {
            
            if (segment.contains(point)) {
                selectedSegment = segment;
                dragging = true;
                lastMouse = point;
            }
        }
        
        // if dragging is still false, we create a new segment
        if (!dragging) {
            
            // create the new segment and insert it into the list.
            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;      
        }
    }  
    
    public void onMouseExit(Location point) {
        
        if (dragging) {
            selectedSegment.removeFromCanvas();
            segments.remove(selectedSegment);
            dragging = false;
        }
    }
 }

RoadSegment.java

import objectdraw.*;
import java.awt.*;
import java.util.ArrayList;

/* $Id: RoadSegment.java 2346 2014-04-15 13:17:42Z 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
 * Updated to use ArrayLists, Spring 2012
 */

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 ArrayList<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 ArrayList<FilledRect>();
        
        for (int stripeNum = 0; stripeNum < numStripes; stripeNum++) {
            FilledRect nextStripe =
                new FilledRect(stripeX, nextStripeY, STRIPE_WIDTH, STRIPE_LENGTH, canvas);
            centerStripes.add(nextStripe); 
            nextStripe.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 (FilledRect stripe : centerStripes) {
            stripe.move(dx, dy);
        }
    }
    
    public void removeFromCanvas() {
        
        pavement.removeFromCanvas();
        sideStripe1.removeFromCanvas();
        sideStripe2.removeFromCanvas();
        for (FilledRect stripe : centerStripes) {
            stripe.removeFromCanvas();
        }
        centerStripes.clear();
    }
}