Computer Science 252
Problem Solving with Java

Spring 2015, The College of Saint Rose

SwingFallingSnow Demo

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



SwingFallingSnow BlueJ Project

Click here to download a BlueJ project for SwingFallingSnow.


SwingFallingSnow Source Code

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


FallingSnow.java

import objectdraw.*;
import java.awt.*;
import javax.swing.JSlider;

/*
 * 
 * Example FallingSnow: the snowflakes that fall, this time with
 * x speeds controlled by a slider that was passed in and saved.
 *
 * Jim Teresco, Siena College, CSIS 120, Spring 2011
 * The College of Saint Rose, Fall 2013, Fall 2014
 * Based on example from CSCI 134, Williams College
 *
 * $Id: FallingSnow.java 2452 2014-10-13 15:59:20Z terescoj $
 */

public class FallingSnow extends ActiveObject {

    // delay between snow motions
    private static final int DELAY_TIME = 33;

    private DrawingCanvas canvas;

    // speed of fall
    private double ySpeed;

    // the snowflake
    private VisibleImage snow;

    // the wind
    private JSlider wind;
    
    // initialize the instance variables and start the active object
    public FallingSnow(DrawingCanvas aCanvas, Image aSnowPic, JSlider w,
                       double x, double aSpeed) {
        canvas = aCanvas;
        ySpeed = aSpeed;
        wind = w;

        snow = new VisibleImage(aSnowPic, 0, 0, canvas);
        snow.move(x, - snow.getHeight());

        start();
    }

    public void run() {

        // as long as the snow is still on the screen
        // move it and pause
        while (snow.getY() < canvas.getHeight()) {
            pause(DELAY_TIME);
            // get the x speed from the JSlider
            snow.move(wind.getValue(),ySpeed);
        }

        snow.removeFromCanvas();
    }

}

Snow.java

import objectdraw.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JSlider;

/*
 * 
 * Example SwingFallingSnow: the main class that creates clouds, which in turn
 * create falling snowflakes.
 * 
 * This one uses Java Swing components to control cloud creation and
 * the wind speed and direction
 *
 * Jim Teresco, The College of Saint Rose, Fall 2014
 *
 * $Id: Snow.java 2452 2014-10-13 15:59:20Z terescoj $
 */

// Program which creates clouds which create snow when the user pressed the 
// button
public class Snow extends WindowController implements ActionListener {

    // 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;

    // 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);
    
    // allowed wind speeds
    private static final int MAX_WIND = 5;

    // image of the snow
    private Image snowPic;
    
    // our wind controller
    private JSlider windSlider;

    public void begin() {

        // we'll get the Swing components there first
        Container contentPane = getContentPane();
        
        // a panel to go on the bottom of the window
        JPanel southPanel = new JPanel();
        
        // a button to create a new cloud, add to panel, set up listener
        JButton cloudButton = new JButton("Create Cloud");
        southPanel.add(cloudButton);
        cloudButton.addActionListener(this);
        
        // a slider that will control the wind
        windSlider = new JSlider(JSlider.HORIZONTAL, -MAX_WIND, MAX_WIND, 0);
        southPanel.add(windSlider);
        
        // add the panel to the south
        contentPane.add(southPanel, BorderLayout.SOUTH);
        contentPane.validate();
        
        // now start drawing things on our canvas
        
        // 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;
        }


    }

    public void actionPerformed(ActionEvent e) {
        // make a new snow-dropping cloud when the user presses
        // the (only) button
        new Cloud(snowPic, windSlider, canvas);
    }
}


Cloud.java

import objectdraw.*;
import java.awt.*;
import java.util.Random;
import javax.swing.JSlider;

/*
 * 
 * Example FallingSnow: the cloud class which is responsible for
 * creating a bunch of snowflakes, this one passing through a
 * JSlider to control "wind" to the snowflakes when created.
 *
 * Jim Teresco, Siena College, CSIS 120, Spring 2011
 * The College of Saint Rose, Fall 2013, Fall 2014
 * Based on example from CSCI 134, Williams College
 *
 * $Id: Cloud.java 2452 2014-10-13 15:59:20Z terescoj $
 */

public class Cloud extends ActiveObject {

    // total number of snowflakes
    private static final int MAX_SNOW = 150;
    // time between flakes
    private static final int FLAKE_INTERVAL = 900;

    // the canvas
    private DrawingCanvas canvas;

    // picture of a snowflake
    private Image snowPic;

    // used to generate random speeds and positions for snowflakes
    private Random snowGen;
    
    // our Wind generator
    private JSlider wind;

    public Cloud(Image aSnowPic, JSlider wind, DrawingCanvas aCanvas) {

        // save the parameters for the "run" method
        canvas = aCanvas;
        snowPic = aSnowPic;
        this.wind = wind;
 
        snowGen = new Random();

        start();
    }

    public void run()
    {
        for (int snowCount= 0; snowCount < MAX_SNOW; snowCount++) {

            new FallingSnow(canvas, snowPic, wind,
                            snowGen.nextInt((int)canvas.getWidth()),   // x coordinate
                            snowGen.nextDouble()*2+2); // y speed
            pause(FLAKE_INTERVAL);
        }
    }
}