Computer Science 120
Introduction to Programming
Fall 2011, Siena College
Railroad Demo
A working demo of Railroad will appear below. Click inside the applet to interact with it.
Railroad BlueJ Project
Click here to download a BlueJ project for Railroad.
Railroad Source Code
The Java source code for Railroad is below. Click on a file name to download it.
import objectdraw.*; import java.awt.*; /* * Example Railroad -- draw a railroad track with a loop * * Jim Teresco, Siena College, CSIS 120, Fall 2011 * * $Id: Railroad.java 1535 2011-02-14 03:49:40Z terescoj $ */ public class Railroad extends WindowController { // screen dimensions private static final int SCREEN_HEIGHT = 400; private static final int SCREEN_WIDTH = 400; // distance between rails private static final double GAUGE = 100; // where to start top rail private static final double TRACK_TOP = (SCREEN_HEIGHT - GAUGE) / 2; // width of rail private static final double RAIL_WIDTH = 10; // width of tie private static final double TIE_WIDTH = 20; // how far tie extends beyond rails private static final double TIE_EXTEND = 20; // length of tie private static final double TIE_LENGTH = GAUGE + 2 * RAIL_WIDTH + 2 * TIE_EXTEND; // distance between successive ties private static final double TIE_SPACING = 25; // coordinate of top of ties private static final double TIE_TOP = TRACK_TOP - TIE_EXTEND; // x coordinate of first tie private static final double FIRST_TIE_X = 5; // colors of ground, rails, and ties private final Color GROUND_COLOR = new Color(50, 150, 50); private final Color RAIL_COLOR = new Color(200, 200, 200); private final Color TIE_COLOR = new Color(150, 40, 40); private FilledRect rail1, rail2; // rails of track public void begin() { // draw rails and background new FilledRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, canvas).setColor(GROUND_COLOR); rail1 = new FilledRect(0, TRACK_TOP, SCREEN_WIDTH, RAIL_WIDTH, canvas); rail1.setColor(RAIL_COLOR); rail2 = new FilledRect(0, TRACK_TOP + GAUGE, SCREEN_WIDTH, RAIL_WIDTH, canvas); rail2.setColor(RAIL_COLOR); double tiePosition = FIRST_TIE_X; // draw each tie while ( tiePosition < SCREEN_WIDTH ) { new FilledRect(tiePosition, TIE_TOP, TIE_WIDTH, TIE_LENGTH, canvas).setColor(TIE_COLOR); rail1.sendToFront(); // move rails on top of new tie rail2.sendToFront(); // update position for next tie tiePosition = tiePosition + TIE_WIDTH + TIE_SPACING; } } }