/* * * Copyright (c) 1996 - 1998 * Frank Buss (fb@frank-buss.de), Stephan Schloepke (stephan@nordrhein.net) * * This applet was published in the book: * * "Programmier Training Java, In 15 Stunden topfit" * ISBN 3-8158-1303-4 * Authors: Frank Buss, Stephan Schloepke * Data Becker Verlage (http://www.databecker.de) * * You can get this applet and more at http://www.frank-buss.de * */ // // Animation - Beispiel Applet für Animationen mit Thread, // Double-Buffering und Daumenkinoeffekt // import java.applet.*; import java.awt.*; public class Animation extends Applet implements Runnable { int X, Y; double SinX=0.0, SinY=0.0; double SinOffX=1 * Math.PI / 180, SinOffY=3 * Math.PI / 180; Thread Anim; private Image Buffer; private Graphics gBuffer; private Image Kugeln[]; private int KugelNr=0; private int WarteZähler=0; public void start() { if (Anim == null) { Anim = new Thread (this); Anim.start(); } } public void stop() { if (Anim != null) { Anim.stop(); Anim = null; } } public void run() { while (true) { try { Anim.sleep (10); } catch (Exception e) { } repaint(); } } public void update(Graphics g) { paint(g); } public Image ladeBild(String bild) { Image image=getImage(getDocumentBase(),bild); MediaTracker tracker=new MediaTracker(this); tracker.addImage(image,0); try { tracker.waitForID(0); } catch (InterruptedException e) { } return image; } public void init() { setBackground(Color.white); Kugeln=new Image[8]; for (int c=0;c<8;c++) Kugeln[c]=ladeBild("kugel"+c+".gif"); Buffer=createImage(size().width,size().height); gBuffer=Buffer.getGraphics(); gBuffer.setColor(getBackground()); gBuffer.fillRect(0,0,size().width,size().height); } public void paint(Graphics g) { g.drawImage (Buffer,0,0, this); gBuffer.setColor(getBackground()); gBuffer.fillRect(X,Y,Kugeln[KugelNr].getWidth(this),Kugeln[KugelNr].getHeight(this)); X = 190 + (int) (170 * Math.sin (SinX)); SinX += SinOffX; Y = 110 - (int) (Math.abs (110 * Math.sin (SinY))); SinY += SinOffY; WarteZähler--; if (WarteZähler<=0) { KugelNr++; if (KugelNr>=8) KugelNr=0; WarteZähler=4; } gBuffer.drawImage(Kugeln[KugelNr],X, Y, this); } }