RailroadWhile.java
import objectdraw.*;
import java.awt.*;
public class RailroadWhile extends WindowController{
private static final int SCREEN_LEFT = 0;
private static final int SCREEN_TOP = 0;
private static final int SCREEN_HEIGHT = 400;
private static final int SCREEN_WIDTH = 500;
private static final double GAUGE = 100;
private static final double TRACK_TOP = (SCREEN_HEIGHT - GAUGE) / 2;
private static final double RAIL_WIDTH = 10;
private static final double TIE_WIDTH = 20;
private static final double TIE_EXTEND = TIE_WIDTH;
private static final double TIE_LENGTH = GAUGE + 2 * RAIL_WIDTH +
2 * TIE_EXTEND;
private static final double TIE_SPACING = 25;
private static final double TIE_TOP = TRACK_TOP - TIE_EXTEND;
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 double tiePosition; // where to draw next tie
private FilledRect rail1, rail2;
public void begin()
{
// draw the "grass", just a green background
new FilledRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT,
canvas).setColor(GROUND_COLOR);
// loop to draw railroad ties across the screen
tiePosition = 5;
while ( tiePosition < SCREEN_WIDTH ) {
new FilledRect(tiePosition, TIE_TOP, TIE_WIDTH, TIE_LENGTH,
canvas).setColor(TIE_COLOR);
tiePosition = tiePosition + TIE_WIDTH + TIE_SPACING;
}
// finally, the rails themselves.
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);
}
}