/*
 * Oscil.java
 *
 * Created on 13 grudzień 2004, 01:28
 */

package JSciDraw.examples;
/**
 *
 * @author  Przemek
 */
import JSciDraw.interfaces.*;
import JSciDraw.physics.*;
import JSciDraw.physics.forces.*;
import JSciDraw.physics.forces.constraints.*;
import JSci.maths.*;
import java.util.*;
import java.awt.geom.*;
import java.awt.event.*;
import java.awt.image.*;
import java.awt.*;

public class rezonans_1_1 extends Frame{
      private ArrayList objects = new ArrayList();
      private Display display = new Display();
      Timer timer;
      double dt = 0.75;
      public AttachedSpring spr1,spr2,naped;      
      
      public rezonans_1_1() {
      super("Symulacja struny");
     add(display,"Center");
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent evt) {
                dispose();
                System.exit(0);
            }
        });

        
      int n=103;
      FreeSpring[] spr;
      ClassicalParticleV2D[] str;
      double[] velf;
      double[] posf;
      final double k=30;
      final double m=50;
        
        
        str =new ClassicalParticleV2D[n];
        spr =new FreeSpring[n-1];
        velf=new double[n];
        posf=new double[n];
        int licz=0;  
        double jump = (770/(n+1));
        final boolean hid=true;
        final int r=3;
        //MOJE KSZTALTY
        int j=0;
        int mid=(n-1)/2;
        //MŁOTEK
        //velf[(n-1)/2]=55;
        
        //Trojkat
        /*
        while (j<mid{
            posf[j]=j*200/mid;
            j=j+1;
        }
        while (j<n){
            posf[j]=400-j*200/mid;
            j=j+1;
        }
        */
        
        //prostokat
        /*for (int jl=0;jl<=8;jl++){
            posf[(jl+(1*mid/4)-4)]=340;
        }
        //prostokat
        for (int jl=0;jl<=8;jl++){
            posf[(jl+(14*mid/8)-4)]=-340;
        }*/
        
        
        //prostokat z prawej
        /*for (int jl=0;jl<=40;jl++){
            posf[(jl+mid+30)]=100;
        }
        */
        //rezonans
        posf[mid]=200;
        
        //koniec kształty
                
        str[0] = new ClassicalParticleV2D();
        str[n-1] = new ClassicalParticleV2D();
        
        str[0].setMass(m);
        str[0].setDrawRadius(r);
        str[0].setPosition(new Double2Vector(jump,300));
        str[0].setVelocity(new Double2Vector(0,0));
        str[0].applyConstraints(new VerticalLine());
        
        str[n-1].setMass(m);
        str[n-1].setDrawRadius(r);
        str[n-1].setPosition(new Double2Vector(n*jump,300));
        str[n-1].setVelocity(new Double2Vector(0,0));
        str[n-1].applyConstraints(new VerticalLine());
        str[0].setHidden(hid);        str[n-1].setHidden(hid);        
        spr1 = new AttachedSpring(new Double2Vector(0,300),str[0],k,jump);
        spr2 = new AttachedSpring(new Double2Vector((n+1)*jump,300),str[n-1],k,jump);
        licz=1;
        objects.add(spr1);objects.add(str[0]);
        while (licz<(mid))
        {
        str[licz] = new ClassicalParticleV2D();
        str[licz].setMass(m);
        str[licz].setDrawRadius(r);
        str[licz].applyConstraints(new VerticalLine());
        str[licz].setHidden(hid);
        spr[licz-1] = new FreeSpring(str[licz-1],str[licz],k,jump);
        //sekcja ustawiania
        str[licz].setVelocity(new Double2Vector(0,velf[licz]));          
        str[licz].setPosition(new Double2Vector((licz+1)*jump,300-posf[licz]));        
        //sekcja uswiania
        str[licz].setColor(spr[0].getColor());
        objects.add(str[licz]);
        objects.add(spr[licz-1]);
        licz=licz+1;
        }
         while (licz<(n-1))
        {
        str[licz] = new ClassicalParticleV2D();
        str[licz].setMass(m);
        str[licz].setDrawRadius(r);
        str[licz].applyConstraints(new VerticalLine());
        str[licz].setHidden(hid);
        spr[licz-1] = new FreeSpring(str[licz-1],str[licz],k,jump);
        //sekcja ustawiania
        str[licz].setVelocity(new Double2Vector(0,velf[licz]));          
        str[licz].setPosition(new Double2Vector((licz+1)*jump,300-posf[licz]));        
        //sekcja uswiania
        spr[licz-1].setColor(new Color(0,255,0));
        objects.add(str[licz]);
        objects.add(spr[licz-1]);
        licz=licz+1;
        }
        str[0].setColor(spr[0].getColor());
        str[n-1].setColor(spr[0].getColor());
        str[mid].setHidden(false);str[mid].setMass(m*2000000000);
        double a;
        a=0.53;
        naped = new AttachedSpring(new Double2Vector((mid+1)*jump,0),str[mid],2000000000*a*a,300);
        naped.setColor(new Color(255,0,0));
        timer = new Timer();
        timer.schedule(new SimulationTask(),0, 100);
        spr[n-2] = new FreeSpring(str[n-2],str[n-1],k,jump);
        objects.add(str[n-1]); objects.add(spr[n-2]);
        objects.add(spr2);
        objects.add(naped);
        
      }
    
    public static void main(String[] args) {
    
        Frame app = new rezonans_1_1();
        app.setSize(800,800);
        app.setVisible(true);
    
}
       class SimulationTask extends TimerTask {
      public void run()
        {
           Iterator i = objects.iterator();
            while(i.hasNext()) {
                  ((Moveable2D)i.next()).calculateForces(dt);
            }
            i = objects.iterator();
            while(i.hasNext()) {
                  ((Moveable2D)i.next()).move2D(dt);
            }
            display.repaint();
       }
    }
    private final class Display extends Canvas {
        private boolean firstTime = true;
        BufferedImage bi;
        Graphics2D big;
        Rectangle2D area;
        public void paint(Graphics g) {
            update(g);
        }

        public void update(Graphics g) {
            Graphics2D g2D = (Graphics2D)g;
            Drawable2D d;
            if(firstTime){
                initializeBufferedImage();
                firstTime = false;
            }
            Iterator i = objects.iterator();
            while(i.hasNext()) {
                d = (Drawable2D)i.next();
                d.erase2D(big);
                d.draw2D(big);
            }
            g2D.drawImage(bi, 0, 0, this);

        }
        private void initializeBufferedImage() {
            Dimension dim = getSize();
            int w = dim.width;
            int h = dim.height;
            area = new Rectangle2D.Double(0,0,w,h);
            bi = (BufferedImage)createImage(w, h);
            big = bi.createGraphics();
            big.setColor(Color.white);
            big.fill(area);
            //  big.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);

        }
    }
}
