|
@@ -13,14 +13,9 @@ 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;
|
|
|
|
|
@@ -28,48 +23,17 @@ import java.util.ListIterator;
|
|
|
* This Class represents a Graph where the User can model the behavior of
|
|
|
* elements and switches over time.
|
|
|
*
|
|
|
- * @author Gruppe14
|
|
|
+ * @author Tom Troppmann
|
|
|
*/
|
|
|
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;
|
|
|
-
|
|
|
- private String dragInformation = "";
|
|
|
-
|
|
|
- private Point recSize = new Point(8, 8);
|
|
|
- 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;
|
|
|
-
|
|
|
- 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;
|
|
|
|
|
|
-
|
|
|
+
|
|
|
|
|
|
private int border = 4;
|
|
|
private int clickThreshholdSquared = 25;
|
|
|
+
|
|
|
|
|
|
|
|
|
* The size of a dot in the graph.
|
|
@@ -79,14 +43,22 @@ public class UnitGraph extends JPanel implements MouseListener, MouseMotionListe
|
|
|
|
|
|
Color dotColor = Color.blue;
|
|
|
Color editDotColor = new Color(255, 119, 0);
|
|
|
+
|
|
|
|
|
|
|
|
|
private LinkedList<UnitGraphPoint> actualGraphPoints = new LinkedList<UnitGraphPoint>();
|
|
|
private Graphtype actualGraphType;
|
|
|
private GraphEditable actualElement;
|
|
|
- Position currentPosition;
|
|
|
Position editPosition;
|
|
|
- boolean released = false;
|
|
|
+ boolean editMode = false;
|
|
|
+ private enum pointType {Normal, StartPoint, EndPoint};
|
|
|
+ pointType editPoint = pointType.Normal;
|
|
|
+
|
|
|
+
|
|
|
+ private Model model;
|
|
|
+ private Control controller;
|
|
|
+
|
|
|
+
|
|
|
private int widthWithBorder, heightWithBorder;
|
|
|
|
|
|
|
|
@@ -104,8 +76,6 @@ public class UnitGraph extends JPanel implements MouseListener, MouseMotionListe
|
|
|
setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
|
|
|
this.controller = control;
|
|
|
this.model = model;
|
|
|
-
|
|
|
- this.pointList = new LinkedList<>();
|
|
|
this.setBackground(Color.WHITE);
|
|
|
|
|
|
this.addMouseListener(this);
|
|
@@ -126,19 +96,38 @@ public class UnitGraph extends JPanel implements MouseListener, MouseMotionListe
|
|
|
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)
|
|
|
+ if(editMode)
|
|
|
{
|
|
|
drawUnitGraphPointsReleased(g2D);
|
|
|
}else
|
|
|
{
|
|
|
drawUnitGraphPoints(g2D);
|
|
|
}
|
|
|
+ g2D.setStroke(new BasicStroke(1));
|
|
|
+ drawCurrentIterartionLine(g2D);
|
|
|
}
|
|
|
|
|
|
|
|
|
+ private void drawCurrentIterartionLine(Graphics2D g)
|
|
|
+ {
|
|
|
+ int cur = model.getCurIteration();
|
|
|
+ int max = model.getIterations();
|
|
|
+ double where = ((double) cur)/((double) max);
|
|
|
+ Position oben = new Position(border + (int)(where * widthWithBorder), 0);
|
|
|
+ Position unten = new Position(border + (int)(where * widthWithBorder), 2 * border + heightWithBorder);
|
|
|
+ drawLine(g,oben,unten);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void drawLine(Graphics2D g, Position start, Position end)
|
|
|
+ {
|
|
|
+ Path2D.Double path = new Path2D.Double();
|
|
|
+ path.moveTo(start.x, start.y);
|
|
|
+ path.lineTo(end.x, end.y);
|
|
|
+ g.draw(path);
|
|
|
+ }
|
|
|
+
|
|
|
private Path2D.Double initBezier(Position start) {
|
|
|
|
|
|
|
|
@@ -161,7 +150,7 @@ public class UnitGraph extends JPanel implements MouseListener, MouseMotionListe
|
|
|
drawBoolGraph(g);
|
|
|
break;
|
|
|
case doubleGraph:
|
|
|
- if(released)
|
|
|
+ if(editMode)
|
|
|
drawDoubleGraphWithEditPosition(g);
|
|
|
else
|
|
|
drawDoubleGraph(g);
|
|
@@ -177,6 +166,15 @@ public class UnitGraph extends JPanel implements MouseListener, MouseMotionListe
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ private void saveGraph() {
|
|
|
+ LinkedList<Point2D.Double> actual = actualElement.getStateGraph();
|
|
|
+ actual.clear();
|
|
|
+ for(UnitGraphPoint p: actualGraphPoints)
|
|
|
+ {
|
|
|
+ actual.add(p.getPoint());
|
|
|
+ }
|
|
|
+ actualElement.sampleGraph();
|
|
|
+ }
|
|
|
private void drawUnitGraphPointsReleased(Graphics2D g) {
|
|
|
drawUnitGraphPoints(g);
|
|
|
g.setColor(editDotColor);
|
|
@@ -199,19 +197,6 @@ public class UnitGraph extends JPanel implements MouseListener, MouseMotionListe
|
|
|
actual = target;
|
|
|
}
|
|
|
g.draw(path);
|
|
|
-
|
|
|
- g.setColor(Color.darkGray);
|
|
|
-
|
|
|
- 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);
|
|
|
-
|
|
|
- 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) {
|
|
@@ -279,36 +264,34 @@ public class UnitGraph extends JPanel implements MouseListener, MouseMotionListe
|
|
|
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) {
|
|
|
-
|
|
|
- Position mPosition = new Position(mEvent.getPoint());
|
|
|
- ListIterator<UnitGraphPoint> iter2 = actualGraphPoints.listIterator();
|
|
|
- while (iter2.hasNext())
|
|
|
+
|
|
|
+ private void removePointNearPosition(Position mPosition) {
|
|
|
+ ListIterator<UnitGraphPoint> iter = actualGraphPoints.listIterator();
|
|
|
+ while (iter.hasNext())
|
|
|
{
|
|
|
- if(mPosition.squareDistance(iter2.next().displayedPosition) < clickThreshholdSquared)
|
|
|
+ if(mPosition.squareDistance(iter.next().displayedPosition) < clickThreshholdSquared)
|
|
|
{
|
|
|
- iter2.remove();
|
|
|
+ iter.remove();
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ private void detectStartEndPoint(Position mPosition)
|
|
|
+ {
|
|
|
+ UnitGraphPoint first = actualGraphPoints.getFirst();
|
|
|
+ UnitGraphPoint last = actualGraphPoints.getLast();
|
|
|
+ if((mPosition.squareDistance(first.displayedPosition) < clickThreshholdSquared)) editPoint = pointType.StartPoint;
|
|
|
+ else if(mPosition.squareDistance(last.displayedPosition) < clickThreshholdSquared) editPoint = pointType.EndPoint;
|
|
|
+ else editPoint = pointType.Normal;
|
|
|
+ }
|
|
|
|
|
|
private void updateEditPointPosition(Position newPosition) {
|
|
|
|
|
|
- currentPosition = setInBounds(newPosition);
|
|
|
+ Position currentPosition = setInBounds(newPosition);
|
|
|
+ if(editPoint != pointType.Normal) attachToBorder(currentPosition);
|
|
|
this.editPosition = currentPosition;
|
|
|
- repaint();
|
|
|
}
|
|
|
|
|
|
private Position setInBounds(Position p) {
|
|
@@ -317,6 +300,71 @@ public class UnitGraph extends JPanel implements MouseListener, MouseMotionListe
|
|
|
return p;
|
|
|
}
|
|
|
|
|
|
+ private Point.Double getBezierPoint(double t, Point.Double p0, Point.Double p1,Point.Double p2,Point.Double p3) {
|
|
|
+
|
|
|
+ * Calculate Beziér:
|
|
|
+ * B(t) = (1-t)^3 * P0 + 3*(1-t)^2 * t * P1 + 3*(1-t)*t^2 * P2 + t^3 * P3 , 0 < t < 1
|
|
|
+ *
|
|
|
+ * Source:
|
|
|
+ */
|
|
|
+ Point.Double bezier = new Point.Double();
|
|
|
+ double OneSubT = 1-t;
|
|
|
+ double OneSubT2 = Math.pow(OneSubT, 2);
|
|
|
+ double OneSubT3 = Math.pow(OneSubT, 3);
|
|
|
+ double t2 = Math.pow(t , 2);
|
|
|
+ double t3 = Math.pow(t , 3);
|
|
|
+
|
|
|
+ bezier.x = OneSubT3 * p0.x + 3 * OneSubT2 * t * p1.x + 3 * OneSubT * t2 * p2.x + t3 * p3.x;
|
|
|
+ bezier.y = OneSubT3 * p0.y + 3 * OneSubT2 * t * p1.y + 3 * OneSubT * t2 * p2.y + t3 * p3.y;
|
|
|
+ return bezier;
|
|
|
+
|
|
|
+ }
|
|
|
+ private double getYBetweenTwoPoints(double t, Point.Double start, Point.Double end) {
|
|
|
+
|
|
|
+ double mitte = (start.x + end.x)* 0.5;
|
|
|
+ Point.Double bezier = getBezierPoint(t, start, new Point.Double(mitte, start.y), new Point.Double(mitte, end.y), end);
|
|
|
+ return bezier.y;
|
|
|
+ }
|
|
|
+
|
|
|
+ private float[] sampleGraph(int sampleLength)
|
|
|
+ {
|
|
|
+ ListIterator<UnitGraphPoint> iter = actualGraphPoints.listIterator();
|
|
|
+ Point.Double before = iter.next().getPoint();
|
|
|
+ Point.Double after = iter.next().getPoint();
|
|
|
+ float [] sampleCurve = new float[sampleLength];
|
|
|
+ for(int i = 0; i<sampleLength ; i++)
|
|
|
+ {
|
|
|
+ double graphX = (double)i / (double) (sampleLength - 1);
|
|
|
+ if(graphX > after.x)
|
|
|
+ {
|
|
|
+ before = after;
|
|
|
+ after = iter.next().getPoint();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ double t = (after.x -before.x > 0)? (graphX - before.x) / (after.x -before.x) : 0.0;
|
|
|
+ sampleCurve[i] = (float) getYBetweenTwoPoints(t, before, after);
|
|
|
+ }
|
|
|
+ return sampleCurve;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private Position attachToBorder(Position p)
|
|
|
+ {
|
|
|
+ switch(editPoint)
|
|
|
+ {
|
|
|
+ case StartPoint:
|
|
|
+ p.x = border;
|
|
|
+ break;
|
|
|
+ case EndPoint:
|
|
|
+ p.x = border + widthWithBorder;
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ return p;
|
|
|
+ }
|
|
|
+
|
|
|
private void insertNewGraphPoint(Position pos)
|
|
|
{
|
|
|
System.out.println("insertNewGraphPoint");
|
|
@@ -338,16 +386,20 @@ public class UnitGraph extends JPanel implements MouseListener, MouseMotionListe
|
|
|
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);
|
|
|
+
|
|
|
+ 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;
|
|
|
+ return temp;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
@Override
|
|
|
public void mouseDragged(MouseEvent e) {
|
|
|
System.out.println("MouseDragged");
|
|
|
updateEditPointPosition(new Position(e.getPoint()));
|
|
|
+ repaint();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -368,19 +420,37 @@ public class UnitGraph extends JPanel implements MouseListener, MouseMotionListe
|
|
|
|
|
|
@Override
|
|
|
public void mousePressed(MouseEvent e) {
|
|
|
- System.out.println("mousePressed");
|
|
|
- detectPointUnderCurserAndRemove(e);
|
|
|
- updateEditPointPosition(new Position(e.getPoint()));
|
|
|
- released = true;
|
|
|
- repaint();
|
|
|
- }
|
|
|
+ System.out.println("mousePressed");
|
|
|
+ Position mPosition = new Position(e.getPoint());
|
|
|
+ if (e.getButton() == MouseEvent.BUTTON3) {
|
|
|
+
|
|
|
+ detectStartEndPoint(mPosition);
|
|
|
+ if (editPoint == pointType.Normal) {
|
|
|
+ removePointNearPosition(mPosition);
|
|
|
+ repaint();
|
|
|
+ }
|
|
|
+ editMode = false;
|
|
|
+
|
|
|
+ } else if (e.getButton() == MouseEvent.BUTTON1) {
|
|
|
+
|
|
|
+ detectStartEndPoint(mPosition);
|
|
|
+ removePointNearPosition(mPosition);
|
|
|
+ updateEditPointPosition(mPosition);
|
|
|
+ editMode = true;
|
|
|
+ repaint();
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
@Override
|
|
|
public void mouseReleased(MouseEvent e) {
|
|
|
System.out.println("mouseReleased");
|
|
|
- this.insertNewGraphPoint(editPosition);
|
|
|
- released = false;
|
|
|
- repaint();
|
|
|
+ if(editMode)
|
|
|
+ {
|
|
|
+ this.insertNewGraphPoint(editPosition);
|
|
|
+ editMode = false;
|
|
|
+ repaint();
|
|
|
+ }
|
|
|
+ saveGraph();
|
|
|
}
|
|
|
|
|
|
|
|
@@ -392,27 +462,6 @@ public class UnitGraph extends JPanel implements MouseListener, MouseMotionListe
|
|
|
System.out.println("componentResized");
|
|
|
calculateWidthHeight();
|
|
|
updateRepresentativePositions();
|
|
|
-
|
|
|
-
|
|
|
- if (init) {
|
|
|
- init = false;
|
|
|
-
|
|
|
- if (width == -1 && height == -1) {
|
|
|
- width = this.getWidth() - (border * 2);
|
|
|
- height = this.getHeight() - (border * 2);
|
|
|
- }
|
|
|
-
|
|
|
- scaleX = (this.getWidth() - (border * 2)) / width;
|
|
|
- scaleY = (this.getHeight() - (border * 2)) / height;
|
|
|
-
|
|
|
-
|
|
|
- this.getParent().getParent().setVisible(false);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- scaleX = (this.getWidth() - (border * 2)) / width;
|
|
|
- scaleY = (this.getHeight() - (border * 2)) / height;
|
|
|
- */
|
|
|
repaint();
|
|
|
}
|
|
|
|
|
@@ -429,391 +478,28 @@ public class UnitGraph extends JPanel implements MouseListener, MouseMotionListe
|
|
|
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");
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- * 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");
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- * Visualize the Switch on the Graph.
|
|
|
- *
|
|
|
- * @param s which should be visualized
|
|
|
- */
|
|
|
- public void repaintWithNewSwitch(HolonSwitch s) {
|
|
|
- System.out.println("repaintWithNewSwitch");
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- * 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();
|
|
|
-
|
|
|
-
|
|
|
- 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;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- 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());
|
|
|
-
|
|
|
- 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)
|
|
|
- {
|
|
|
-
|
|
|
- int x = (int )((p.getX() / width)* 100.0);
|
|
|
-
|
|
|
- int y = (int )((1.0-(p.getY() / height))* 100.0);
|
|
|
-
|
|
|
- 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)
|
|
|
- {
|
|
|
-
|
|
|
- int x = (int )((p.getX() / 100.0)* width);
|
|
|
-
|
|
|
- int y = (int )((1.0-(p.getY() / 100.0))* height);
|
|
|
-
|
|
|
- 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++) {
|
|
|
-
|
|
|
- if (xVal <= pointList.get(i + 1).getX()) {
|
|
|
-
|
|
|
- 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++) {
|
|
|
-
|
|
|
- if (xVal >= pointList.get(i).getX()) {
|
|
|
-
|
|
|
- c = buildCurve(pointList.get(i), pointList.get(i + 1));
|
|
|
- c.subdivide(cl, cr);
|
|
|
-
|
|
|
- if (cl.getX1() <= xVal * scaleX && cl.getX2() > xVal * scaleX) {
|
|
|
- c = cl;
|
|
|
-
|
|
|
- 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 {
|
|
|
- for (float j = 0; j < height; j += 0.1f) {
|
|
|
- if (c.contains(xVal * scaleX, j * scaleY)) {
|
|
|
- return (float) (j);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- } else {
|
|
|
- c = cr;
|
|
|
-
|
|
|
- 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 {
|
|
|
- for (float j = (float) (height - 1); j >= 0; j -= 0.1f) {
|
|
|
- if (c.contains(xVal * scaleX, j * scaleY)) {
|
|
|
- return (float) (j);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- 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;
|
|
|
- }
|
|
|
- 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;
|
|
|
- } else {
|
|
|
- double z = (sx * (qy - py) + sy * (px - qx)) / det;
|
|
|
- if (z < 0 || z > 1) {
|
|
|
- return 0;
|
|
|
- }
|
|
|
- return (float) (py + z * ry);
|
|
|
-
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
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<>();
|
|
|
+ public void setStretching(boolean selected) {
|
|
|
+ System.out.println("setStretching");
|
|
|
+ }
|
|
|
|
|
|
- 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();
|
|
|
- }
|
|
|
- }
|
|
|
+ public void setLocalPeriod(int localLength) {
|
|
|
+ System.out.println("setLocalPeriod");
|
|
|
+ }
|
|
|
|
|
|
- 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();
|
|
|
- }
|
|
|
-
|
|
|
- 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);
|
|
|
- }
|
|
|
-
|
|
|
+ public void repaintGraph(AbstractCpsObject cps) {
|
|
|
+
|
|
|
+ System.out.println("repaintGraph");
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
|
|
|
}
|