import java.applet.*;
import java.awt.*;
 

public class HalloDukeNeu extends Applet implements Runnable {
   static Thread thread = null;
   static private DasApplet applet;
   static private Checkbox mit;
   static private Checkbox ohne;
   
   static MediaTracker tracker;
   static Image[] bild;
   static int akt = 0;
   static int anz[] = { 17, 10 };

   static AudioClip sound;
   static String soundname;
   static CheckboxGroup g;
   static boolean mitSound = false;
   static boolean ton_an = false;
  
   static int lauf = 6;   
   static int x = 400;
   static int breite = 60;
   static int warte = 120;

   public void init() {
      String name[] = new String[2];

      name[0] = getParameter ( "name1" );
      name[1] = getParameter ( "name2" );
      soundname = getParameter ( "sound" );     

      bild = new Image[anz[0]+anz[1]];
      tracker = new MediaTracker ( this );
      for ( int j = 0; j < 2; j++ ) {
         for ( int i = 0; i < anz[j]; i++ ) {
            bild[i+j*anz[0]] = getImage ( getDocumentBase(), name[j] + (i+1) + ".gif" );
            tracker.addImage ( bild[i+j*anz[0]], 0 );
         }
      }

      Panel p = new Panel();
      p.setLayout ( new FlowLayout() );
      g = new CheckboxGroup();
      p.add ( mit = new Checkbox ( "mit Sound", g, false ) );
      p.add ( ohne = new Checkbox ( "ohne Sound", g, true ) );
      add ( "Right", p );
      applet = new DasApplet ( size() );
      add ( "Center", applet );
   }

   public boolean action ( Event e, Object o ) {
      if ( e.target.equals ( ohne ) )
         ton_an = false;
      else
         if ( e.target.equals ( mit ) ) {
            if ( !mitSound ) {
               sound = getAudioClip ( getDocumentBase(), soundname );
               mitSound = true;
            }
            ton_an = true;
         }
         else
            return super.action ( e, o );
      return true;      
   }

   public void start() {
      try { tracker.waitForAll(); }
      catch ( InterruptedException e ) {}

      if ( thread == null ) {
         thread = new Thread ( this );
         thread.start();
      }
   }

   public void stop() {
      if ( thread != null ) {
         thread.stop();
         thread = null;
      }
   }

   public boolean mouseDown ( Event e, int x, int y ) {
      ton_an = !ton_an;
      return true;
   }
      
   public void run() {
      boolean hin = false;

      while ( true ) {
         hin = !hin;

         for ( int i = 0; i < lauf*17+10; i++ ) {

            if ( i%17 == 0 ) {
               if ( hin ) { if ( i < lauf*17 ) x -= breite; }
               else { if ( i != 0 ) x += breite; }
            }
            if ( i < 17*lauf ) {
               if ( hin ) akt = i % 17;
               else akt = 17 - i % 17;
            }
            else { akt = i - 17*(lauf-1); }

            if ( i == (lauf-1)*17+14  &&  ton_an ) sound.play();
            applet.repaint();
            try {
                thread.sleep ( warte );
            }
            catch ( InterruptedException e );
         }

      }
   }

}

class DasApplet extends Canvas {
   Dimension d;
   
   public DasApplet ( Dimension d ) {
      this.d = d;
   }
   
   public Dimension preferredSize() {
      return d;
   }
   
   public Dimension minimumSize() {
      return d;
   }
   
   public void paint ( Graphics g )
   {
      g.drawImage ( HalloDukeNeu.bild[HalloDukeNeu.akt], HalloDukeNeu.x, 0, this );
   }
}

