|
@@ -6,10 +6,14 @@ import java.awt.Graphics;
|
|
import java.awt.Graphics2D;
|
|
import java.awt.Graphics2D;
|
|
import java.awt.RenderingHints;
|
|
import java.awt.RenderingHints;
|
|
import java.awt.geom.GeneralPath;
|
|
import java.awt.geom.GeneralPath;
|
|
-import java.util.LinkedList;
|
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
|
|
import javax.swing.JPanel;
|
|
import javax.swing.JPanel;
|
|
|
|
|
|
|
|
+import com.sun.org.apache.xml.internal.utils.ObjectPool;
|
|
|
|
+
|
|
|
|
+import classes.AbstractCpsObject;
|
|
|
|
+import classes.HolonElement;
|
|
import classes.HolonObject;
|
|
import classes.HolonObject;
|
|
import ui.controller.Control;
|
|
import ui.controller.Control;
|
|
import ui.model.Model;
|
|
import ui.model.Model;
|
|
@@ -23,6 +27,18 @@ public class StatisticGraph extends JPanel {
|
|
*/
|
|
*/
|
|
private static final long serialVersionUID = 1L;
|
|
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
|
|
// model and controller
|
|
private Model model;
|
|
private Model model;
|
|
private Control controller;
|
|
private Control controller;
|
|
@@ -32,7 +48,8 @@ public class StatisticGraph extends JPanel {
|
|
GeneralPath path = new GeneralPath();
|
|
GeneralPath path = new GeneralPath();
|
|
|
|
|
|
// Data
|
|
// Data
|
|
- private LinkedList<HolonObject> objects = new LinkedList<>();
|
|
|
|
|
|
+ private ArrayList<HolonObject> objects = new ArrayList<>();
|
|
|
|
+ private ArrayList<GeneralPath> objPaths = new ArrayList<>();
|
|
|
|
|
|
/**
|
|
/**
|
|
* Constructor.
|
|
* Constructor.
|
|
@@ -45,9 +62,18 @@ public class StatisticGraph extends JPanel {
|
|
public StatisticGraph(final Model model, Control control) {
|
|
public StatisticGraph(final Model model, Control control) {
|
|
this.controller = control;
|
|
this.controller = control;
|
|
this.model = model;
|
|
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);
|
|
this.setBackground(Color.WHITE);
|
|
-
|
|
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -59,35 +85,114 @@ public class StatisticGraph extends JPanel {
|
|
*/
|
|
*/
|
|
public void paintComponent(Graphics g) {
|
|
public void paintComponent(Graphics g) {
|
|
super.paintComponent(g);
|
|
super.paintComponent(g);
|
|
- path.reset();
|
|
|
|
-
|
|
|
|
|
|
+
|
|
|
|
+ // Graphics2D init
|
|
g2 = (Graphics2D) g;
|
|
g2 = (Graphics2D) g;
|
|
RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
|
RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
|
g2.setRenderingHints(rh);
|
|
g2.setRenderingHints(rh);
|
|
|
|
|
|
|
|
+ // Paint the Grid
|
|
g2.setStroke(new BasicStroke(0));
|
|
g2.setStroke(new BasicStroke(0));
|
|
-
|
|
|
|
g2.setColor(Color.BLACK);
|
|
g2.setColor(Color.BLACK);
|
|
for (int i = 0; i <= this.getWidth(); i += 10) {
|
|
for (int i = 0; i <= this.getWidth(); i += 10) {
|
|
g2.drawLine(i, 0, i, this.getHeight());
|
|
g2.drawLine(i, 0, i, this.getHeight());
|
|
}
|
|
}
|
|
-
|
|
|
|
for (int i = 0; i <= this.getHeight(); i += 5) {
|
|
for (int i = 0; i <= this.getHeight(); i += 5) {
|
|
g2.drawLine(0, i, this.getWidth(), i);
|
|
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;
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|