Active+Objects

In java, active object class is designed to allow objects to move in a specific path without the user having to constantly click for each individual movement. Active objects are made up of two separate classes; the active object class itself, and the class that creates it.

FallingSnow.java
code 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; } }

code This class is part of a progarm that creates snowflakes at a random X position across the screen (in many cases you'll want to use the VisibleImage class rather than the Image class. Notice the "start" and "public void run" methods. The start method basically implements everything in the run method, so when you implement a "FallingSnow" class, it will initialize it's variables, create the snow using "aSnowPic", set it to location (0,0) on the canvas, move it, and execute everthing in the **public void run** method.