import java.awt.*; public class NewBounce extends java.applet.Applet implements Runnable { Image ball; float current = (float) 0; float strength = (float) 1; Thread runner; int xPosition = 10; int xMove = 1; int yPosition = -1; int ballHeight = 102; int ballWidth = 111; int height; Image workspace; Graphics offscreen; public void init() { workspace = createImage(size().width, size().height); offscreen = workspace.getGraphics(); setBackground(Color.white); ball = getImage(getCodeBase(), "tennis.jpg"); } public void paint(Graphics screen) { height = size().height - ballHeight; if (yPosition == -1) yPosition = height; offscreen.setColor(Color.white); offscreen.fillRect(0,0,size().width,size().height); offscreen.drawImage(ball, (int) xPosition, (int) yPosition, this); screen.drawImage(workspace, 0, 0, this); } public void start() { if (runner == null) { runner = new Thread(this); runner.start(); } } public void run() { while (true) { repaint(); current += (float) 0.1; if (current > 3) { current = (float) 0; strength = (float)(strength * 0.9); } xPosition += xMove; if (xPosition > (size().width - 111)) xMove *= -1; if (xPosition < 1) xMove *= -1; double bounce = Math.sin(current) * height * strength; yPosition = (int) (height - bounce); try { Thread.sleep(200); } catch (InterruptedException e) { } } } public void stop() { if (runner != null) { runner.stop(); runner = null; } } public void update(Graphics screen) { paint(screen); } }