/*
 * Simulation.java
 *
 * Created on November 6, 2004, 4:36 AM
 *
 *TO NIE DZIALA !!!
 *
 */

package JSciDraw.examples;

import JSciDraw.interfaces.*;
import JSciDraw.physics.*;
import JSciDraw.physics.fields.*;
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.*;
/**
 *
 * @author  przemek
 */
public class VertPend_5 extends Frame {

    final double k=5;
    private ArrayList objects = new ArrayList();
    private Display display = new Display();
    public ClassicalParticleV2D c0=new ClassicalParticleV2D();
    public ClassicalParticleV2D c1=new ClassicalParticleV2D();
    Timer timer;// = new Timer();
    double dt = 0.07;
    /** Creates a new instance of Simulation */
    public static void main(String arg[]) {
        Frame app = new VertPend_5();
        app.setSize(800,800);
        app.setVisible(true);
    }
    public VertPend_5() {
        super("Klasyczne dudnienia");
        add(display,"Center");
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent evt) {
                dispose();
                System.exit(0);
            }
        });
        c0=new ClassicalParticleV2D();
        c0.setColor(Color.orange);
        c0.setMass(8);
        c0.setPosition(new Double2Vector(300,100));
        c0.setVelocity(new Double2Vector(0,0));
        c1=new ClassicalParticleV2D();
        c1.setColor(Color.cyan);
        c1.setMass(8);
        c1.setPosition(new Double2Vector(500,100));
        c1.setVelocity(new Double2Vector(0,0));
        c0.applyConstraints(new HorizontalLine());
        c1.applyConstraints(new HorizontalLine());
        c0.constr.setDrawable(true);
        c0.setHidden(true); 
        c1.setHidden(true);
        DrawablePendulum pend1 =new DrawablePendulum(c0,450,-20,15,false);
        pend1.setColor(Color.red);
        DrawablePendulum pend2 =new DrawablePendulum(c1,450,0,15,false);
        pend2.setColor(Color.red);
        FreeSpring spr3 = new FreeSpring(pend1.joint,pend2.joint,2,200);
        timer = new Timer();
        timer.schedule(new SimulationTask(),0, 100);
        objects.add(spr3);
        objects.add(c0); objects.add(c1);
        ConstantGravityField g0= new ConstantGravityField(new Double2Vector(0,550));
        g0.applyField(objects);
     }
    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);
             }
            i = objects.iterator();
            while(i.hasNext()) {
                d = (Drawable2D)i.next();
                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);
        }
    }
}
