|
@@ -11,10 +11,10 @@ import java.awt.event.MouseEvent;
|
|
|
import java.awt.event.MouseListener;
|
|
|
import java.awt.event.MouseMotionListener;
|
|
|
import java.awt.geom.CubicCurve2D;
|
|
|
+import java.awt.geom.Line2D;
|
|
|
import java.util.LinkedList;
|
|
|
import java.awt.Point;
|
|
|
|
|
|
-import javax.swing.JButton;
|
|
|
import javax.swing.JPanel;
|
|
|
|
|
|
import classes.HolonElement;
|
|
@@ -36,6 +36,8 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
|
|
|
private double scaleX;
|
|
|
private double scaleY;
|
|
|
|
|
|
+ private float[] arrayOfValue = null;
|
|
|
+
|
|
|
private double width = -1;
|
|
|
private double height = -1;
|
|
|
|
|
@@ -71,6 +73,13 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
|
|
|
public void paintComponent(Graphics g) {
|
|
|
super.paintComponent(g);
|
|
|
|
|
|
+ if (arrayOfValue != null) {
|
|
|
+ for (int i = 0; i < arrayOfValue.length; i++) {
|
|
|
+ System.out.println(""+arrayOfValue[i]);
|
|
|
+ arrayOfValue[i] = getYValueAt((int) (i * width / (ITERATIONS - 1)));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
g2 = (Graphics2D) g;
|
|
|
RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
|
|
g2.setRenderingHints(rh);
|
|
@@ -82,7 +91,7 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
|
|
|
g2.drawLine((i) * this.getWidth() / (ITERATIONS - 1), MAXIMUM, (i) * this.getWidth() / (ITERATIONS - 1),
|
|
|
this.getHeight());
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
for (int i = 0; i < ITERATIONS; i++) {
|
|
|
g2.drawLine(0, (i) * this.getHeight() / (ITERATIONS - 1), this.getWidth(),
|
|
|
(i) * this.getHeight() / (ITERATIONS - 1));
|
|
@@ -91,13 +100,7 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
|
|
|
|
|
|
g2.setColor(Color.BLACK);
|
|
|
for (int i = 0; i < pointList.size() - 1; i++) {
|
|
|
-
|
|
|
- * g2.drawLine((int) (pointList.get(i).getX() * scaleX), (int)
|
|
|
- * (pointList.get(i).getY() * scaleY), (int) (pointList.get(i +
|
|
|
- * 1).getX() * scaleX), (int) (pointList.get(i + 1).getY() *
|
|
|
- * scaleY));
|
|
|
- */
|
|
|
- c = buildCurve(pointList.get(i), pointList.get(i+1));
|
|
|
+ c = buildCurve(pointList.get(i), pointList.get(i + 1));
|
|
|
g2.draw(c);
|
|
|
}
|
|
|
|
|
@@ -108,8 +111,8 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
|
|
|
(int) (pointList.get(i).getY() * scaleY - recSize.getY() / 2), (int) recSize.getX(),
|
|
|
(int) recSize.getY());
|
|
|
}
|
|
|
- g2.drawLine((model.getCurIteration()) * this.getWidth() / (ITERATIONS - 1), MAXIMUM, (model.getCurIteration()) * this.getWidth() / (ITERATIONS - 1),
|
|
|
- this.getHeight());
|
|
|
+ g2.drawLine((model.getCurIteration()) * this.getWidth() / (ITERATIONS - 1), MAXIMUM,
|
|
|
+ (model.getCurIteration()) * this.getWidth() / (ITERATIONS - 1), this.getHeight());
|
|
|
|
|
|
}
|
|
|
|
|
@@ -222,10 +225,10 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
|
|
|
|
|
|
scaleX = this.getWidth() / width;
|
|
|
scaleY = this.getHeight() / height;
|
|
|
-
|
|
|
+
|
|
|
if (pointList.isEmpty()) {
|
|
|
pointList.addFirst(new Point(0, MAXIMUM));
|
|
|
- pointList.addLast(new Point((int) (this.getWidth()/scaleX), MAXIMUM));
|
|
|
+ pointList.addLast(new Point((int) (this.getWidth() / scaleX), MAXIMUM));
|
|
|
}
|
|
|
}
|
|
|
scaleX = this.getWidth() / width;
|
|
@@ -278,7 +281,7 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
|
|
|
* ele, which should be visualized
|
|
|
*/
|
|
|
public void repaintWithNewElement(HolonElement ele) {
|
|
|
- float[] arrayOfValue = ele.getEnergyAt();
|
|
|
+ arrayOfValue = ele.getEnergyAt();
|
|
|
tempElement = ele;
|
|
|
pointList = ele.getGraphPoints();
|
|
|
init = true;
|
|
@@ -286,30 +289,74 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
|
|
|
repaint();
|
|
|
}
|
|
|
|
|
|
- public CubicCurve2D buildCurve(Point p1, Point p2){
|
|
|
+
|
|
|
+ * Build a Curve for the Graph
|
|
|
+ *
|
|
|
+ * @param Point,Point
|
|
|
+ * ,startpoint p1 and endpoint p2
|
|
|
+ *
|
|
|
+ * @return CubicCurve2D, c, the CubicCurve2D for the Graph
|
|
|
+ */
|
|
|
+ public CubicCurve2D buildCurve(Point p1, Point p2) {
|
|
|
x1 = (int) p1.getX();
|
|
|
y1 = (int) p1.getY();
|
|
|
x2 = (int) p2.getX();
|
|
|
y2 = (int) p2.getY();
|
|
|
- ctrlx1 = (int) p1.getX()
|
|
|
- + ((int) p2.getX() - (int) p1.getX()) / 2;
|
|
|
- ctrlx2 = (int) p2.getX()
|
|
|
- - ((int) p2.getX() - (int) p1.getX()) / 2;
|
|
|
+ ctrlx1 = (int) p1.getX() + ((int) p2.getX() - (int) p1.getX()) / 2;
|
|
|
+ ctrlx2 = (int) p2.getX() - ((int) p2.getX() - (int) p1.getX()) / 2;
|
|
|
if (y1 < y2) {
|
|
|
- ctrly1 = (int) p1.getY()
|
|
|
- + ((int) p2.getY() - (int) p1.getY()) / 10;
|
|
|
- ctrly2 = (int) p2.getY()
|
|
|
- - ((int) p2.getY() - (int) p1.getY()) / 10;
|
|
|
+ ctrly1 = (int) p1.getY() + ((int) p2.getY() - (int) p1.getY()) / 10;
|
|
|
+ ctrly2 = (int) p2.getY() - ((int) p2.getY() - (int) p1.getY()) / 10;
|
|
|
} else {
|
|
|
- ctrly1 = (int) p1.getY()
|
|
|
- - ((int) p1.getY() - (int) p2.getY()) / 10;
|
|
|
- ctrly2 = (int) p2.getY()
|
|
|
- + ((int) p1.getY() - (int) p2.getY()) / 10;
|
|
|
+ ctrly1 = (int) p1.getY() - ((int) p1.getY() - (int) p2.getY()) / 10;
|
|
|
+ ctrly2 = (int) p2.getY() + ((int) p1.getY() - (int) p2.getY()) / 10;
|
|
|
}
|
|
|
|
|
|
c.setCurve(x1 * scaleX, y1 * scaleY, ctrlx1 * scaleX, ctrly1 * scaleY, ctrlx2 * scaleX, ctrly2 * scaleY,
|
|
|
x2 * scaleX, y2 * scaleY);
|
|
|
return c;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
+ *
|
|
|
+ * @param xVal,
|
|
|
+ * the x value for the y value
|
|
|
+ * @return y, the value at x
|
|
|
+ */
|
|
|
+ public int getYValueAt(int xVal) {
|
|
|
+ for (int i = 0; i < pointList.size() - 1; i++) {
|
|
|
+
|
|
|
+ if (xVal > pointList.get(i + 1).getX()) {
|
|
|
+ x = pointList.get(i + 1).getX();
|
|
|
+ y = pointList.get(i).getX();
|
|
|
+
|
|
|
+
|
|
|
+ c = buildCurve(pointList.get(i), pointList.get(i + 1));
|
|
|
+ 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 (int) getIntersectionPoint(l1, l2).getY();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ public Point getIntersectionPoint(Line2D l1, Line2D l2) {
|
|
|
+ if (!l1.intersectsLine(l2))
|
|
|
+ return 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 null;
|
|
|
+ } else {
|
|
|
+ double z = (sx * (qy - py) + sy * (px - qx)) / det;
|
|
|
+ if (z == 0 || z == 1)
|
|
|
+ return null;
|
|
|
+ return new Point((int) (px + z * rx), (int) (py + z * ry));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
}
|