Browse Source

Bodies don't reset after subnet change + scaleable

jess 7 years ago
parent
commit
1d8e6ced5a

+ 7 - 5
src/classes/HolonBody.java

@@ -9,10 +9,12 @@ public class HolonBody implements Comparable<HolonBody> {
 	public Vector2d position;
 	private float mass;
 	private float radius;
-	private float angularVel;
-	private float orientation = 45f;
 	private Color color;
 
+	public Color getColor() {
+		return color;
+	}
+
 	public HolonBody(float x, float y, float radius, float mass, Color color) {
 		this.velocity = new Vector2d(0, 0);
 		this.position = new Vector2d(x, y);
@@ -22,8 +24,8 @@ public class HolonBody implements Comparable<HolonBody> {
 	}
 
 	
-	public void draw(Graphics2D g2) {
-
+	public void draw(Graphics2D g2, int holonBodyScale) {
+		
 		g2.setColor(color);
 		g2.fillOval((int) (position.getX() - getRadius()), (int) (position.getY() - getRadius()),
 				(int) (2 * getRadius()), (int) (2 * getRadius()));
@@ -108,7 +110,7 @@ public class HolonBody implements Comparable<HolonBody> {
 
 	}
 
-	private void setMass(float mass) {
+	public void setMass(float mass) {
 		this.mass = mass;
 	}
 

+ 20 - 0
src/ui/controller/Control.java

@@ -796,4 +796,24 @@ public class Control {
 	public int getNumberStateObjects(ArrayList<AbstractCpsObject> list, int state) {
 		return objectController.getNumberStateObjects(list, state);
 	}
+
+	/**
+	 * Changes the value of HolonBodySCALE 
+	 * 
+	 * @param s
+	 *            HolonBodyScale
+	 */
+	public void setHolonBodyScale(int s) {
+		globalController.setHolonBodyScale(s);
+	}
+	
+	/**
+	 * Returns HolonBodySCALE.
+	 * 
+	 * @return SCALE
+	 */
+	public int getHolonBodyScale() {
+		return globalController.getHolonBodyScale();
+	}
+
 }

+ 19 - 0
src/ui/controller/GlobalController.java

@@ -129,8 +129,27 @@ public class GlobalController {
 		model.addSubNetColor(c);
 	}
 	
+	
 	public void setMaxCapacity(float cap) {
 		model.setMaxCapacity(cap);
 	}
 
+	/**
+	 * Changes the value of HolonBodySCALE 
+	 * 
+	 * @param s
+	 *            HolonBodyScale
+	 */
+	public void setHolonBodyScale(int s) {
+		model.setHolonBodyScale(s);
+	}
+
+	/**
+	 * Returns HolonBodySCALE.
+	 * 
+	 * @return HolonBodySCALE
+	 */
+	public int getHolonBodyScale() {
+		return model.getHolonBodyScale();
+	}
 }

+ 20 - 0
src/ui/model/Model.java

@@ -37,6 +37,7 @@ public class Model {
 	private int canvasY = 1000;
 	private static int sCALE = 50; // Picture Scale
 	private static int sCALEdIV2 = sCALE / 2;
+	private static int holonBodysCALE = 100; // Picture Scale
 	private static final int ITERATIONS = 100;
 	private int curIteration = 0;
 	private LinkedList<Color> subNetColors = new LinkedList<>();
@@ -727,5 +728,24 @@ public class Model {
 	public void setTableHolonElement(JTable tableHolonElement) {
 		this.tableHolonElement = tableHolonElement;
 	}
+	
+	/**
+	 * Sets the HolonBody Scale.
+	 * 
+	 * @param scale
+	 *            for the HolonBody
+	 */
+	public void setHolonBodyScale(int scale) {
+		holonBodysCALE = scale;
+	}
+
+	/**
+	 * Returns the sCale (Scale for the Images).
+	 * 
+	 * @return sCALE
+	 */
+	public int getHolonBodyScale() {
+		return holonBodysCALE;
+	}
 
 }

+ 22 - 0
src/ui/view/GUI.java

@@ -83,6 +83,7 @@ import interfaces.CategoryListener;
 import ui.controller.Control;
 import ui.controller.UpdateController;
 import ui.model.Model;
+import java.awt.Label;
 
 /**
  * Graphical User Interface.
@@ -266,6 +267,9 @@ public class GUI<E> implements CategoryListener {
 	private String selectObjBeforeErase = "Please select a Category or an Object in order to delete something.";
 
 	private UpdateController updCon;
+	private final JSplitPane splitPane_1 = new JSplitPane();
+	private final JSlider holonBodySizeSlider = new JSlider();
+	private final JLabel lblHolonBodySize = new JLabel("HolonBody SIze");
 
 	/**
 	 * Create the application.
@@ -660,6 +664,24 @@ public class GUI<E> implements CategoryListener {
 		splitPane3.setRightComponent(sizeSlider);
 
 		splitPane3.setLeftComponent(lblImageSize);
+		
+		mnNewMenuView.add(splitPane_1);
+		holonBodySizeSlider.setValue(100);
+		holonBodySizeSlider.setMinimum(1);
+		
+		holonBodySizeSlider.addChangeListener(new ChangeListener() {
+			
+			@Override
+			public void stateChanged(ChangeEvent e) {
+				controller.setHolonBodyScale(holonBodySizeSlider.getValue());
+				tree.setRowHeight(50);
+				canvas.repaint();				
+			}
+		});
+		
+		splitPane_1.setRightComponent(holonBodySizeSlider);
+		
+		splitPane_1.setLeftComponent(lblHolonBodySize);
 
 		menuBar.add(mnHelp);
 

+ 47 - 19
src/ui/view/HolonCanvas.java

@@ -27,7 +27,6 @@ public class HolonCanvas extends JPanel {
 
 	// Ball objects
 	private ArrayList<HolonBody> bodies = new ArrayList<>();
-	private HolonBody currentBall;
 	private int subCount;
 
 	// Frames
@@ -64,8 +63,14 @@ public class HolonCanvas extends JPanel {
 		if (!controller.getSimManager().getSubNets().equals(subnets)) {
 			subnets = controller.getSimManager().getSubNets();
 			subCount = subnets.size();
-			bodies = new ArrayList<>();
-			createBodies(subCount);
+			// bodies = new ArrayList<>();
+			// createBodies(subCount);
+			calcCenter();
+			if (bodies.isEmpty()) {
+				createBodies(subCount);
+			} else {
+				addNewBodies(subCount);
+			}
 		}
 
 		currentTime = System.currentTimeMillis();
@@ -93,12 +98,37 @@ public class HolonCanvas extends JPanel {
 		repaint();
 	}
 
-	
-	private void createBodies(int subCount) {
-		calcCenter();
+	// updates the bodies according to the changes of subnets
+	private void addNewBodies(int subCount) {
 		
-		for(int i = 0; i<subCount; i++){
-			HolonBody temp = new HolonBody(center.width+i, center.height+i, subnets.get(i).getObjects().size()*5+10, subnets.get(i).getObjects().size()*5+10, model.getSubNetColors().get(i));
+		ArrayList<HolonBody> newBodies = new ArrayList<>();
+		for (int i = 0; i < bodies.size(); i++) {
+			for (int j = 0; j < subCount; j++) {
+				if (model.getSubNetColors().get(j) == bodies.get(i).getColor()) {
+					bodies.get(i).setRadius(subnets.get(j).getObjects().size() * 5 + 10);
+					bodies.get(i).setMass(subnets.get(j).getObjects().size() * 5 + 10);
+					newBodies.add(bodies.get(i));
+					break;
+				}
+			}
+		}
+
+		bodies = newBodies;
+		for (int i = bodies.size(); i < subCount; i++) {
+			HolonBody temp = new HolonBody((center.width + 1) + (-1 * i), (center.height + 1) + (-1 * i),
+					subnets.get(i).getObjects().size() * 5 + 10, subnets.get(i).getObjects().size() * 5 + 10,
+					model.getSubNetColors().get(i));
+			bodies.add(temp);
+		}
+
+	}
+
+	// creates the first round of bodies
+	private void createBodies(int subCount) {
+		for (int i = 0; i < subCount; i++) {
+			HolonBody temp = new HolonBody((center.width + 1) + (-1 * i), (center.height + 1) + (-1 * i),
+					subnets.get(i).getObjects().size() * 5 + 10, subnets.get(i).getObjects().size() * 5 + 10,
+					model.getSubNetColors().get(i));
 			bodies.add(temp);
 		}
 	}
@@ -118,13 +148,9 @@ public class HolonCanvas extends JPanel {
 
 		// Render Game Objects
 		for (int i = 0; i < subCount; i++) {
-			bodies.get(i).draw(this.g2);
+			bodies.get(i).setRadius((subnets.get(i).getObjects().size() * 5 + 10)*controller.getHolonBodyScale()/100);
+			bodies.get(i).draw(this.g2, controller.getHolonBodyScale());
 		}
-
-		HolonBody tempBall = currentBall;
-		if (tempBall != null)
-			tempBall.draw(this.g2);
-
 	}
 
 	public void updateGame(float elapsedSeconds) {
@@ -133,10 +159,12 @@ public class HolonCanvas extends JPanel {
 		// and elapsedTime
 		calcCenter();
 		for (int i = 0; i < subCount; i++) {
-			bodies.get(i).position.setX((float) (bodies.get(i).position.getX() + (bodies.get(i).velocity.getX() * (elapsedSeconds))
-					- ((bodies.get(i).position.getX() - center.getWidth()) / 50)));
-			bodies.get(i).position.setY((float) (bodies.get(i).position.getY() + (bodies.get(i).velocity.getY() * (elapsedSeconds))
-					- ((bodies.get(i).position.getY() - center.getHeight()) / 50)));
+			bodies.get(i).position
+					.setX((float) (bodies.get(i).position.getX() + (bodies.get(i).velocity.getX() * (elapsedSeconds))
+							- ((bodies.get(i).position.getX() - center.getWidth()) / 100)));
+			bodies.get(i).position
+					.setY((float) (bodies.get(i).position.getY() + (bodies.get(i).velocity.getY() * (elapsedSeconds))
+							- ((bodies.get(i).position.getY() - center.getHeight()) / 100)));
 
 			if (Math.abs(bodies.get(i).velocity.getX()) < Constants.epsilon)
 				bodies.get(i).velocity.setX(0);
@@ -165,7 +193,7 @@ public class HolonCanvas extends JPanel {
 	public void checkCollisions() {
 		insertionSort(bodies);
 
-		for (int i = 0; i < subCount; i++) {		
+		for (int i = 0; i < subCount; i++) {
 			// Ball to Ball collision
 			for (int j = i + 1; j < subCount; j++) {
 				if ((bodies.get(i).position.getX() + bodies.get(i).getRadius()) < (bodies.get(j).position.getX()