/*
 * 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 JSci.maths.*;
import java.util.*;
import java.awt.geom.*;
import java.awt.event.*;
import java.awt.image.*;
import java.awt.*;

public class Fantastic extends Frame{
      public AttachedSpring spr1,spr2,spr3,spr4;
      public FreeSpring sprF1,sprF2,sprF3,sprF4;
      final double k=5;
      private ArrayList objects = new ArrayList();
      private Display display = new Display();
      Timer timer;
      double dt = 0.3;
      
      public Fantastic() {
        super("Widac dudnienia!! (slabe sprzezenia)");
        add(display,"Center");
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent evt) {
                dispose();
                System.exit(0);
            }
        });
        ClassicalParticleV2D c0=new ClassicalParticleV2D();
        c0.setMass(10*k);
        c0.setPosition(new Double2Vector(300,250));
        c0.setVelocity(new Double2Vector(0,0));
        ClassicalParticleV2D c1=new ClassicalParticleV2D();
        c1.setMass(10*k);
        c1.setPosition(new Double2Vector(300,450));
        c1.setVelocity(new Double2Vector(-20,20));
        ClassicalParticleV2D c2=new ClassicalParticleV2D();
        c2.setMass(10*k);
        c2.setPosition(new Double2Vector(500,250));
        c2.setVelocity(new Double2Vector(0,0));
        ClassicalParticleV2D c3=new ClassicalParticleV2D();
        c3.setMass(10*k);
        c3.setPosition(new Double2Vector(500,450));
        c3.setVelocity(new Double2Vector(0,0));
        spr1 = new AttachedSpring(new Double2Vector(100,50),c0,2*k,200*Math.sqrt(2));
        spr2 = new AttachedSpring(new Double2Vector(100,650),c1,2*k,200*Math.sqrt(2));
        spr3 = new AttachedSpring(new Double2Vector(700,50),c2,2*k,200*Math.sqrt(2));
        spr4 = new AttachedSpring(new Double2Vector(700,650),c3,2*k,200*Math.sqrt(2));
        sprF1= new FreeSpring(c0,c1,0.4*k,200);
        sprF2= new FreeSpring(c1,c3,0.4*k,200);
        sprF3= new FreeSpring(c0,c2,0.4*k,200);
        sprF4= new FreeSpring(c2,c3,0.4*k,200);

        timer = new Timer();
        timer.schedule(new SimulationTask(),0, 100);
        objects.add(spr1);objects.add(spr2);
        objects.add(spr3);objects.add(spr4);
        objects.add(sprF1);objects.add(sprF2);
        objects.add(sprF3);objects.add(sprF4);
        objects.add(c0);objects.add(c1);        
        objects.add(c2);objects.add(c3);

      }
    
    public static void main(String[] args) {
    
        Frame app = new Fantastic();
        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);

        }
    }
}
