import java.util.Hashtable; import java.io.*; import awt.*; import net.www.html.*; import browser.*; import browser.audio.*; class Bounce extends Applet implements Runnable { Image image; boolean done; AudioData sound; int x = 10; int y = 10; int YSpeed = 0; int XSpeed = 1; int YAccel = 1; int EnergyLoss = 4; public void run() { long lasttime; play(sound); try { sound = getAudioData("audio/bubble1.au"); image = getImage("images/ball.gif"); Thread.sleep(1000); lasttime = System.nowMillis(); while (!done) { long now = System.nowMillis(); long deltaT = now - lasttime; x+=XSpeed; y+=YSpeed; YSpeed+=YAccel; if (x<=0) XSpeed=Math.abs(XSpeed); if (x<0) x=0; if (x+image.width >= width) XSpeed=-Math.abs(XSpeed); if (x+image.width > width) x=width-image.width; if (y+image.height >= height-1) { YSpeed=-Math.abs(YSpeed)+EnergyLoss; if (YSpeed>0) YSpeed=0; else play(sound); } if (y+image.height > height-1) y=height-image.height-1; if (y<=0) YSpeed=Math.abs(YSpeed); if (y<0) y=0; repaint(); lasttime = now; Thread.sleep(33); } } finally { } } public void init() { if ((width <= 100) || (height <= 100)) { resize(500, 300); } } public void start() { done = false; (new Thread(this)).start(); } public void stop() { done = true; } public void paint(Graphics g) { g.setForeground(Color.gray); g.drawRect(0, 0, width - 1, height - 1); if (image != null) { g.drawImage(image, (int)x, (int)y); } } }