Programlama dilleri ve veri tabanları ile ilgili ipuçları

12/2/2009 - İki cismin çarpışmasına örneklerle ulaşmak: Örnek2

Kategori: Programming


/**
 *
 * @author KAYWINSOFT
 */
import java.applet.Applet;
import java.awt.*;

public class ProfPendulum extends Applet implements Runnable
{
  public Thread myThread = null;
  Image hiddenImage;
  Graphics hiddenGraphics;
  Font font = new Font("Times Roman", Font.BOLD, 18);

  int step = 0;
  int tableSetUp = 0;
  int oldSecond = 0;

  int radius = 95;

  double[] sine       = new double[20]; // sine values every 18 degrees
  double[] sineScaled = new double[20]; // sine values / 3
  double[] pendulumX  = new double[20];
  double[] pendulumY  = new double[20];
  double[] beamXLeft  = new double[20];
  double[] beamYLeft  = new double[20];
  double[] beamXRight = new double[20];
  double[] beamYRight = new double[20];
  double[] stopXRight = new double[20];
  double[] stopYRight = new double[20];
  double[] stopXLeft  = new double[20];
  double[] stopYLeft  = new double[20];

  int[] pendulumXabs  = new int[20];
  int[] pendulumYabs  = new int[20];
  int[] beamXLeftAbs  = new int[20];
  int[] beamYLeftAbs  = new int[20];
  int[] beamXRightAbs = new int[20];
  int[] beamYRightAbs = new int[20];
  int[] stopXLeftAbs  = new int[20];
  int[] stopYLeftAbs  = new int[20];
  int[] stopXRightAbs = new int[20];
  int[] stopYRightAbs = new int[20];

  int[] spokeAngleX   = new int[66];
  int[] spokeAngleY   = new int[66];

 

 

  // pendulum
  int PIVOTX     = 100;
  int PIVOTY     = 117;
  int PENDLENGTH = 250;
  int BEAMWIDTHL =  35;
  int BEAMWIDTHR =  45;
  int SLOPE      =   8; // this puts slope on stop at left of beam



  public void init()
  {
    hiddenImage = createImage(400,400);
    hiddenGraphics = hiddenImage.getGraphics();
    fillSpokeAngleTable( radius );
  }


  public void fillSpokeAngleTable( int radius )
  { // this is filled during initialisation
    for(int x = 0; x <= 65; x++)
    {
      spokeAngleX[x] = (int)(radius * Math.sin( x * 2 * Math.PI/60));
      spokeAngleY[x] = (int)(radius * Math.cos( x * 2 * Math.PI/60));
    }
  }


  public synchronized void update(Graphics g)
  {
    // could check and only do this if its changed
    buildBuffer(hiddenGraphics);

    // paint the buffer to the screen
    paint(g);
  }


  public void paint (Graphics g)
  {
    g.drawImage(hiddenImage,0,0,this);
    g.drawString("PENDULUM", 50,50);
  }


  public void start()
  {
    if (myThread == null) myThread = new Thread(this);
    myThread.start();
  }


  public void stop()
  {
    myThread.stop();
    myThread = null;  // ...or else the next start() will break
  }


  public void run()
  {
    while (true)
    {
      try{myThread.sleep(100);}
      catch (InterruptedException e){ }
      step++;
      if(step >= 20) step = 0;
      repaint();
    }
  }


  private void buildBuffer(Graphics g)
  {
   

    // yellow background
    g.setColor(Color.yellow);
    g.fillRect(0,0,size().width, size().height);

    // red border
    g.setColor(Color.red);
    g.drawRect(0,0,size().width - 1, size().height -1);
    g.drawRect(1,1,size().width - 3, size().height -3);

    drawPendulum( g, PENDLENGTH,BEAMWIDTHL,BEAMWIDTHR,SLOPE,PIVOTX,PIVOTY);
 
  }

  public void drawPendulum(Graphics g,
                           int pendLength,
                           int beamWidthL,
                           int beamWidthR,
                           int slope, // puts a slope on left stop
                           int pivotX,
                           int pivotY)
  {
    if(tableSetUp == 0) fillPendulumTable(pendLength,
                                          pivotX,
                                          pivotY,
                                          beamWidthL,
                                          beamWidthR);
    tableSetUp = 1;
    // pendulum is pivotted at point pivotX and pivotY
    // it has a beam across the top with different left and right lengths
    g.setColor(Color.red);
    // pendulum
    g.drawLine(pivotX, pivotY, pendulumXabs[step], pendulumYabs[step]);
    g.fillOval(pendulumXabs[step] - 5, pendulumYabs[step] - 5, 10, 10 );

    // beam
    g.drawLine(beamXLeftAbs[step],
               beamYLeftAbs[step],
               beamXRightAbs[step],
               beamYRightAbs[step] );

    // stop (right)
    g.drawLine(beamXRightAbs[step],
               beamYRightAbs[step],
               stopXRightAbs[step],
               stopYRightAbs[step] );
     // stop (left)
    g.drawLine(beamXLeftAbs[step],
               beamYLeftAbs[step],
               stopXLeftAbs[step] - slope,
               stopYLeftAbs[step] );
  }


  // This fills two arrays with the x,y positions of the pendulum bottom.
  public void fillPendulumTable( int length,
                                 int pivotX,
                                 int pivotY,
                                 int beamWidthL,
                                 int beamWidthR )
  {
    for (int x = 0; x <=19; x++)
    {
      // sine values, 0 to 360degrees
      sine[x] = Math.sin(x * 2.0 * Math.PI / 20);
      // sine values multiplied by the max swing
      // this is the angle per tenth of a second in radians
      sineScaled[x] = sine[x] * 0.3;//0.3=swing in rads,about +-57* .3=17deg
      // pendulum x and y values
      pendulumX[x] = Math.sin(sineScaled[x]);
      pendulumY[x] = Math.cos(sineScaled[x]);
      //beam right
      beamXRight[x] = Math.sin(sineScaled[x] + (Math.PI/2) );
      beamYRight[x] = Math.cos(sineScaled[x] + (Math.PI/2) );
      //beam left
      beamXLeft[x] = Math.sin(sineScaled[x] - (Math.PI/2) );
      beamYLeft[x] = Math.cos(sineScaled[x] - (Math.PI/2) );
      // stops: Math.PI/5 is an arbitrary angle of 72 degrees
      // stop right
      stopXRight[x] = Math.sin(sineScaled[x] + (Math.PI/2) - Math.PI/5 );
      stopYRight[x] = Math.cos(sineScaled[x] + (Math.PI/2) - Math.PI/5 );
      // stop left
      stopXLeft[x] = Math.sin(sineScaled[x] - (Math.PI/2) + Math.PI/5 );
      stopYLeft[x] = Math.cos(sineScaled[x] - (Math.PI/2) + Math.PI/5 );


      // absolute values
      // pendulum
      pendulumXabs[x] = pivotX + (int)(length * pendulumX[x]);
      pendulumYabs[x] = pivotY + (int)(length * pendulumY[x]);
      // beam
      beamXLeftAbs[x] = pivotX + (int)(beamWidthL * beamXLeft[x]);
      beamYLeftAbs[x] = pivotY + (int)(beamWidthL * beamYLeft[x]);
      beamXRightAbs[x] = pivotX + (int)(beamWidthR * beamXRight[x]);
      beamYRightAbs[x] = pivotY + (int)(beamWidthR * beamYRight[x]);
      stopXLeftAbs[x] = pivotX + (int)(beamWidthL * stopXLeft[x]);
      stopYLeftAbs[x] = pivotY + (int)(beamWidthL * stopYLeft[x]);
      stopXRightAbs[x] = pivotX + (int)(beamWidthR * stopXRight[x]);
      stopYRightAbs[x] = pivotY + (int)(beamWidthR * stopYRight[x]);
    }
  }
 
}



Gerçekten profosyenelce öyle değil mi? [Katkılarından dolayı Ali'ye teşekkürler.]




EkleBunu Sosyal Paylaşım Butonu
Yorum yaz!

<- Son Sayfa :: Sonraki Sayfa ->

Hakkımda

Kategoriler

Arkadaşlarım