KnitAScarf


Your browser is ignoring the <APPLET> tag!

When the user clicks the mouse, the program draws a scarf as rows and column of interlocking ovals.

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

/**
 * Draw a picture that looks like a knitted scarf.
 */
public class KnitAScarf extends WindowController
{
    // The dimensions of the scarf
    private static final int SCARF_WIDTH = 75;
    private static final int SCARF_HEIGHT = SCARF_WIDTH * 4;
    
    // Scarf color
    private static final Color SCARF_COLOR = Color.BLUE;
    
    // Size of a single stitch
    private static final int DIAMETER = 10;
    
    // Distance between stitches within a row
    private static final int X_DISP = 7;
    
    // Distance between rows
    private static final int Y_DISP = X_DISP;
    
    /**
     * Draw a scarf when the user clicks the mouse.
     * @param point where the upper left corner of the scarf should be
     */
    public void onMouseClick (Location point) {
        // Get location of first stitch
        double x = point.getX();
        double y = point.getY();

        // while there are more rows to knit
        while (y < point.getY() + SCARF_HEIGHT) {
            // knit one row
            while (x < point.getX() + SCARF_WIDTH) {
                // knit one stitch
                new FramedOval(x, y, DIAMETER, DIAMETER, canvas).setColor(SCARF_COLOR);
                
                // update for next stitch
                x = x + X_DISP;
            }

            // Update for next row by moving y down and moving x back
            // to the left edge of the next row.
            y = y + Y_DISP;
            x = point.getX();
        }
    }
}