/** * Road.java * * @author David Tran * @version 1.0 */ package background; import java.awt.Graphics2D; import java.awt.Color; import java.awt.Stroke; import java.awt.BasicStroke; import java.awt.Point; import java.awt.geom.*; public class Road extends Background { protected boolean isVertical; /** * Initialize variables for the vehicle * * @param x x-coordinate of Road in the world * @param y y-coordinate of Road in the world */ public Road(double x, double y, double w, double h) { super(x, y, w, h); super.externalName = "Road"; //subclass should change this this.isVertical = true; //a vertical road by default } /** * Use the graphics system to actually draw the shapes * we have created. * * @param g the graphics system to use */ public void draw(Graphics2D g) { g.setPaint(Color.GRAY); g.fill(new Rectangle2D.Double(xCoor, yCoor, width, height)); // Draw the yellow line dividing the road between north/south or west/east traffic Stroke oldStroke = g.getStroke(); float dash1[] = {10.0f}; BasicStroke bs = new BasicStroke(1.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10.0f, dash1, 0.0f); g.setStroke(bs); Line2D line = new Line2D.Float(20.0f, 10.0f, 100.0f, 10.0f); g.setPaint(Color.YELLOW); // Set the coordinates of dashed line for the road, depending on // its orientation (e.g. vertical vs. horizontal) if (isVertical) { line.setLine(new Point2D.Double(xCoor + width/2, yCoor), new Point2D.Double(xCoor + width/2, yCoor + height)); g.draw(line); } else { line.setLine(new Point2D.Double(xCoor, yCoor + height/2), new Point2D.Double(xCoor + width, yCoor + height/2)); g.draw(line); } g.setStroke(oldStroke); } }