import java.applet.*; // allows access to MediaTracker
import java.awt.*;
import hsa.*;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Random;
public class ApplePic
{
static Timer timer;
static final int MAX_TIME = 1;
static final long PERIOD = 1;
static Console c;
static Image picture; // image class
static int picWidth, picHeight; // store width/height
private static final String PICTURE_PATH = "Fruits.jpg";
public static void loadImage () // This sets up the image
{
picture = Toolkit.getDefaultToolkit ().getImage (PICTURE_PATH); // Load the image.
// Now, it can actually take some time to load the image, and
// it could fail (image not found, etc). The following checks for all that.
MediaTracker tracker = new MediaTracker (new Frame ());
tracker.addImage (picture, 0); // Add the picture to the list of images to be tracked
// Wait until all the images are loaded. This can throw an
// InterruptedException although it's not likely, so we ignore it if it occurs.
try
{
tracker.waitForAll ();
}
catch (InterruptedException e)
{
}
if (tracker.isErrorAny ()) // If there were any errors then abort theprogram
{
c.println ("Couldn't load " + PICTURE_PATH);
return;
}
picWidth = picture.getWidth (null); // Initialize the picture size
picHeight = picture.getHeight (null);
} // init method
public ApplePic () // Create the console
{
c = new Console ();
}
public static void main (String[] args)
{
ApplePic a = new ApplePic ();
// Load the picture
loadImage ();
Random rd = new Random ();
final long time = 1 + rd.nextInt (MAX_TIME);
final Timer timer = new Timer ();
timer.schedule (new TimerTask ()
{
public void run ()
{
// // Initialize x, y, dx, dy
int x, y;
int dx, dy;
x = 200;
y = -100;
dx = 3;
dy = 3;
do
{
for (y = -500 ; y < 500 ; y++)
{
try
{
Thread.sleep (5);
}
catch (InterruptedException e)
{
}
x = x;
y++;
dy = +dy;
dx = -dx;
c.clearRect (x, y, picWidth, picHeight);
x += dx;
y += dy;
c.drawImage (picture, x, y, null);
}
}
while (y - dy > 0);
}
timer.cancel ();
}
, 0, PERIOD);
} // main method
}
|