Computer Science 202
Introduction to Programming

Fall 2013, The College of Saint Rose

SnowyNight Demo

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



SnowyNight BlueJ Project

Click here to download a BlueJ project for SnowyNight.


SnowyNight Source Code

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


SnowyNight.java

import objectdraw.*;
import java.awt.*;
/*
 * 
 * Example SnowyNight: loops to draw a picture including some snowflakes
 *
 * Jim Teresco
 * The College of Saint Rose, Fall 2013
 * Based on example from CSCI 134, Williams College
 *
 * $Id: SnowyNight.java 2231 2013-10-27 18:40:06Z terescoj $
 */

// Program which creates clouds which create snow when the user clicks
public class SnowyNight extends WindowController {

    // constants for the moon size and location
    private static final double MOON_INSET = 50;
    private static final int MOON_SIZE = 70;

    // sizes and locations for the bricks
    private static final double BRICK_LINE = 40;  // from bottom
    private static final double BRICK_HEIGHT = 12;
    private static final double BRICK_WIDTH = 30;
    private static final double BRICK_SPACING = 3;

    // how many snow flakes?
    private static final int NUM_FLAKES = 25;

    // colors of the sky, mortar, and brick
    private final Color NIGHTSKY = new Color(50, 50, 100);
    private final Color MORTAR = new Color(200, 200, 200);
    private final Color BRICKRED = new Color(150, 40, 40);

    // image of the snow
    private Image snowPic;

    public void begin() {

        // where to draw next brick
        double brickPosition;

        // get leaf picture
        snowPic = getImage("snow.gif");

        // draw solid sky, mortar, and moon
        new FilledRect(0, 0, canvas.getWidth(), canvas.getHeight() - BRICK_LINE, canvas).setColor(NIGHTSKY);
        new FilledRect(0, canvas.getHeight() - BRICK_LINE, canvas.getWidth(), 
            canvas.getHeight(),
            canvas).setColor(MORTAR);
        new FilledOval(MOON_INSET,MOON_INSET,MOON_SIZE,MOON_SIZE,
            canvas).setColor(Color.white);

        // add the bricks
        brickPosition = 0;
        while ( brickPosition < canvas.getWidth() ) {
            new FilledRect(brickPosition, canvas.getHeight() - BRICK_LINE,
                BRICK_WIDTH, BRICK_HEIGHT,
                canvas).setColor(BRICKRED);

            brickPosition = brickPosition + BRICK_WIDTH + BRICK_SPACING;
        }

        // and finally, a bunch of snowflakes at random positions
        // but with a bit of a difference -- we want the snowflakes
        // to be placed anywhere on the upper 2/3 of the canvas, where
        // all flakes are visible but could be partially off either
        // side or the top of the canvas
        //
        // to accomplish this, we need the dimensions of the snowflake,
        // but those depend on the dimensions of the Image, which we
        // cannot easily get out hands on until we use it to create a
        // VisibleImage - so we create one, "measure" it, then remove
        // it from the canvas
        VisibleImage testFlake = new VisibleImage(snowPic, 0, 0, canvas);
        double width = testFlake.getWidth();
        double height = testFlake.getHeight();
        testFlake.removeFromCanvas();
        // now we know the range of values that should be allowed in our
        // random generators
        RandomDoubleGenerator xGen = new RandomDoubleGenerator(-width, canvas.getWidth());
        RandomDoubleGenerator yGen = new RandomDoubleGenerator(-height, canvas.getHeight()*2/3);
        for (int flakeNum = 0; flakeNum < NUM_FLAKES; flakeNum++) {
            new VisibleImage(snowPic, xGen.nextValue(), yGen.nextValue(), canvas);
        }

    }

}