// ------  Simulation einer Hantel  -----------

import java.applet.*;
import java.awt.*;
class Hantel
{  double r;                 // Kugelradius  
   double s;                 // Abstand der Kugeln von S
   double xs, ys;            // Schwerpunkt-Koord.  
   double phi;               // Richtung der Achse (Winkel zur x-Achse)
   double vx, vy, omega;     // Schwerpunktgeschw., Winkelgeschwindigkeit
   double ax, ay;            // Schwerpunktbeschleunigung


   Hantel(double r, double s, double xs, double ys,
          double phi, double vx, double vy, double omega, double ax, double ay)
   {  this.r = r;
      this.s = s;
      this.xs = xs;
      this.ys = ys;
      this.phi = phi;
      this.vx = vx;
      this.vy = vy;
      this.omega = omega;
      this.ax = ax;
      this.ay = ay;
   }
   
   
   void move(double dt)
   {  double dy = Math.abs(s*Math.sin(phi));
      if ( ys-dy <= r )                   
      {  vy = -vy;                                       // Stoss
         ys = dy + r;  
      }   
      xs += vx*dt;
      ys += vy*dt;
      phi += omega*dt;
      vx += ax*dt;
      vy += ay*dt;
   }
   
   
   void zeichne(Graphics g, Graph2d graph2d)
   {  double dx = s*Math.cos(phi);             
      double dy = s*Math.sin(phi);
      double x1 = xs - dx;                              // Kugel-Mittelpunkt
      double y1 = ys - dy;
      double x2 = xs + dx;
      double y2 = ys + dy;
      int xx1 = graph2d.pixCol(x1);                     // Pixel-Koordinaten
      int yy1 = graph2d.pixLine(y1);
      int xx2 = graph2d.pixCol(x2);
      int yy2 = graph2d.pixLine(y2);
      int rr = graph2d.pixCol(r)-graph2d.pixCol(0);     
      g.fillOval(xx1-rr, yy1-rr, 2*rr, 2*rr);           // Kugeln
      g.fillOval(xx2-rr, yy2-rr, 2*rr, 2*rr);
      g.drawLine(xx1, yy1, xx2, yy2);                   // Stange (aus 5 Linien)
      g.drawLine(xx1+1, yy1, xx2+1, yy2);
      g.drawLine(xx1-1, yy1, xx2-1, yy2);
      g.drawLine(xx1, yy1+1, xx2, yy2+1);
      g.drawLine(xx1, yy1-1, xx2, yy2-1);
   }
}


public class HantelSim extends Anim
{

  Graph2d graph2d;
  Dimension dim; 
  Image memScreen;                                      // Offline-Screen
  Graphics memGr;                                       // Graphics-Objekt
  Hantel hantel;                                        // Hantel


  public void init()
  {  setBackground(Color.black);
     dim = getSize();
     memScreen = createImage(dim.width, dim.height);
     memGr = memScreen.getGraphics();
     graph2d = new Graph2d(dim.width, dim.height);
     graph2d.setDisplayRange(-12, 12,                   // x-Koordinatenbereich
                              0, 18);                   // y-Koordinatenbereich
     hantel = new Hantel(0.3, 1.6,                      // r, s
                         0, 16, 0.9,                    // xs, ys, phi
                         0, -0.2, 0.1,                  // vx, vy, omega
                         0, -0.02);                     // ax, ay
  }


  public void paint(Graphics g)
  {  memGr.setColor(Color.black);
     hantel.zeichne(memGr, graph2d);                    // altes Bild loeschen       
     hantel.move(0.2);                                  // Bewegungsschritt
     memGr.setColor(Color.blue);    
     hantel.zeichne(memGr, graph2d);
     g.drawImage(memScreen, 0, 0, null);
  }

}
