import java.awt.*;

/**
 * Frame that displays the train set.
 *
 * YOU WILL USE THIS CLASS.  
 * THERE IS NO NEED FOR YOU TO MODIFY THIS CLASS.
 * If you want to know more about Java graphics follow 
 * the link in the hints and announcements page.
 */
 
public class TrainFrame extends Frame {

    /** The position of the top left pixel in the visible area. */
    public static int xOrigin = 5;
    public static int yOrigin = 25;

    /** The default font used to draw items. */
    public static Font FONT = new Font("Monospaced", Font.PLAIN, 10);

    /** The height and width of the this frame (set by paint). */
    public int h;
    public int w;
    
    /** The bottom and right edge of the trainset display. */
    public int bottomEdge;
    public int rightEdge;

    /** Sizes for the default font used to draw items. */
    public FontMetrics fm;
    public int fontCharWidth = 10;  // Initial values, reset by setDefaultFont
    public int fontAscent = 10;      

    /**
     * Paints the train set.
     *
     * @param  g  the graphics context to use for painting.
     */
    public void paint(Graphics g) {
        if (fm == null)
            setDefaults(g);

        // Paint the window white.
        g.setColor(Color.white);
        g.fillRect(0, 0, w, h);
                
        TrainSetDriver.theTrainSet.draw(g);
    }

    //**** Helper routines 

    /**
     * Set the pixel sizes for the default font.
     */
    public void setDefaults(Graphics g) {
    
        // Set fonts.
        g.setFont(FONT);
        fm = g.getFontMetrics(FONT);
        fontCharWidth = fm.charWidth('W');
        fontAscent = fm.getAscent();
        
        // Get my width and height.
        w = getBounds().width;
        h = getBounds().height;
        
        // Calculate lower and right bound of display window. 
        bottomEdge = frameBottom();
        rightEdge = frameRight();
        
    }

    /**
     * Draws the given string in the given graphics context at
     * at the given cursor location.
     *
     * @param  g  the graphics context in which to draw the string.
     * @param  s  the string to draw.
     * @param  x  the x-coordinate (in lines of text) of the
     *            string's cursor location.
     * @param  y  the y-coordinate (in number of characters) of the string's
     *            cursor location.
     */ 
    public void drawString(Graphics g, String s, int x, int y) {
        g.setFont(FONT);
        g.drawString(s, xOrigin+x*fontCharWidth, 
                     yOrigin+(y+1)*fontAscent);
    }

    /**
     * Get the bottom row of the current frame.
     */
    private int frameBottom() {

        // Compute the row number (in double) of the bottom row, and
        // then round it.
        double dy = ((double) (h - yOrigin))/fontAscent - 1;
        return((int) Math.round(dy));
    } 

    /**
     * Get the right row of the current frame.
     */
    private int frameRight() {

        // Compute the row number (in double) of the right row, and
        // then round it.
        double dx = ((double) (w - xOrigin))/fontCharWidth - 1;
        return((int) Math.round(dx));
    } 
}
