Ver Fonte

Merge branch 'master' of https://git.tk.informatik.tu-darmstadt.de/carlos.garcia/praktikum-holons

dominik.rieder há 7 anos atrás
pai
commit
170901801f

+ 4 - 1
src/ui/view/GUI.java

@@ -223,6 +223,7 @@ public class GUI<E> implements CategoryListener {
 	private Console console = new Console();
 	private MyCanvas canvas;
 	private UnitGraph unitGraph;
+	private StatisticGraph statGraph;
 	private final JSplitPane splitPane3 = new JSplitPane();
 	private final JSlider sizeSlider = new JSlider();
 	private final JLabel lblImageSize = new JLabel(Languages.getLanguage()[94]);
@@ -266,6 +267,7 @@ public class GUI<E> implements CategoryListener {
 		this.model = control.getModel();
 		this.canvas = new MyCanvas(model, control);
 		this.unitGraph = new UnitGraph(model, control);
+		this.statGraph = new StatisticGraph(model, controller);
 		control.initListener(this);
 		controller.setCanvas(canvas);
 		model.setConsole(console);
@@ -1571,6 +1573,7 @@ public class GUI<E> implements CategoryListener {
 				controller.calculateStateForTimeStep(i);
 
 				unitGraph.repaint();
+				statGraph.repaint();
 			}
 		});
 		splitPane.setRightComponent(splitPane1);
@@ -1581,7 +1584,7 @@ public class GUI<E> implements CategoryListener {
 		splitPane.setLeftComponent(scrollPane1);
 		splitPaneCanvasConsole.setLeftComponent(panelTapped_SimMenu);
 		tabbedPane.addTab("View", canvasSP);
-		tabbedPane.addTab("Statistics", statScrollPane);
+		tabbedPane.addTab("Statistics", new JPanel());
 		// splitPaneCanvasConsole.setLeftComponent(canvasSP);
 		splitPaneCanvasConsole.setRightComponent(console);
 		splitPane1.setLeftComponent(splitPaneCanvasConsole);

+ 1 - 1
src/ui/view/SimulationMenu.java

@@ -148,7 +148,7 @@ public class SimulationMenu extends JMenuBar {
 
 		// timerSpeed
 		simSpeedText.setMaximumSize(new Dimension(300, 300));
-		simSpeedText.setMinimumSize(new Dimension(300, 300));
+		//simSpeedText.setMinimumSize(new Dimension(300, 300));
 		simSpeedText.addCaretListener(new CaretListener() {
 			@Override
 			public void caretUpdate(CaretEvent e) {

+ 124 - 19
src/ui/view/StatisticGraph.java

@@ -6,10 +6,14 @@ import java.awt.Graphics;
 import java.awt.Graphics2D;
 import java.awt.RenderingHints;
 import java.awt.geom.GeneralPath;
-import java.util.LinkedList;
+import java.util.ArrayList;
 
 import javax.swing.JPanel;
 
+import com.sun.org.apache.xml.internal.utils.ObjectPool;
+
+import classes.AbstractCpsObject;
+import classes.HolonElement;
 import classes.HolonObject;
 import ui.controller.Control;
 import ui.model.Model;
@@ -23,6 +27,18 @@ public class StatisticGraph extends JPanel {
 	 */
 	private static final long serialVersionUID = 1L;
 
+	// Max Objects
+	final int MAX_OBJECTS = 8;
+
+	// Maximum y Value
+	double maximum = 0;
+
+	// is the Simulation running?
+	private boolean isSimRunning;
+
+	// Colours
+	private ArrayList<Color> colors = new ArrayList<>();
+
 	// model and controller
 	private Model model;
 	private Control controller;
@@ -32,7 +48,8 @@ public class StatisticGraph extends JPanel {
 	GeneralPath path = new GeneralPath();
 
 	// Data
-	private LinkedList<HolonObject> objects = new LinkedList<>();
+	private ArrayList<HolonObject> objects = new ArrayList<>();
+	private ArrayList<GeneralPath> objPaths = new ArrayList<>();
 
 	/**
 	 * Constructor.
@@ -45,9 +62,18 @@ public class StatisticGraph extends JPanel {
 	public StatisticGraph(final Model model, Control control) {
 		this.controller = control;
 		this.model = model;
-
+		colors.add(Color.BLUE);
+		colors.add(Color.RED);
+		colors.add(Color.GREEN);
+		colors.add(Color.CYAN);
+		colors.add(Color.ORANGE);
+		colors.add(Color.PINK);
+		colors.add(Color.MAGENTA);
+		colors.add(Color.YELLOW);
+		
+		
+		
 		this.setBackground(Color.WHITE);
-
 	}
 
 	/**
@@ -59,35 +85,114 @@ public class StatisticGraph extends JPanel {
 	 */
 	public void paintComponent(Graphics g) {
 		super.paintComponent(g);
-		path.reset();
-		
+
+		// Graphics2D init
 		g2 = (Graphics2D) g;
 		RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
 		g2.setRenderingHints(rh);
 
+		// Paint the Grid
 		g2.setStroke(new BasicStroke(0));
-
 		g2.setColor(Color.BLACK);
 		for (int i = 0; i <= this.getWidth(); i += 10) {
 			g2.drawLine(i, 0, i, this.getHeight());
 		}
-
 		for (int i = 0; i <= this.getHeight(); i += 5) {
 			g2.drawLine(0, i, this.getWidth(), i);
 		}
 
-		g2.setColor(Color.BLUE);
-		g2.setStroke(new BasicStroke(2));
-		
-		path.moveTo(0,this.getHeight());
-		for (int i = 0; i <= 49; i++) {
-			double coordY = Math.random()*this.getHeight();
-			controller.addTextToConsole(""+i);
-			path.lineTo(i*this.getWidth()/49, coordY);
-			path.moveTo(i*this.getWidth()/49, coordY);
+		// Reset?
+		if (!isSimRunning && model.getIsSimulation()) {
+			for (int i = 0; i < objects.size(); i++) {
+				objPaths.get(i).reset();
+				objPaths.get(i).moveTo(model.getCurIteration() * this.getWidth() / model.getIterations() - 1,
+						convertToCanvasY(Math.abs(objects.get(i).getCurrentEnergy())));
+			}
 		}
-		
-		g2.draw(path);
+
+		isSimRunning = model.getIsSimulation();
+
+		// if sim is on
+		if (isSimRunning) {
+			maximum = 0;
+			for (HolonObject obj : objects) {
+				if (maximum <= (Math.abs(obj.getCurrentEnergy()))) {
+					maximum = (Math.abs(obj.getCurrentEnergy()));
+				}
+			}
+
+			for (int i = 0; i < objects.size(); i++) {
+				g2.setColor(new Color((int) (Math.random() * 255), (int) (Math.random() * 255),
+						(int) (Math.random() * 255)));
+				objPaths.get(i).lineTo(model.getCurIteration() * this.getWidth() / model.getIterations() - 1,
+						convertToCanvasY(Math.abs(getEnergyAtCurrentTimeStep(objects.get(i)))));
+				objPaths.get(i).moveTo(model.getCurIteration() * this.getWidth() / model.getIterations() - 1,
+						convertToCanvasY(Math.abs(getEnergyAtCurrentTimeStep(objects.get(i)))));
+
+			}
+		}
+		g2.setStroke(new BasicStroke(3));
+		for (int i = 0; i < objPaths.size(); i++) {
+			g2.setColor(colors.get(i));
+			g2.draw(objPaths.get(i));
+		}
+
+	}
+
+	/**
+	 * Add an Object to the Graph if the maximum has not reached yet.
+	 * 
+	 * @param obj
+	 *            the Object to add
+	 */
+	public void addObject(HolonObject obj) {
+		if (objects.size() < MAX_OBJECTS && !objects.contains(obj)) {
+			objects.add((HolonObject) obj);
+			objPaths.add(new GeneralPath());
+			objPaths.get(objPaths.size() - 1)
+					.moveTo(model.getCurIteration() * this.getWidth() / model.getIterations() - 1, convertToCanvasY(Math
+							.abs(objects.get(objects.size() - 1).getCurrentEnergyAtTimeStep(model.getCurIteration()))));
+		}
+	}
+
+	/**
+	 * Removes an Object from the Graph.
+	 * 
+	 * @param obj
+	 *            the Object to remove
+	 */
+	public void removeObject(HolonObject obj) {
+		if (objects.contains(obj)) {
+			objPaths.remove(objects.indexOf(obj));
+			objects.remove(obj);
+		}
+	}
+
+	/**
+	 * converts the number to fit the canvas.
+	 * 
+	 * @param d
+	 *            the number to convert
+	 * @return the converted number
+	 */
+	public double convertToCanvasY(float d) {
+		return Math.abs((this.getHeight() - (d * (this.getHeight() / maximum))));
+	}
+	
+	/**
+	 * Does take into account which timestep is watched, calculates the max
+	 * values.
+	 * 
+	 * @return the currentEnergy
+	 */
+	public float getEnergyAtCurrentTimeStep(HolonObject obj) {
+		float temp = 0;
+		for (HolonElement e : obj.getElements()) {
+			if (e.getActive()) {
+				temp = temp + e.getEnergyAt()[model.getCurIteration()];
+			}
+		}
+		return temp;
 	}
 
 }

+ 1 - 1
src/ui/view/UnitGraph.java

@@ -20,6 +20,7 @@ import java.awt.Point;
 import javax.swing.JPanel;
 
 import classes.HolonElement;
+import classes.HolonObject;
 import ui.controller.Control;
 import ui.model.Model;
 import classes.HolonSwitch;
@@ -747,5 +748,4 @@ public class UnitGraph extends JPanel implements MouseListener, MouseMotionListe
 											// (py + z * ry));
 		}
 	} // end intersection line-line
-
 }