|
@@ -90,9 +90,8 @@ public class UnitGraph extends JPanel implements MouseListener, MouseMotionListe
|
|
|
*/
|
|
|
public void paintComponent(Graphics g) {
|
|
|
super.paintComponent(g);
|
|
|
-
|
|
|
- //System.out.println("paint");
|
|
|
Graphics2D g2D = (Graphics2D) g;
|
|
|
+ drawGrid(g2D);
|
|
|
g2D.setColor(Color.BLACK);
|
|
|
g2D.setRenderingHints(new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON));
|
|
|
g2D.setStroke(new BasicStroke(2));
|
|
@@ -109,7 +108,19 @@ public class UnitGraph extends JPanel implements MouseListener, MouseMotionListe
|
|
|
g2D.setStroke(new BasicStroke(1));
|
|
|
drawCurrentIterartionLine(g2D);
|
|
|
}
|
|
|
- //TODO -> New Section
|
|
|
+
|
|
|
+ private void drawGrid(Graphics2D g2D) {
|
|
|
+ g2D.setStroke(new BasicStroke(1));
|
|
|
+ g2D.setColor(Color.lightGray);
|
|
|
+ int amountOfLines = 10;
|
|
|
+ int width = widthWithBorder + 2 * border;
|
|
|
+ int height = heightWithBorder;
|
|
|
+ for(int i = 0; i<=amountOfLines; i++)
|
|
|
+ {
|
|
|
+ int linehieght = (int) (((double)i/ (double) amountOfLines) * (double) height) + border;
|
|
|
+ g2D.drawLine(0, linehieght, width, linehieght);
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
private void drawCurrentIterartionLine(Graphics2D g)
|
|
|
{
|
|
@@ -132,7 +143,6 @@ public class UnitGraph extends JPanel implements MouseListener, MouseMotionListe
|
|
|
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;
|
|
@@ -148,7 +158,10 @@ public class UnitGraph extends JPanel implements MouseListener, MouseMotionListe
|
|
|
private void drawUnitGraph(Graphics2D g) {
|
|
|
switch(actualGraphType) {
|
|
|
case boolGraph:
|
|
|
- drawBoolGraph(g);
|
|
|
+ if(editMode)
|
|
|
+ drawBoolGraphWithEditPosition(g);
|
|
|
+ else
|
|
|
+ drawBoolGraph(g);
|
|
|
break;
|
|
|
case doubleGraph:
|
|
|
if(editMode)
|
|
@@ -183,9 +196,83 @@ public class UnitGraph extends JPanel implements MouseListener, MouseMotionListe
|
|
|
}
|
|
|
|
|
|
private void drawBoolGraph(Graphics2D g) {
|
|
|
- throw new NotImplementedException();
|
|
|
+ System.out.println("DoImplement");
|
|
|
+ if(actualGraphPoints.size() <= 1) return;
|
|
|
+ LinkedList<Position> cornerPoints = new LinkedList<Position>();
|
|
|
+ ListIterator<UnitGraphPoint> iter = actualGraphPoints.listIterator();
|
|
|
+ Position actual = actualGraphPoints.getFirst().displayedPosition;
|
|
|
+ Path2D.Double path = new Path2D.Double();
|
|
|
+ path.moveTo(actual.x, actual.y);
|
|
|
+ while (iter.hasNext())
|
|
|
+ {
|
|
|
+ Position target = iter.next().displayedPosition;
|
|
|
+ //BooleanConnection
|
|
|
+ path.lineTo(target.x, actual.y); //line to corner
|
|
|
+ cornerPoints.add(new Position(target.x, actual.y)); //save corner
|
|
|
+ path.lineTo(target.x, target.y); //line to next Point
|
|
|
+
|
|
|
+ actual = target;
|
|
|
+ }
|
|
|
+ g.draw(path);
|
|
|
+ //Draw the Points on the Corner that dont exist in Data but should be visual
|
|
|
+ g.setColor(dotColor);
|
|
|
+ for(Position p: cornerPoints)
|
|
|
+ {
|
|
|
+ drawDot(g, p);
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
|
|
|
+ private void drawBoolGraphWithEditPosition(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);
|
|
|
+ }
|
|
|
+ g.setColor(Color.BLACK);
|
|
|
+ drawBoolGraphFromList(g, before);
|
|
|
+ g.setColor(Color.BLACK);
|
|
|
+ drawBoolGraphFromList(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);
|
|
|
+ drawBoolGraphFromList(g, middle);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private void drawBoolGraphFromList(Graphics2D g, LinkedList<Position> list) {
|
|
|
+ if(list.size() <= 1) return;
|
|
|
+ ListIterator<Position> iter = list.listIterator();
|
|
|
+ LinkedList<Position> cornerPoints = new LinkedList<Position>();
|
|
|
+ Position actual = list.getFirst();
|
|
|
+ Path2D.Double path = new Path2D.Double();
|
|
|
+ path.moveTo(actual.x, actual.y);
|
|
|
+ while (iter.hasNext())
|
|
|
+ {
|
|
|
+ Position target = iter.next();
|
|
|
+ //BooleanConnection
|
|
|
+ path.lineTo(target.x, actual.y); //line to corner
|
|
|
+ cornerPoints.add(new Position(target.x, actual.y)); //save corner
|
|
|
+ path.lineTo(target.x, target.y); //line to next Point
|
|
|
+ actual = target;
|
|
|
+ }
|
|
|
+ g.draw(path);
|
|
|
+ g.setColor(dotColor);
|
|
|
+ for(Position p: cornerPoints)
|
|
|
+ {
|
|
|
+ drawDot(g, p);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
private void drawDoubleGraph(Graphics2D g) {
|
|
|
if(actualGraphPoints.isEmpty()) throw new IndexOutOfBoundsException("A Graph Without Points is not supportet jet");
|
|
|
ListIterator<UnitGraphPoint> iter = actualGraphPoints.listIterator();
|
|
@@ -271,7 +358,7 @@ public class UnitGraph extends JPanel implements MouseListener, MouseMotionListe
|
|
|
ListIterator<UnitGraphPoint> iter = actualGraphPoints.listIterator();
|
|
|
while (iter.hasNext())
|
|
|
{
|
|
|
- if(mPosition.squareDistance(iter.next().displayedPosition) < clickThreshholdSquared)
|
|
|
+ if(near(mPosition,iter.next().displayedPosition))
|
|
|
{
|
|
|
iter.remove();
|
|
|
break;
|
|
@@ -283,15 +370,29 @@ public class UnitGraph extends JPanel implements MouseListener, MouseMotionListe
|
|
|
{
|
|
|
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;
|
|
|
+ if(near(mPosition, first.displayedPosition)) editPoint = pointType.StartPoint;
|
|
|
+ else if(near(mPosition, last.displayedPosition)) editPoint = pointType.EndPoint;
|
|
|
else editPoint = pointType.Normal;
|
|
|
}
|
|
|
+
|
|
|
+ private boolean near(Position actual, Position target) {
|
|
|
+ switch(actualGraphType)
|
|
|
+ {
|
|
|
+ case boolGraph: //Distance only with X
|
|
|
+ int xDis = target.x - actual.x;
|
|
|
+ return xDis * xDis < clickThreshholdSquared;
|
|
|
+ case doubleGraph:
|
|
|
+ return actual.squareDistance(target) < clickThreshholdSquared;
|
|
|
+ default:
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
private void updateEditPointPosition(Position newPosition) {
|
|
|
//make it in the bounds of the UnitGraph no Point out of the Border
|
|
|
Position currentPosition = setInBounds(newPosition);
|
|
|
if(editPoint != pointType.Normal) attachToBorder(currentPosition);
|
|
|
+ if(actualGraphType == Graphtype.boolGraph) snapBoolean(currentPosition);
|
|
|
this.editPosition = currentPosition;
|
|
|
}
|
|
|
|
|
@@ -301,6 +402,18 @@ public class UnitGraph extends JPanel implements MouseListener, MouseMotionListe
|
|
|
return p;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ private Position snapBoolean(Position p)
|
|
|
+ {
|
|
|
+ if (p.y < border + heightWithBorder / 2) {
|
|
|
+ p.y = border;
|
|
|
+ } else {
|
|
|
+ p.y = border + heightWithBorder;
|
|
|
+ }
|
|
|
+ return p;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
private Point.Double getBezierPoint(double t, Point.Double p0, Point.Double p1,Point.Double p2,Point.Double p3) {
|
|
|
/*
|
|
|
* Calculate Beziér:
|