123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819 |
- package ui.view;
- import classes.*;
- import classes.comparator.UnitGraphPointComperator;
- import interfaces.GraphEditable;
- import interfaces.GraphEditable.Graphtype;
- import interfaces.IGraphedElement;
- import sun.reflect.generics.reflectiveObjects.NotImplementedException;
- import ui.controller.Control;
- import ui.controller.SingletonControl;
- import ui.model.Model;
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- import java.awt.geom.CubicCurve2D;
- import java.awt.geom.GeneralPath;
- import java.awt.geom.Line2D;
- import java.awt.geom.Path2D;
- import java.awt.geom.Point2D;
- import java.util.ArrayDeque;
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.LinkedList;
- import java.util.ListIterator;
- /**
- * This Class represents a Graph where the User can model the behavior of
- * elements and switches over time.
- *
- * @author Gruppe14
- */
- public class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, ComponentListener {
- private static final long serialVersionUID = 1L;
- public static final int STANDARD_GRAPH_ACCURACY = 100;
- private GeneralPath graphCurve = new GeneralPath();
- private float maximum = 0;
- // Information shown when a Point is Dragged
- private String dragInformation = "";
- // Points
- private Point recSize = new Point(8, 8); // Point Size
- private Graphics2D g2;
- private CubicCurve2D c = new CubicCurve2D.Double();
- private CubicCurve2D cr = new CubicCurve2D.Double();
- private CubicCurve2D cl = new CubicCurve2D.Double();
- private LinkedList<Point> pointList;
- // Scale for the Graph
- private double scaleX;
- private double scaleY;
- private double width = -1;
- private double height = -1;
- private boolean isElement = false;
- private boolean isSwitch = false;
- private ArrayList<HolonElement> tempElements = new ArrayList<>();
- private Model model;
- private Control controller;
- private Line2D.Double line = null;
- private boolean pointDrag = false;
- private boolean init = true;
- private Point tempP = null;
- private double x = 0, y = 0;
- private int x1, x2, y1, y2, ctrlx1, ctrly1, ctrlx2, ctrly2;
-
- private int textWidth = 0;
-
- private IGraphedElement current;
-
- //NEW ERA
- // Normal Settings
- private int border = 4;
- private int clickThreshholdSquared = 25;
- // Display Settings
- /**
- * The size of a dot in the graph.
- * It should be at least 1.
- * */
- int dotSize = 8;
- /** The Color of a dot in the graph. */
- Color dotColor = Color.blue;
- Color editDotColor = new Color(255, 119, 0);
- //Intern Variables
- //TODO: JavaDoc
- private LinkedList<UnitGraphPoint> actualGraphPoints = new LinkedList<UnitGraphPoint>();
- private Graphtype actualGraphType;
- private GraphEditable actualElement;
- Position currentPosition; //outDated
- Position editPosition;
- boolean released = false;
- private int widthWithBorder, heightWithBorder;
-
-
-
-
-
-
- /**
- * Constructor.
- *
- * @param model the Model
- * @param control the Controller
- */
- public UnitGraph(final Model model, Control control) {
- setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
- this.controller = control;
- this.model = model;
- this.pointList = new LinkedList<>();
- this.setBackground(Color.WHITE);
- this.addMouseListener(this);
- this.addMouseMotionListener(this);
- this.addComponentListener(this);
- }
- /**
- * Paints all Components on the Canvas.
- *
- * @param g Graphics
- */
- public void paintComponent(Graphics g) {
- super.paintComponent(g);
-
- //System.out.println("paint");
- Graphics2D g2D = (Graphics2D) g;
- g2D.setColor(Color.BLACK);
- g2D.setRenderingHints(new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON));
- g2D.setStroke(new BasicStroke(2));
- printDebugRepresentive();
- drawUnitGraph(g2D);
- g2D.setColor(dotColor);
- if(released)
- {
- drawUnitGraphPointsReleased(g2D);
- }else
- {
- drawUnitGraphPoints(g2D);
- }
- }
- //TODO -> New Section
-
- private Path2D.Double initBezier(Position start) {
- //Good Source for basic understanding for Bezier Curves
- //http://www.theappguruz.com/blog/bezier-curve-in-games
-
- Path2D.Double path = new Path2D.Double();
- path.moveTo(start.x, start.y);
- return path;
- }
- private void curveTo(Path2D.Double path, Position actual, Position target) {
- double mitte = (actual.x + target.x)* 0.5;
- path.curveTo(mitte, actual.y, mitte, target.y, target.x, target.y);
- }
- private void drawDot(Graphics2D g, Position p)
- {
- g.fillOval(p.x -dotSize/2, p.y-dotSize/2, dotSize, dotSize);
- }
- private void drawUnitGraph(Graphics2D g) {
- switch(actualGraphType) {
- case boolGraph:
- drawBoolGraph(g);
- break;
- case doubleGraph:
- if(released)
- drawDoubleGraphWithEditPosition(g);
- else
- drawDoubleGraph(g);
- break;
- default:
- throw new UnsupportedOperationException();
- }
- }
- private void drawUnitGraphPoints(Graphics2D g) {
- g.setColor(dotColor);
- for(UnitGraphPoint p : actualGraphPoints){
- drawDot(g, p.displayedPosition);
- }
- }
-
- private void drawUnitGraphPointsReleased(Graphics2D g) {
- drawUnitGraphPoints(g);
- g.setColor(editDotColor);
- drawDot(g, editPosition);
- }
-
- private void drawBoolGraph(Graphics2D g) {
- throw new NotImplementedException();
- }
-
- private void drawDoubleGraph(Graphics2D g) {
- if(actualGraphPoints.isEmpty()) throw new IndexOutOfBoundsException("A Graph Without Points is not supportet jet");
- ListIterator<UnitGraphPoint> iter = actualGraphPoints.listIterator();
- Position actual = iter.next().displayedPosition;
- Path2D.Double path = this.initBezier(actual);
- while (iter.hasNext())
- {
- Position target = iter.next().displayedPosition;
- this.curveTo(path, actual, target);
- actual = target;
- }
- g.draw(path);
- //Maybe New Feature
- g.setColor(Color.darkGray);
- //Start
- Path2D.Double path2 = new Path2D.Double();
- Position startpunkt = actualGraphPoints.getFirst().displayedPosition;
- path2.moveTo(0,startpunkt.y);
- path2.lineTo(startpunkt.x, startpunkt.y);
- g.draw(path2);
- //Ende
- Position endpunkt = actualGraphPoints.getLast().displayedPosition;
- path2.moveTo(endpunkt.x,endpunkt.y);
- path2.lineTo(2 * border + border+widthWithBorder, endpunkt.y);
- g.draw(path2);
-
- }
- private void drawDoubleGraphWithEditPosition(Graphics2D g) {
- LinkedList<Position> before = new LinkedList<Position>();
- LinkedList<Position> after = new LinkedList<Position>();
- for(UnitGraphPoint p: actualGraphPoints)
- {
- if(p.displayedPosition.x < editPosition.x)
- before.add(p.displayedPosition);
- else
- after.add(p.displayedPosition);
- }
- drawUnitGraphFromList(g, before);
- drawUnitGraphFromList(g, after);
- //EditGraph
- LinkedList<Position> middle = new LinkedList<Position>();
- if(!before.isEmpty()) middle.add(before.getLast());
- middle.add(editPosition);
- if(!after.isEmpty()) middle.add(after.getFirst());
-
- g.setColor(editDotColor);
- drawUnitGraphFromList(g, middle);
- }
- private void drawUnitGraphFromList(Graphics2D g, LinkedList<Position> list) {
- if(list.size() <= 1) return;
- ListIterator<Position> iter = list.listIterator();
- Position actual = list.getFirst();
- Path2D.Double path = this.initBezier(actual);
- while (iter.hasNext())
- {
- Position target = iter.next();
- curveTo(path, actual, target);
- actual = target;
- }
- g.draw(path);
- }
-
-
-
- private void updateRepresentativePositions()
- {
- for(UnitGraphPoint p : actualGraphPoints) {
- p.calcDisplayedPosition(border, widthWithBorder, heightWithBorder);
- }
- }
-
- private void overrideUnitGraph(LinkedList<Point2D.Double> stateCurve) {
- actualGraphPoints.clear();
- for(Point2D.Double p: stateCurve){
- actualGraphPoints.add(new UnitGraphPoint(p));
- }
- updateRepresentativePositions();
- }
- private void calculateWidthHeight()
- {
- widthWithBorder = this.getWidth() - 2 * border;
- heightWithBorder = this.getHeight() - 2 * border;
- }
-
- public void initNewElement(GraphEditable element)
- {
- overrideUnitGraph(element.getStateGraph());
- actualGraphType = element.getGraphType();
- actualElement = element;
- repaint();
- }
- private void printDebugRepresentive(){
- if(this.actualGraphPoints.isEmpty()) return;
- System.out.print("{");
- for(UnitGraphPoint p: actualGraphPoints){
- System.out.print(p.displayedPosition);
- }
- System.out.println("}");
- }
-
- private void detectPointUnderCurserAndRemove(MouseEvent mEvent) {
- //get mouse Position
- Position mPosition = new Position(mEvent.getPoint());
- ListIterator<UnitGraphPoint> iter2 = actualGraphPoints.listIterator();
- while (iter2.hasNext())
- {
- if(mPosition.squareDistance(iter2.next().displayedPosition) < clickThreshholdSquared)
- {
- iter2.remove();
- break;
- }
- }
- }
-
-
- private void updateEditPointPosition(Position newPosition) {
- //make it in the bounds of the UnitGraph no Point out of the Border
- currentPosition = setInBounds(newPosition);
- this.editPosition = currentPosition;
- repaint();
- }
-
- private Position setInBounds(Position p) {
- p.clampX(border, border + widthWithBorder);
- p.clampY(border, border + heightWithBorder);
- return p;
- }
-
- private void insertNewGraphPoint(Position pos)
- {
- System.out.println("insertNewGraphPoint");
- setInBounds(pos);
- ListIterator<UnitGraphPoint> iter2 = actualGraphPoints.listIterator();
- while (iter2.hasNext())
- {
- Position tempPosition = iter2.next().displayedPosition;
- if(pos.x <= tempPosition.x)
- {
- //previous to go back a position to make the new point before the the Position with greater X
- iter2.previous();
- iter2.add(generateUnitGraphPoint(pos));
- break;
- }
- }
- if(!iter2.hasNext()) //if behind last point
- {
- iter2.add(generateUnitGraphPoint(pos));
- }
- }
- private UnitGraphPoint generateUnitGraphPoint(Position pos) {
- UnitGraphPoint temp = new UnitGraphPoint((double)(pos.x - border)/(double)widthWithBorder,1 - (double) (pos.y - border)/(double)heightWithBorder,true);
- temp.displayedPosition = pos;
- return temp;
- }
- @Override
- public void mouseDragged(MouseEvent e) {
- System.out.println("MouseDragged");
- updateEditPointPosition(new Position(e.getPoint()));
- }
- @Override
- public void mouseMoved(MouseEvent e) {
- }
- @Override
- public void mouseClicked(MouseEvent e) {
- }
- @Override
- public void mouseEntered(MouseEvent e) {
- }
- @Override
- public void mouseExited(MouseEvent e) {
- }
- @Override
- public void mousePressed(MouseEvent e) {
- System.out.println("mousePressed");
- detectPointUnderCurserAndRemove(e);
- updateEditPointPosition(new Position(e.getPoint()));
- released = true;
- repaint();
- }
- @Override
- public void mouseReleased(MouseEvent e) {
- System.out.println("mouseReleased");
- this.insertNewGraphPoint(editPosition);
- released = false;
- repaint();
- }
- /**
- * When the Component is Resized.
- *
- * @param e ComponentEvent
- */
- public void componentResized(ComponentEvent e) {
- System.out.println("componentResized");
- calculateWidthHeight();
- updateRepresentativePositions();
- // Wenn ein anderes Element genommen wird
- /*
- if (init) {
- init = false;
- // for scale on the first initialisation
- if (width == -1 && height == -1) {
- width = this.getWidth() - (border * 2);
- height = this.getHeight() - (border * 2);
- }
- // Scale
- scaleX = (this.getWidth() - (border * 2)) / width;
- scaleY = (this.getHeight() - (border * 2)) / height;
- // set the scroll graph invisible
- this.getParent().getParent().setVisible(false);
- }
- // Scale
- scaleX = (this.getWidth() - (border * 2)) / width;
- scaleY = (this.getHeight() - (border * 2)) / height;
- */
- repaint();
- }
- @Override
- public void componentHidden(ComponentEvent e) {
- }
- @Override
- public void componentMoved(ComponentEvent e) {
- }
- @Override
- public void componentShown(ComponentEvent e) {
- }
- /**
- * Empty the Graph.
- */
- public void empty() {
- System.out.println("empty");
- pointList = null;
- tempElements = null;
- current = null;
- isSwitch = false;
- isElement = false;
- repaint();
- }
- /**
- * Resets the Points for the Element.
- */
- public void reset() {
- System.out.println("reset");
- // pointList.removeAll(pointList);
- // if (isSwitch) {
- // pointList.addFirst(new Point(-border, (int) (height / 6)));
- // pointList.addLast(new Point((int) ((this.getWidth()) / scaleX), (int) (height / 6)));
- // } else {
- // pointList.addFirst(new Point(0, 0));
- // pointList.addLast(new Point((int) ((this.getWidth() - (border * 2)) / scaleX), 0));
- // }
- // repaint();
- }
- /**
- * converts the number to fit the canvas.
- *
- * @param d the number to convert
- * @return the converted number
- */
- public double convertToCanvasY(float d) {
- System.out.println("convertToCanvasY");
- return (height - (d * (height / maximum)));
- }
- /**
- * converts the number to fit the value.
- *
- * @param d the number to convert
- * @return the converted number
- */
- public float convertToValueY(double d) {
- System.out.println("convertToValueY");
- return (float) Math.round(((height - (height * (d / height))) / (height / maximum)) * 10) / 10;
- }
- /**
- * Visualize the HolonElement on the Graph.
- *
- * @param selectedElement which should be visualized
- */
- public void repaintWithNewElement(ArrayList<HolonElement> selectedElement) {
- System.out.println("repaintWithNewElement");
- // //maybe linkedlist better?
- // //arrayOfFloats = selectedElement.get(selectedElement.size() - 1).getAvailableEnergyPerElementAt();
- // current = selectedElement.get(selectedElement.size()-1);
- // tempElements=selectedElement;
- // pointList = selectedElement.get(selectedElement.size() - 1).getGraphPoints();
- // isSwitch = false;
- // isElement = true;
- // maximum = getMaximum(selectedElement.get(selectedElement.size() - 1));
- // // First time clicked on the Element
- // if (pointList.isEmpty()) {
- // pointList.addFirst(new Point(0, 0));
- // pointList.addLast(new Point((int) ((this.getWidth() - (border * 2)) / scaleX), 0));
- // }
- // repaint();
- }
- /**
- * Visualize the Switch on the Graph.
- *
- * @param s which should be visualized
- */
- public void repaintWithNewSwitch(HolonSwitch s) {
- System.out.println("repaintWithNewSwitch");
-
- // //arrayOfBooleans = s.getValueArray();
- // current=s;
- // pointList = s.getGraphPoints();
- // isSwitch = true;
- // isElement = false;
- // // First time clicked on the Element
- // if (pointList.isEmpty()) {
- // pointList.addFirst(new Point(-border, (int) (height / 6)));
- // pointList.addLast(new Point((int) ((this.getWidth()) / scaleX), (int) (height / 6)));
- // }
- // repaint();
- }
- /**
- * Build a Curve for the Graph.
- *
- * @param p1 startpoint
- * @param p2 endpoint
- * @return the CubicCurve2D for the Graph
- */
- public CubicCurve2D buildCurve(Point p1, Point p2) {
- System.out.println("buildCurve");
- x1 = (int) p1.getX();
- y1 = (int) p1.getY();
- x2 = (int) p2.getX();
- y2 = (int) p2.getY();
- // calculate the controllpoints
- ctrlx1 = x1 + (x2 - x1) / 2;
- ctrlx2 = x2 - (x2 - x1) / 2;
- if (y1 < y2) {
- ctrly1 = y1 + (y2 - y1) / 10;
- ctrly2 = y2 - (y2 - y1) / 10;
- } else {
- ctrly1 = y1 - (y1 - y2) / 10;
- ctrly2 = y2 + (y1 - y2) / 10;
- }
- // set the curve
- c.setCurve(x1 * scaleX, y1 * scaleY, ctrlx1 * scaleX, ctrly1 * scaleY, ctrlx2 * scaleX, ctrly2 * scaleY,
- x2 * scaleX, y2 * scaleY);
- return c;
- }
- /**
- * Fills the Arrays with booleans.
- */
- public void fillArrayofBooleans() {
- System.out.println("fillArrayofBooleans");
- for (int i = 0; i < STANDARD_GRAPH_ACCURACY; i++) {
- int t = (int) getYValueAt((int) (i * width / (STANDARD_GRAPH_ACCURACY - 1)));
- if (t <= height / 2) {
- ((HolonSwitch)current).setActiveAt(i, true);
- } else {
- ((HolonSwitch)current).setActiveAt(i, false);
- }
- }
- }
- /**
- * Fills the Arrays of each HolonElement.
- */
- @SuppressWarnings("unchecked")
- public void generateSampleCurves() {
- System.out.println("generateSampleCurves");
- for (HolonElement he : tempElements) {
- maximum = getMaximum(he);
- he.setGraphPoints((LinkedList<Point>) pointList.clone());
- //foreach(Point p: pointList)
- System.out.println("------------");
- System.out.println("pointList[");
- for(Point p: pointList)
- {
- System.out.println(p);
- }
- System.out.println("]");
- for (int i = 0; i < STANDARD_GRAPH_ACCURACY; i++) {//!!!!!
- he.setAvailableEnergyPerElementAt(i, convertToValueY(getYValueAt2((int) (i * width / (100 - 1)))));
- }
-
-
- System.out.println("TestgraphPoints:");
- he.testFunctiongetAvailableEnergyAt(0);
- }
- }
-
-
- /**
- * Convert the graph widget point to a pointlist from a holon element
- * @param unitgraph
- */
- public LinkedList<Point> convertUnitGraphToHolonElemntPointList(LinkedList<Point> unitgraph)
- {
- LinkedList<Point> graphpoints = new LinkedList<Point>();
- if(width == 0 || height == 0) return graphpoints;
- for(Point p: unitgraph)
- {
- //CalcX
- int x = (int )((p.getX() / width)* 100.0);
- //CalcY
- int y = (int )((1.0-(p.getY() / height))* 100.0); //1.0- because 0 is at the top, width is at the bottom
- //AddPoint
- graphpoints.add(new Point(x, y));
- }
- return graphpoints;
- }
-
-
- public LinkedList<Point> convertHolonElementPointListToUnitGraph(LinkedList<Point> graphPoints)
- {
- LinkedList<Point> unitgraph = new LinkedList<Point>();
- for(Point p: graphPoints)
- {
- //CalcX
- int x = (int )((p.getX() / 100.0)* width);
- //CalcY
- int y = (int )((1.0-(p.getY() / 100.0))* height); //1.0- because 0 is at the top, width is at the bottom
- //AddPoint
- unitgraph.add(new Point(x, y));
- }
- return unitgraph;
- }
- /**
- * Get the Y Value at the x Coordination.
- *
- * @param xVal the x value for the y value
- * @return y, the value at x
- */
- public float getYValueAt(int xVal) {
- System.out.println("getYValueAt");
- for (int i = 0; i < pointList.size() - 1; i++) {
- // get the Points
- if (xVal <= pointList.get(i + 1).getX()) {
- // Curve erstellen
- Line2D l1 = new Line2D.Double(pointList.get(i).getX(), pointList.get(i).getY(),
- pointList.get(i + 1).getX(), pointList.get(i + 1).getY());
- Line2D l2 = new Line2D.Double(xVal, 0, xVal, height);
- return getIntersectionPoint(l1, l2);
- }
- }
- return 0;
- }
- /**
- * Get y value at the x Coordination via curves.
- *
- * @param xVal the x value for the y value
- * @return y value at x
- */
- public float getYValueAt2(int xVal) {
- System.out.println("getYValueAt2");
- for (int i = 0; i < pointList.size() - 1; i++) {
- // get the Points
- if (xVal >= pointList.get(i).getX()) {
- // Curve erstellen
- c = buildCurve(pointList.get(i), pointList.get(i + 1));
- c.subdivide(cl, cr);
- // Teil der Kurve aussuchen
- if (cl.getX1() <= xVal * scaleX && cl.getX2() > xVal * scaleX) {
- c = cl;
- // Kurve Links von "unten"
- if (pointList.get(i).getY() >= pointList.get(i + 1).getY()) {
- for (float j = (float) (height - 1); j >= 0; j -= 0.1f) {
- if (c.contains(xVal * scaleX, j * scaleY)) {
- return (float) (j);
- }
- }
- } else {// Kurve Links von "oben"
- for (float j = 0; j < height; j += 0.1f) {
- if (c.contains(xVal * scaleX, j * scaleY)) {
- return (float) (j);
- }
- }
- }
- } else {
- c = cr;
- // Kurve Links von "unten"
- if (pointList.get(i).getY() >= pointList.get(i + 1).getY()) {
- for (float j = 0; j < height; j += 0.1f) {
- if (c.contains(xVal * scaleX, j * scaleY)) {
- return (float) (j);
- }
- }
- } else {// Kurve Links von "oben"
- for (float j = (float) (height - 1); j >= 0; j -= 0.1f) {
- if (c.contains(xVal * scaleX, j * scaleY)) {
- return (float) (j);
- }
- }
- }
- }
- }
- }
- // else
- return getYValueAt(xVal);
- }
- /**
- * Get the Intersection Point of 2 Lines.
- *
- * @param l1 the first Line
- * @param l2 the second Line
- * @return The Intersection Point
- */
- public float getIntersectionPoint(Line2D l1, Line2D l2) {
- System.out.println("getIntersectionPoint");
- if (!l1.intersectsLine(l2)) {
- return 0;// null;
- }
- double px = l1.getX1(), py = l1.getY1(), rx = l1.getX2() - px, ry = l1.getY2() - py;
- double qx = l2.getX1(), qy = l2.getY1(), sx = l2.getX2() - qx, sy = l2.getY2() - qy;
- double det = sx * ry - sy * rx;
- if (det == 0) {
- return 0;// null;
- } else {
- double z = (sx * (qy - py) + sy * (px - qx)) / det;
- if (z < 0 || z > 1) {
- return 0;// new Point(0, 0); // intersection at end point!
- }
- return (float) (py + z * ry);// new Point((int) (px + z * rx), (int)
- // (py + z * ry));
- }
- } // end intersection line-line
- public void update(ArrayList<AbstractCpsObject> obj) {
- System.out.println("update");
- ArrayDeque<AbstractCpsObject> queue = new ArrayDeque<>();
- AbstractCpsObject u = null;
- queue.addAll(obj);
- while (!queue.isEmpty()) {
- u = queue.pop();
- repaintGraph(u);
- }
- empty();
- if (u instanceof CpsUpperNode)
- for (AbstractCpsObject adjacent : ((CpsUpperNode) u).getNodes()) {
- queue.add(adjacent);
- }
- }
- void repaintGraph(AbstractCpsObject u) {
- System.out.println("repaintGraph");
- ArrayList<HolonElement> list = new ArrayList<>();
- if (u instanceof HolonObject) {
- for (HolonElement ele : ((HolonObject) u).getElements()) {
- list.add(ele);
- repaintWithNewElement(list);
- generateSampleCurves();
- list.remove(0);
- }
- } else if (u instanceof HolonSwitch) {
- repaintWithNewSwitch((HolonSwitch) u);
- fillArrayofBooleans();
- }
- }
- float getMaximum(HolonElement ele) {
- System.out.println("getMaximum");
- if (ele.isFlexible()) {
- return ele.getFlexibleEnergyAvailablePerElement();
- } else {
- return ele.getEnergyPerElement();
- }
- }
-
- /**
- * sets the localPeriod of the Current Graph
- * @param localPeriod
- */
- public void setLocalPeriod(int localPeriod){
- System.out.println("setLocalPeriod");
- if(isElement)for(IGraphedElement e:tempElements)e.setLocalPeriod(localPeriod);
- else if(isSwitch)current.setLocalPeriod(localPeriod);
- }
-
- /**
- * gets the LocalPeriod of the CurrentGraph
- * @return localPeriod of the current Element or Switch
- */
- public int getLocalPeriod(){
- System.out.println("getLocalPeriod");
- if(current!=null)return current.getLocalPeriod();
- else return model.getGraphIterations();//TODO: maybe rename
- }
-
- public boolean isStretching(){
- System.out.println("isStretching");
- return current.isStretching();
- }
-
- public void setStretching(boolean b){
- System.out.println("setStretching");
- if(isElement)for(IGraphedElement e:tempElements)e.setStretching(b);
- else if(isSwitch)current.setStretching(b);
- }
-
-
- }
|