package ui.view; import java.awt.BasicStroke; import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.geom.GeneralPath; import java.util.ArrayList; import javax.swing.JPanel; import classes.HolonElement; import classes.HolonObject; import ui.controller.Control; import ui.model.Model; 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 colors = new ArrayList<>(); // model and controller private Model model; private Control controller; // Graphics2D private Graphics2D g2; GeneralPath path = new GeneralPath(); // Data private ArrayList objects = new ArrayList<>(); private ArrayList objPaths = new ArrayList<>(); /** * Constructor. * * @param model * the Model * @param control * the Controller */ 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); } /** * Paints all Components on the Canvas. * * @param g * Graphics * */ public void paintComponent(Graphics g) { super.paintComponent(g); // 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); } // 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()))); } } 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; } }