Computer Science 252
Problem Solving with Java
Spring 2015, The College of Saint Rose
DrawRoads Demo
A working demo of DrawRoads will appear below. Click inside the applet to interact with it.
DrawRoads BlueJ Project
Click here to download a BlueJ project for DrawRoads.
DrawRoads Source Code
The Java source code for DrawRoads is below. Click on a file name to download it.
import objectdraw.*; import java.awt.*; /* $Id: DrawRoads.java 2218 2013-10-18 14:06:39Z terescoj $ */ /** * Example DrawRoads: draw "road segments" at mouse * click points. * * @author Jim Teresco * Siena College, CSIS 120, Spring 2011 * */ public class DrawRoads extends WindowController { /** Draw a road segment at the mouse press point * * @param point the Location of the mouse press */ public void onMousePress(Location point) { new RoadSegment(point, canvas); } }
import objectdraw.*; import java.awt.*; /* $Id: RoadSegment.java 2218 2013-10-18 14:06:39Z terescoj $ */ /** * Class to draw a simple road segment at a given point * on the given canvas. * * @author Jim Teresco * Siena College, CSIS 120, Spring 2011 */ public class RoadSegment { // constant parameters defining the sizes for the road segment private static final double ROAD_WIDTH = 50; private static final double ROAD_LENGTH = 200; private static final double STRIPE_WIDTH = 3; private static final double STRIPE_LENGTH = 20; private static final double STRIPE_SPACING = 20; private static final double 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; /** * 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 new FilledRect(point, ROAD_WIDTH, ROAD_LENGTH, canvas).setColor(ROAD_COLOR); // side stripes new FilledRect(point.getX() + SIDE_STRIPE_INSET, point.getY(), STRIPE_WIDTH, ROAD_LENGTH, canvas).setColor(SIDE_LINE_COLOR); new FilledRect(point.getX() + ROAD_WIDTH - SIDE_STRIPE_INSET - STRIPE_WIDTH, point.getY(), STRIPE_WIDTH, ROAD_LENGTH, canvas).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; while (nextStripeY >= point.getY()) { new FilledRect(stripeX, nextStripeY, STRIPE_WIDTH, STRIPE_LENGTH, canvas).setColor(CENTER_LINE_COLOR); nextStripeY = nextStripeY - STRIPE_LENGTH - STRIPE_SPACING; } } }