//Title: Instructor //Version: 1.0 //Copyright: Copyright (c) Vairavan Vairavan //Author: Vairavan Vairavan //Company: UWM //Description: package ballbounce; import java.awt.*; import java.awt.event.*; import java.applet.*; public class BallBounce extends Applet implements Runnable,ActionListener { boolean isStandalone = false; private Thread m_ball = null; Image ballpic ; String ballsrc = "balls011.GIF"; int xpos=10; int ypos = 220; Button bFast,bSlow; int sleeptime=500; //Get a parameter value public String getParameter(String key, String def) { return isStandalone ? System.getProperty(key, def) : (getParameter(key) != null ? getParameter(key) : def); } //Construct the applet public BallBounce() { } //Initialize the applet public void init() { setSize(320, 300); bFast = new Button("BOUNCE FAST"); bSlow = new Button("BOUNCE SLOW"); bFast.addActionListener(this); bSlow.addActionListener(this); add(bFast); add(bSlow); } //Component initialization private void jbInit() throws Exception { } public void paint(Graphics g) { g.drawImage(ballpic, xpos, ypos,this); } public void start() { if (m_ball == null) { m_ball = new Thread(this); m_ball.start(); } } public void stop() { if (m_ball != null) m_ball = null; } public void run() { // initialize ballpic = getImage(getCodeBase(),ballsrc); setBackground(Color.white); // bounce ball from one side of the screen to the other side while (xpos< this.getSize().width) { bounce(true); bounce(false); } } public void actionPerformed(ActionEvent e) { Button b = (Button) e.getSource(); if (b.equals(bFast)) { sleeptime-=50; if (sleeptime == 0) sleeptime=50; } if (b.equals(bSlow)) { sleeptime+=50; if (sleeptime >= 5000) sleeptime=5000; } } void bounce(boolean up) { for (int i=0; i<3; i++) { xpos +=10*i; if (up) ypos -=20*i; else ypos +=20*i; repaint(); pause(sleeptime); } } void pause(int time) { try { Thread.sleep(time); } catch (InterruptedException e) { } } //Destroy the applet public void destroy() { } //Get Applet information public String getAppletInfo() { return "Applet Information"; } //Get parameter info public String[][] getParameterInfo() { return null; } }