//CustomBounce.java //Make BounceBall capable of reading parameters from HTML //Andy Harris, 06/00 import java.awt.*; import java.applet.*; public class CustomBounce extends Applet implements Runnable { int dx = 10; int dy = 10; int ballX = 0; int ballY = 0; Image theImage; AudioClip theSound; Thread looper; boolean keepGoing = true; public void init(){ String imageName = getParameter("Image"); String soundName = getParameter("Sound"); if (imageName == null) { imageName = ("ball.gif"); } // end if if (soundName == null){ soundName = ("boing.au"); } // end if theImage = getImage(getCodeBase(), imageName); theSound = getAudioClip(getCodeBase(), soundName); } // end init public void start(){ if (looper == null){ looper = new Thread(this); looper.start(); } // end if } // end start public void stop(){ keepGoing = false; } // end stop public void run(){ while (keepGoing){ int appWidth = getSize().width; int appHeight = getSize().height; try { looper.sleep(100); } catch (InterruptedException exc){ System.out.println(exc.getMessage()); } // end try ballX += dx; ballY += dy; if (ballX > appWidth - theImage.getWidth(this)){ dx = -dx; theSound.play(); } // end if if (ballX < 0){ dx = -dx; theSound.play(); } // end if if (ballY > appHeight - theImage.getHeight(this)){ dy = -dy; theSound.play(); } // end if if (ballY < 0){ dy = - dy; theSound.play(); } // end if repaint(); } // end while } // end run public void paint(Graphics g){ g.drawImage(theImage, ballX, ballY, this); } // end paint } // end class def