import java.awt.*; public class NewerBounce extends javax.swing.JApplet implements Runnable { Image ball; float current = (float) 0; 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(getSize().width, getSize().height); offscreen = workspace.getGraphics(); setBackground(Color.white); ball = getImage(getCodeBase(), "tennis.jpg"); } public void paint(Graphics screen) { Graphics2D screen2D = (Graphics2D) screen; height = getSize().height - ballHeight; if (yPosition == -1) yPosition = height; offscreen.setColor(Color.white); offscreen.fillRect(0,0,getSize().width,getSize().height); offscreen.drawImage(ball, (int) xPosition, (int) yPosition, this); screen2D.drawImage(workspace, 0, 0, this); } public void start() { if (runner == null) { runner = new Thread(this); runner.start(); } } public void run() { Thread thisThread = Thread.currentThread(); while (runner == thisThread) { repaint(); current += (float) 0.1; if (current > 3) current = (float) 0; xPosition += xMove; if (xPosition > (getSize().width)) xPosition = -111; double bounce = Math.sin(current) * height; yPosition = (int) (height - bounce); try { Thread.sleep(200); } catch (InterruptedException e) { } } } public void stop() { if (runner != null) { runner = null; } } public void update(Graphics screen) { paint(screen); } }