import java.applet.*; import java.awt.*; public class BounceGIF extends Applet implements Runnable { int x = 100, y = 0, dx = 10, dy = 10, maxx, maxy, sizex, sizey; int interval; String imagefile; Image img; Thread th; int Nbounce; double accele = 0.8; double speed = 0; double refrec = 0.8; public void init() { String param; param = getParameter("interval"); interval = (param != null) ? new Integer(param).intValue() : 50; param = getParameter("image"); imagefile = (param != null) ? param : "asakawa.gif"; param = getParameter("dx"); dx = (param != null) ? new Integer(param).intValue() : 3; param = getParameter("dy"); dy = (param != null) ? new Integer(param).intValue() : 10; param = getParameter("accele"); accele = (param != null) ? Float.valueOf(param).floatValue() : 0.8; param = getParameter("refrec"); refrec = (param != null) ? Float.valueOf(param).floatValue() : 0.8; img = getImage(getCodeBase(), imagefile); maxx = size().width; maxy = size().height; setBackground(Color.black); } public void start() { if (th == null) { resize(maxx, maxy); th = new Thread(this); th.start(); } } public void stop() { if (th != null) { th.stop(); th = null; } } public void paint(Graphics g) { sizex = img.getWidth(this); sizey = img.getHeight(this); g.drawImage(img, x, y, this); } /* public void update(Graphics g) { paint(g); } */ public void run() { while (th != null) { // if (Nbounce > 10) { if (Nbounce > 2) { // th.stop(); // th = null; try { Thread.sleep(3000); } catch(InterruptedException e) { break; } speed = -15.0; } try { Thread.sleep(interval); // sleep for interval msec } catch (InterruptedException e) { break; } speed += accele; y += (int)speed; if ( y > (maxy - sizey)) { y = (maxy - sizey); speed = speed * -refrec; } if ( y == (maxy - sizey)) { ++Nbounce; } else { Nbounce = 0; } if (x <= 0 || x >= (maxx - sizex) ) dx = -dx; if ( y <= 0 ) y = 0; x += dx; repaint(); } } }