import java.awt.*; import java.applet.Applet; public class Kommunikation extends Applet implements Runnable { private Thread animatorThread = null; private int posX, posY; // Position private int incx=1, incy=1; // Richtungszuwachs private 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(); posX = (int)(size().width/2); posY = (int)(rad*2); incx = -1; incy = -1; } public void Ballsize(float faktor){ rad = (int)(rad*faktor); if (rad<5) rad=5; } public void update(Graphics g){ paint(offScreenGraphics); // gibt den off-screen buffer und die "paint" Methode, // die jetzt genauso auf den Buffer Zeichnet wie sonst auf den Bildschrim g.drawImage(offScreenBuffer, 0, 0, this); // Jetzt werden die Grafiken auf den grafikkontext vom // Browser gezeichnet } public void paint(Graphics g) { // Überschreiben der Methode Paint() //Graphics2D g2 = (Graphics2D) g; //g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON); g.setColor(Color.black); g.fillRect(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 start() { if(animatorThread == null) { animatorThread = new Thread(this); animatorThread.start(); } } public void stop() { animatorThread = null; } public void run() { while (Thread.currentThread() == animatorThread){ posX+= incx; posY+= incy; if (posX <= rad) incx =1; if (posX >= size().width-rad) incx =-1; if (posY <= rad) incy =1; if (posY >= size().height-rad)incy =-1; repaint(); try { Thread.sleep(10); } catch(InterruptedException e){ showStatus("Error: " + e.toString()); } } } }