Computer Science 120
Introduction to Programming

Spring 2011, Siena College

Knitting Demo

A working demo of Knitting will appear below. Click inside the applet to interact with it.



Knitting BlueJ Project

Click here to download a BlueJ project for Knitting.


Knitting Source Code

The Java source code for Knitting is below. Click on a file name to download it.


Knitting.java

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

/*
 * Example Knitting: make scarves
 *
 * Jim Teresco, Siena College, CSIS 120, Spring 2011
 * Based on example from Williams College CSCI 134
 *
 * $Id: Knitting.java 1535 2011-02-14 03:49:40Z terescoj $
 */

public class Knitting extends WindowController {

    private static final int DIAMETER = 12; // diameter of a stitch

    // spacing between stitches -- will overlap
    private static final int X_DISP = 8;
    private static final int Y_DISP = 8;

    private static final int SCARF_WIDTH = 12; // num stitches per row
    private static final int SCARF_HEIGHT = 40; // num stitches per column

    private static final Color SCARF_COLOR = Color.blue;  // scarf color

    public void begin() {
        new Text("Click to make a scarf", 10, 10, canvas);
    }

    // draws a scarf with upper left at point of click
    public void onMouseClick(Location point) {
        double y = point.getY(); // y position of the next "stitch"
        int numRows = 0;

        // draw SCARF_HEIGHT rows 
        while (numRows < SCARF_HEIGHT) {

            double x = point.getX();  // / x position of the next "stitch"
            int numCols = 0;

            // draw one row consisting of SCARF_WIDTH circles
            while (numCols < SCARF_WIDTH) {
                FramedOval stitch = new FramedOval(x, y, DIAMETER, DIAMETER, canvas);
                stitch.setColor(SCARF_COLOR);
                x = x + X_DISP;
                numCols = numCols + 1;
            }

            y = y + Y_DISP;
            numRows = numRows + 1;
        }
    }
}