import java.awt.*; import java.awt.event.*; import java.applet.Applet; public class BouncerWouter extends Applet implements Runnable { private boolean keepGoing; private Graphics g; private int x = 7, xChange = 7; private int y = 0, yChange = 2; private int diameter = 10; private int rectLeftX = 0, rectRightX = 100; private int rectTopY = 0, rectBottomY = 100; private Thread bouncerdraad = null; public void start() { keepGoing = true; if(bouncerdraad == null) { bouncerdraad = new Thread(this, "bouncer"); bouncerdraad.start(); } } public void paint(Graphics g) { g.setColor(Color.red); g.fillOval (x, y, diameter, diameter); if (x + xChange <= rectLeftX) xChange = -xChange; if (x + xChange >= rectRightX) xChange = -xChange; if (y + yChange <= rectTopY) yChange = -yChange; if (y + yChange >= rectBottomY) yChange = -yChange; x = x + xChange; y = y + yChange; } public void run() // deze methode laat de thread draaien { while (keepGoing) { repaint(); //onderstaande code zorgt voor een vertragingseffect anders gaat het balletje veel te snel try { Thread.sleep(50); // de thread van de stuiterende bal slaapt 50 ms // hierdoor krijgt de thread van de gebruikersinterface // wat processortijd om te draaien } catch (InterruptedException e) { System.err.println("sleep exception"); } } } public void stop() { keepGoing = false; } }