FallingSnow.java
import objectdraw.*;
import java.awt.*;
public class FallingSnow extends ActiveObject {
// delay between snow motions
private static final int DELAY_TIME = 33;
// lowest point in window
private int screenHeight;
private DrawingCanvas canvas;
// speed of fall
private double ySpeed;
// the snowflake
private VisibleImage snow;
// initialize the instance variables and start the active object
public FallingSnow(DrawingCanvas aCanvas, Image aSnowPic,
double x, double aSpeed,
int aScreenHeight) {
canvas = aCanvas;
ySpeed = aSpeed;
screenHeight = aScreenHeight;
snow = new VisibleImage(aSnowPic, 0, 0, canvas);
snow.move(x, - snow.getHeight());
start();
}
public void run() {
// as long as tne snow is still on the screen
// move it and pause
while (snow.getY() < screenHeight ) {
pause(DELAY_TIME);
snow.move(0,ySpeed);
}
snow.removeFromCanvas();
}
}
Snow.java
import objectdraw.*;
import java.awt.*;
// Program which creates clouds which create snow when the user clicks
public class Snow extends WindowController {
// dimensions and location of the screen
private static final int SCREEN_LEFT = 0;
private static final int SCREEN_TOP = 0;
private static final int SCREEN_HEIGHT = 400;
private static final int SCREEN_WIDTH = 500;
// constatnts for the moon size and location
private static final int MOON_INSET = 50;
private static final int MOON_SIZE = 70;
// sizes and locations for the bricks
private static final double BRICK_LINE = SCREEN_HEIGHT*0.85;
private static final double BRICK_HEIGHT = SCREEN_HEIGHT*.04;
private static final double SKYLINE = BRICK_LINE + BRICK_HEIGHT;
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);
// 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,SCREEN_WIDTH,SKYLINE,
canvas).setColor(NIGHTSKY);
new FilledRect(0,SKYLINE,
SCREEN_WIDTH,SCREEN_HEIGHT-BRICK_LINE,
canvas).setColor(MORTAR);
new FilledOval(MOON_INSET,MOON_INSET,MOON_SIZE,MOON_SIZE,
canvas).setColor(Color.white);
// add the bricks
brickPosition = 0;
while ( brickPosition < SCREEN_WIDTH ) {
new FilledRect(brickPosition, BRICK_LINE,
BRICK_WIDTH, BRICK_HEIGHT,
canvas).setColor(BRICKRED);
brickPosition = brickPosition + BRICK_WIDTH + BRICK_SPACING;
}
}
public void onMouseClick(Location point) {
// make a new snow-dropping cloud when the user clicks
new Cloud(snowPic, SCREEN_WIDTH, SCREEN_HEIGHT, canvas);
}
}
Cloud.java
import objectdraw.*;
import java.awt.*;
public class Cloud extends ActiveObject {
// total number of snowflakes
private static final int MAX_SNOW = 150;
// the canvas
private DrawingCanvas canvas;
// picture of a snowflake
private Image snowPic;
// size of the screen
private int screenWidth, screenHeight;
// used to generate random speeds and positions for snowflakes
private RandomIntGenerator snowGen;
public Cloud( Image aSnowPic,
int aScreenWidth, int aScreenHeight,
DrawingCanvas aCanvas) {
// save the parameters for the "run" method
canvas = aCanvas;
snowPic = aSnowPic;
screenWidth = aScreenWidth;
screenHeight = aScreenHeight;
snowGen = new RandomIntGenerator( 0, screenWidth);
start();
}
public void run()
{
int snowCount = 0;
// continue creating snow until the maximum amount
// has been created
while (snowCount < MAX_SNOW ) {
snowCount = snowCount + 1;
new FallingSnow(canvas, snowPic,
snowGen.nextValue(), // x coordinate
snowGen.nextValue()*2/screenWidth+2, // y speed
screenHeight);
pause(900);
}
}
}