import java.util.*;
import java.awt.Graphics;
import java.awt.Graphics2D;

public class BlockSpringConfiguration  {
   private LabPanel labPanel;
   private Vector <PhysicsElement> phyElements; // Vector is a Container
         // it is like an array, but we can't use [i] for accessing.
         // it can grow as we need it.
   public BlockSpringConfiguration (){
      phyElements = new Vector <PhysicsElement>();
   }
   public void setLabPanel( LabPanel lp){
      labPanel = lp;
   }
   public void computeNextState(float delta_t) {
      for (int i=0; i<phyElements.size(); i++)
         phyElements.get(i).computeNextState(delta_t);
   }
   public void updateState() {
      for (int i=0; i<phyElements.size(); i++)
         phyElements.get(i).updateState();
      if (labPanel != null)
         labPanel.repaint();
   }
   public void add(PhysicsElement e) {
      phyElements.add(e);
      if (labPanel != null)
         labPanel.repaint();
   }
   public void remove(PhysicsElement e) {
      phyElements.remove(e);
      if (labPanel != null)
         labPanel.repaint();
   }
   public void paintComponent(Graphics2D g2d){
      for (int i=0; i<phyElements.size(); i++)
         if (phyElements.get(i) instanceof Drawable)
            ((Drawable) phyElements.get(i)).draw(g2d);
   }
   public Draggable find(int x, int y) {
      for (int i=0; i<phyElements.size(); i++)
         if (phyElements.get(i) instanceof Draggable) {
            Draggable element =(Draggable) (phyElements.get(i));
            if (element.contains(x,y)) return element;
         }
      return null;
   }  
   public GBlock findBlock(double x, double y) {
      for (int i=0; i<phyElements.size(); i++)
         if (phyElements.get(i) instanceof GBlock) {
            GBlock block =(GBlock) (phyElements.get(i));
            if (block.contains(x,y)) return block;
         }
      return null;
   }  
}