/*
 * Oscil.java
 *
 * Created on 23 grudzień 2004, 13:48
 */

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

public class Field10 extends Frame{
      final double k=1;
      private ArrayList objects = new ArrayList();
      private Display display = new Display();
      Timer timer;
      double dt = 0.2;
      
      public Field10() {
        super("Rozne tlumienie");
        add(display,"Center");
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent evt) {
                dispose();
                System.exit(0);
            }
        });
        ClassicalParticleV2D c0=new ClassicalParticleV2D();
        c0.setMass(20.);
        c0.setPosition(new Double2Vector(400,20));
        c0.setVelocity(new Double2Vector(0,0));
        timer = new Timer();
        timer.schedule(new SimulationTask(),0, 100);
        objects.add(c0);
        int[] poz = {100,200,450};
        int[] pion = {10};
        double[][] coeff = {{5,5},{2,2},{0,0},{15,15}};
        GridDumping grD = new GridDumping(poz,pion,coeff);
        grD.applyField(objects);
        ConstantGravityField emf=new ConstantGravityField(new Double2Vector(0,10));
        emf.applyField(objects);
   }
    
    public static void main(String[] args) {
    
        Frame app = new Field10();
        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);
            }
            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);

        }
    }
}
