import objectdraw.*;
import java.awt.*;

/*
 * A program to illustrate a simple loop to draw
 * rail road ties.
 */
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

    /*
     * Draw rails and background.
     */
    public void begin() {
        
        // create a background and the two rails.
        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;

        while (tiePosition < SCREEN_WIDTH) {
            
            new FilledRect(tiePosition, TIE_TOP, TIE_WIDTH, TIE_LENGTH, canvas).setColor(TIE_COLOR);

            // move rails on top of new tie
            rail1.sendToFront();
            rail2.sendToFront();
            
            // update position for next tie
            tiePosition = tiePosition + TIE_WIDTH + TIE_SPACING;
        }
    }
}

