import java.awt.*; import java.applet.Applet; public class flimmer extends Applet implements Runnable { private Thread animatorThread = null; private int posX, posY; // Position private int incx=1, incy=1; // Richtungszuwachs private final int rad = 20; private Image offScreenBuffer; private Graphics offScreenGraphics; // Grafik kontext für den offScreenBuffer. public void init(){ offScreenBuffer = this.createImage(size().width , size().height); // Wir müssen unser Buffer-Image als Graphics object benutzen: offScreenGraphics = offScreenBuffer.getGraphics(); } // Überschreiben der Methode Paint() public void paint(Graphics g) { g.setColor(Color.black); g.fillRect(0,0,size().width, size().height); //g.clearRect(0,0,size().width, size().height); g.setColor(Color.cyan); g.create().fillOval(posX-rad-3*incx ,posY-rad-3*incy ,rad*2 ,rad*2 ); g.setColor(Color.blue); g.create().fillOval(posX-rad ,posY-rad ,rad*2 ,rad*2 ); } public void run() { while (Thread.currentThread() == animatorThread){ posX+= incx; posY+= incy; if ((posX <= rad) || (posX >= size().width-rad)) incx*=-1; if ((posY <= rad) || (posY >= size().height-rad)) incy*=-1; repaint(); try { Thread.sleep(10); } catch(InterruptedException e){ showStatus("Error: " + e.toString()); } } } public void start() { if(animatorThread == null) { animatorThread = new Thread(this, "Test Thread"); posX = (int)(size().width/2); posY = (int)(rad*2); incx = (Math.random() < 0.5) ? 2 : -2; incy = (Math.random() < 0.5) ? 2 : -2; animatorThread.start(); } } public void stop() { animatorThread = null; } }