import java.applet.*; import java.awt.*; public class MoveBall extends Applet implements Runnable { // Initialisierung der Variablen int x_pos = 10; // x - Position des Balles int y_pos = 100; // y - Position des Balles int radius = 20; // Radius des Balles int moveBy = 1; // amount to moveBy static final int WIDTH = 200; // width of the canvas static final int HEIGTH = 200; // height of the canvas public void init() { setBackground (Color.blue); } public void start () { // Schaffen eines neuen Threads, in dem das Spiel läuft Thread th = new Thread (this); // Starten des Threads th.start (); } public void stop() { } public void destroy() { } public void run () { Thread.currentThread().setPriority(Thread.MIN_PRIORITY); while (true) { if (x_pos >= WIDTH) moveBy = -1; if (x_pos <= 30) moveBy = 1; x_pos = x_pos + moveBy; repaint(); try { Thread.sleep (20); } catch (InterruptedException ex) { } Thread.currentThread().setPriority(Thread.MAX_PRIORITY); } } public void paint (Graphics g) { g.setColor (Color.red); g.fillOval (x_pos - radius, y_pos - radius, 2 * radius, 2 * radius); } }