Kevin Trometer преди 8 години
родител
ревизия
7a938d53a9
променени са 1 файла, в които са добавени 113 реда и са изтрити 63 реда
  1. 113 63
      src/ui/view/UnitGraph.java

+ 113 - 63
src/ui/view/UnitGraph.java

@@ -76,6 +76,9 @@ public class UnitGraph extends JPanel implements MouseListener, MouseMotionListe
 	private double x = 0, y = 0;
 	private int x1, x2, y1, y2, ctrlx1, ctrly1, ctrlx2, ctrly2;
 
+	private int border = 4;
+	private int textWidth = 0;
+
 	/**
 	 * Constructor.
 	 * 
@@ -113,15 +116,18 @@ public class UnitGraph extends JPanel implements MouseListener, MouseMotionListe
 
 		graphCurve.reset();
 
+		border = (int) recSize.getX() >> 1;
 		// Draw the Vertical Lines
 		g2.setColor(Color.BLACK);
-		for (int i = 0; i < this.getWidth(); i += 7) {
-			g2.drawLine(i, 0, i, this.getHeight());
+		for (int i = border; i < this.getWidth() - border; i += border * 2) {
+			g2.drawLine(i, border, i, this.getHeight() - border);
 		}
+		g2.drawLine(this.getWidth() - border, border, this.getWidth() - border, this.getHeight() - border);
 
-		for (int i = 0; i < this.getHeight(); i += 7) {
-			g2.drawLine(0, i, this.getWidth(), i);
+		for (int i = border; i < this.getHeight() - border; i += border * 2) {
+			g2.drawLine(border, i, this.getWidth() - border, i);
 		}
+		g2.drawLine(border, this.getHeight() - border, this.getWidth() - border, this.getHeight() - border);
 
 		if (isElement) {
 			// array fillen
@@ -133,6 +139,9 @@ public class UnitGraph extends JPanel implements MouseListener, MouseMotionListe
 				g2.setColor(Color.BLACK);
 				for (int i = 0; i < pointList.size() - 1; i++) {
 					c = buildCurve(pointList.get(i), pointList.get(i + 1));
+					c.setCurve((x1 * scaleX) + border, (y1 * scaleY) + border, (ctrlx1 * scaleX) + border,
+							(ctrly1 * scaleY) + border, (ctrlx2 * scaleX) + border, (ctrly2 * scaleY) + border,
+							(x2 * scaleX) + border, (y2 * scaleY) + border);
 					graphCurve.append(c, true);
 				}
 				g2.draw(graphCurve);
@@ -140,32 +149,49 @@ public class UnitGraph extends JPanel implements MouseListener, MouseMotionListe
 				// Draw the Points
 				g2.setColor(Color.BLUE);
 				for (int i = 0; i < pointList.size() - 0; i++) {
-					g2.fillOval((int) (pointList.get(i).getX() * scaleX - recSize.getX() / 2),
-							(int) (pointList.get(i).getY() * scaleY - recSize.getY() / 2), (int) recSize.getX(),
-							(int) recSize.getY());
+					g2.fillOval((int) (pointList.get(i).getX() * scaleX - recSize.getX() / 2) + border,
+							(int) (pointList.get(i).getY() * scaleY - recSize.getY() / 2) + border,
+							(int) recSize.getX(), (int) recSize.getY());
 				}
 
 				// Iteration Value
-				if (arrayOfFloats != null) {
+				textWidth = g.getFontMetrics().stringWidth("" + arrayOfFloats[model.getCurIteration()]) + 2;
+				if (textWidth
+						+ (model.getCurIteration()) * (this.getWidth() - (border * 2)) / (model.getIterations() - 1) + 2
+						+ border <= this.getWidth()) {
 					g2.drawString("" + arrayOfFloats[model.getCurIteration()],
-							(model.getCurIteration()) * this.getWidth() / (model.getIterations() - 1) + 2,
+							(model.getCurIteration()) * (this.getWidth() - (border * 2)) / (model.getIterations() - 1)
+									+ 2 + border,
+							this.getHeight() - 10);
+				} else {
+					g2.drawString("" + arrayOfFloats[model.getCurIteration()],
+							(model.getCurIteration()) * (this.getWidth() - (border * 2)) / (model.getIterations() - 1)
+									+ border - textWidth,
 							this.getHeight() - 10);
 				}
+
 			}
 			// drag Information
-			if (tempP != null) {
+			if (tempP != null && pointDrag) {
 				dragInformation = "" + convertToValueY(getYValueAt((int) tempP.getX()));
-				g2.drawString(dragInformation, (int) (tempP.getX() * scaleX) + 10, (int) (tempP.getY() * scaleY) + 10);
+				textWidth = g.getFontMetrics().stringWidth("" + convertToValueY(getYValueAt((int) tempP.getX()))) + 2;
+				if (textWidth + (tempP.getX() * scaleX) + 10 + border <= this.getWidth()) {
+					g2.drawString(dragInformation, (int) (tempP.getX() * scaleX) + 10 + border,
+							(int) (tempP.getY() * scaleY) + 10);
+				} else {
+					g2.drawString(dragInformation, (int) (tempP.getX() * scaleX) - textWidth,
+							(int) (tempP.getY() * scaleY) + 10);
+				}
 			}
 
 			/*
 			 * // Actual Iteration Point Visualization g2.setColor(Color.RED);
 			 * if (arrayOfFloats != null) { for (int i = 0; i <
 			 * arrayOfFloats.length; i++) { g2.fillOval((int) (i * width /
-			 * (model.getIterations() - 1) * scaleX - recSize.getX() / 2), (int)
-			 * (convertToCanvasY((int) arrayOfFloats[i]) * scaleY -
-			 * recSize.getY() / 2), (int) recSize.getX(), (int) recSize.getY());
-			 * } }
+			 * (model.getIterations() - 1) * scaleX - recSize.getX() /
+			 * 2)+border, (int) (convertToCanvasY((int) arrayOfFloats[i]) *
+			 * scaleY - recSize.getY() / 2)+border, (int) recSize.getX(), (int)
+			 * recSize.getY()); } }
 			 */
 
 		} else if (isSwitch) {
@@ -177,8 +203,9 @@ public class UnitGraph extends JPanel implements MouseListener, MouseMotionListe
 				g2.setStroke(new BasicStroke(2));
 				g2.setColor(Color.BLACK);
 				for (int i = 0; i < pointList.size() - 1; i++) {
-					line = new Line2D.Double(pointList.get(i).getX() * scaleX, pointList.get(i).getY() * scaleY,
-							pointList.get(i + 1).getX() * scaleX, pointList.get(i + 1).getY() * scaleY);
+					line = new Line2D.Double(pointList.get(i).getX() * scaleX + border,
+							pointList.get(i).getY() * scaleY, pointList.get(i + 1).getX() * scaleX + border,
+							pointList.get(i + 1).getY() * scaleY);
 					graphCurve.append(line, true);
 				}
 				g2.draw(graphCurve);
@@ -186,27 +213,47 @@ public class UnitGraph extends JPanel implements MouseListener, MouseMotionListe
 				/*
 				 * // Draw the Points g2.setColor(Color.BLUE); for (int i = 0; i
 				 * < pointList.size() - 0; i++) { g2.fillOval((int)
-				 * (pointList.get(i).getX() * scaleX - recSize.getX() / 2),
-				 * (int) (pointList.get(i).getY() * scaleY - recSize.getY() /
-				 * 2), (int) recSize.getX(), (int) recSize.getY()); }
+				 * (pointList.get(i).getX() * scaleX - recSize.getX() / 2) +
+				 * border, (int) (pointList.get(i).getY() * scaleY -
+				 * recSize.getY() / 2), (int) recSize.getX(), (int)
+				 * recSize.getY()); }
 				 */
 
 				// Iteration Value
-				if (arrayOfBooleans != null) {
+				g2.setColor(Color.BLUE);
+				textWidth = g.getFontMetrics().stringWidth("" + arrayOfBooleans[model.getCurIteration()]) + 2;
+				if (textWidth
+						+ (model.getCurIteration()) * (this.getWidth() - (border * 2)) / (model.getIterations() - 1) + 2
+						+ border <= this.getWidth()) {
 					g2.drawString("" + arrayOfBooleans[model.getCurIteration()],
-							(model.getCurIteration()) * this.getWidth() / (model.getIterations() - 1) + 2,
+							(model.getCurIteration()) * (this.getWidth() - (border * 2)) / (model.getIterations() - 1)
+									+ 2 + border,
+							this.getHeight() - 10);
+				} else {
+					g2.drawString("" + arrayOfBooleans[model.getCurIteration()],
+							(model.getCurIteration()) * (this.getWidth() - (border * 2)) / (model.getIterations() - 1)
+									+ border - textWidth,
 							this.getHeight() - 10);
 				}
+
 			}
-			if (tempP != null) {
+			// When the switch graph is dragged
+			if (tempP != null && pointDrag) {
 				try {
 					int i;
-					for (i = 0; (i * this.getWidth() / (model.getIterations() - 1) < getMousePosition().getX()); i++) {
-
+					for (i = 0; (i * (this.getWidth() - (border * 2)) / (model.getIterations() - 1)
+							+ border < getMousePosition().getX()); i++) {
 					}
 					dragInformation = "" + i;
-					g2.drawString(dragInformation, (int) (getMousePosition().getX()) + 10,
-							(int) (getMousePosition().getY()) + 10);
+					textWidth = g.getFontMetrics().stringWidth("" + convertToValueY(getYValueAt((int) tempP.getX())))
+							+ 2;
+					if (textWidth + (tempP.getX() * scaleX) + 10 + border <= this.getWidth()) {
+						g2.drawString(dragInformation, (int) (getMousePosition().getX()) + 10 + border,
+								(int) (getMousePosition().getY() * scaleY) + 10);
+					} else {
+						g2.drawString(dragInformation, (int) (getMousePosition().getX()) - textWidth,
+								(int) (getMousePosition().getY() * scaleY) + 10);
+					}
 				} catch (Exception e) {
 				}
 			}
@@ -215,8 +262,9 @@ public class UnitGraph extends JPanel implements MouseListener, MouseMotionListe
 		// Iteration Line
 		g2.setColor(Color.BLUE);
 		g2.setStroke(new BasicStroke(1));
-		g2.drawLine((model.getCurIteration()) * this.getWidth() / (model.getIterations() - 1), 0,
-				(model.getCurIteration()) * this.getWidth() / (model.getIterations() - 1), this.getHeight());
+		g2.drawLine(border + (model.getCurIteration()) * (this.getWidth() - border * 2) / (model.getIterations() - 1),
+				0, border + (model.getCurIteration()) * (this.getWidth() - border * 2) / (model.getIterations() - 1),
+				this.getHeight());
 
 		// algorithmus
 		controller.calculateStateForTimeStep(model.getCurIteration());
@@ -241,17 +289,18 @@ public class UnitGraph extends JPanel implements MouseListener, MouseMotionListe
 		if (pointDrag && tempP != null) {
 			// Out of Bounds verhindern
 			int i = pointList.indexOf(tempP);
-			x = e.getX() / scaleX;
-			y = e.getY() / scaleY;
+			x = (e.getX() - border) / scaleX;
+			y = (e.getY() - border) / scaleY;
 			// y
-			if (e.getY() <= 0) {
+			if (e.getY() <= border) {
 				y = 0;
-			} else if (this.getHeight() <= e.getY()) {
-				y = this.getHeight() / scaleY;
+			} else if (this.getHeight() - border <= e.getY()) {
+				y = (this.getHeight() - border * 2) / scaleY;
 			}
 			// x
-			if (tempP == pointList.getFirst() || tempP == pointList.getLast() || pointList.get(i + 1).getX() < x+3
-					|| pointList.get(i - 1).getX() > x-3 || pointList.getFirst().getX() > x-3 || pointList.getLast().getX() < x+3) {
+			if (tempP == pointList.getFirst() || tempP == pointList.getLast() || pointList.get(i + 1).getX() < x + 2
+					|| pointList.get(i - 1).getX() > x - 2 || pointList.getFirst().getX() > x - 2
+					|| pointList.getLast().getX() < x + 2) {
 				x = tempP.getX();
 			}
 			tempP.setLocation(x, y);
@@ -269,7 +318,7 @@ public class UnitGraph extends JPanel implements MouseListener, MouseMotionListe
 	public void switchDragged(MouseEvent e) {
 		if (pointDrag && tempP != null && tempP != pointList.getFirst() && tempP != pointList.getLast()) {
 			int i = pointList.indexOf(tempP);
-			x = e.getX() / scaleX;
+			x = (e.getX() - border) / scaleX;
 
 			if (pointList.get(i + 1).getY() == tempP.getY()) {
 				// x
@@ -325,8 +374,8 @@ public class UnitGraph extends JPanel implements MouseListener, MouseMotionListe
 		boolean added = false;
 		boolean deletePoint = false;
 
-		int x = (int) (e.getX() / scaleX);
-		double y = e.getY() / scaleY;
+		int x = (int) ((e.getX() - border) / scaleX);
+		double y = (e.getY() - border) / scaleY;
 
 		// Click on Point
 		tempP = null;
@@ -353,7 +402,7 @@ public class UnitGraph extends JPanel implements MouseListener, MouseMotionListe
 						break;
 					}
 					if (x < pointList.get(i).getX() && !added) {
-						if (e.getY() <= 0) {
+						if (e.getY() <= border) {
 							pointList.add(i, new Point((int) (x), 0));
 						} else {
 							pointList.add(i, new Point((int) (x), (int) y));
@@ -384,7 +433,7 @@ public class UnitGraph extends JPanel implements MouseListener, MouseMotionListe
 		boolean added = false;
 		boolean deletePoint = false;
 
-		double x = e.getX() / scaleX;
+		double x = (e.getX() - border) / scaleX;
 		e.getY();
 
 		// Halbe Iterations Distanz
@@ -411,7 +460,7 @@ public class UnitGraph extends JPanel implements MouseListener, MouseMotionListe
 						// double p1, p2 um location der points zu bestimmen
 						double p1 = pointList.get(i - 1).getX();
 						double p2 = pointList.get(i).getX();
-						// Punkte hinzufgen, je nachdem ob true oder false
+						// Punkte hinzufügen, je nachdem ob true oder false
 						if (pointList.get(i - 1).getY() != (int) (height / 6)
 								&& pointList.get(i).getY() != (int) (height / 6)) {
 							pointList.add(i, new Point((int) ((x + p2) / 2 + dist), (int) (height - height / 6)));
@@ -435,13 +484,13 @@ public class UnitGraph extends JPanel implements MouseListener, MouseMotionListe
 			if (deletePoint && tempP.getX() != 0
 					&& (tempP.getX() != this.getWidth() / scaleX || tempP != pointList.getLast())) {
 				int i = pointList.indexOf(tempP);
-				if (tempP.getY() == (int) (height / 6)) {
+				//If Right, else if Left
+				if (tempP.getY() == (int) (height / 6) && i < pointList.size() - 1 && i>0) {
 					pointList.remove(i);
 					pointList.remove(i - 1);
 					pointList.remove(i - 2);
 					pointList.remove(i - 3);
-				}
-				if (tempP.getY() == (int) (height - height / 6)) {
+				} else if (tempP.getY() == (int) (height - height / 6)) {
 					pointList.remove(i + 2);
 					pointList.remove(i + 1);
 					pointList.remove(i);
@@ -478,17 +527,17 @@ public class UnitGraph extends JPanel implements MouseListener, MouseMotionListe
 			init = false;
 			// for scale on the first initialisation
 			if (width == -1 && height == -1) {
-				width = this.getWidth();
-				height = this.getHeight();
+				width = this.getWidth() - (border * 2);
+				height = this.getHeight() - (border * 2);
 			}
-
-			scaleX = this.getWidth() / width;
-			scaleY = this.getHeight() / height;
+			// Scale
+			scaleX = (this.getWidth() - (border * 2)) / width;
+			scaleY = (this.getHeight() - (border * 2)) / height;
 		}
 
 		// Scale
-		scaleX = this.getWidth() / width;
-		scaleY = this.getHeight() / height;
+		scaleX = (this.getWidth() - (border * 2)) / width;
+		scaleY = (this.getHeight() - (border * 2)) / height;
 		repaint();
 	}
 
@@ -528,7 +577,7 @@ public class UnitGraph extends JPanel implements MouseListener, MouseMotionListe
 			pointList.addLast(new Point((int) (width) + 4, (int) (height / 6)));
 		} else {
 			pointList.addFirst(new Point(0, 0));
-			pointList.addLast(new Point((int) (this.getWidth() / scaleX), 0));
+			pointList.addLast(new Point((int) ((this.getWidth() - (border * 2)) / scaleX), 0));
 		}
 		repaint();
 	}
@@ -571,7 +620,7 @@ public class UnitGraph extends JPanel implements MouseListener, MouseMotionListe
 		// First time clicked on the Element
 		if (pointList.isEmpty()) {
 			pointList.addFirst(new Point(0, 0));
-			pointList.addLast(new Point((int) (this.getWidth() / scaleX), 0));
+			pointList.addLast(new Point((int) ((this.getWidth() - (border * 2)) / scaleX), 0));
 		}
 		repaint();
 	}
@@ -589,8 +638,8 @@ public class UnitGraph extends JPanel implements MouseListener, MouseMotionListe
 		isElement = false;
 		// First time clicked on the Element
 		if (pointList.isEmpty()) {
-			pointList.addFirst(new Point(-4, (int) (height / 6)));
-			pointList.addLast(new Point((int) (width) + 4, (int) (height / 6)));
+			pointList.addFirst(new Point(-border, (int) (height / 6)));
+			pointList.addLast(new Point((int) ((this.getWidth() - border) / scaleX), (int) (height / 6)));
 		}
 		repaint();
 	}
@@ -612,14 +661,14 @@ public class UnitGraph extends JPanel implements MouseListener, MouseMotionListe
 		y2 = (int) p2.getY();
 
 		// calculate the controllpoints
-		ctrlx1 = (int) p1.getX() + ((int) p2.getX() - (int) p1.getX()) / 2;
-		ctrlx2 = (int) p2.getX() - ((int) p2.getX() - (int) p1.getX()) / 2;
+		ctrlx1 = x1 + (x2 - x1) / 2;
+		ctrlx2 = x2 - (x2 - x1) / 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 = y1 + (y2 - y1) / 10;
+			ctrly2 = y2 - (y2 - y1) / 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 = y1 - (y1 - y2) / 10;
+			ctrly2 = y2 + (y1 - y2) / 10;
 		}
 
 		// set the curve
@@ -634,7 +683,7 @@ public class UnitGraph extends JPanel implements MouseListener, MouseMotionListe
 	public void fillArrayofBooleans() {
 		for (int i = 0; i < arrayOfBooleans.length; i++) {
 			int t = (int) getYValueAt2((int) (i * width / (model.getIterations() - 1)));
-			if (t == (int) (height / 6)) {
+			if (t <= height / 2) {
 				arrayOfBooleans[i] = true;
 			} else {
 				arrayOfBooleans[i] = false;
@@ -729,6 +778,7 @@ public class UnitGraph extends JPanel implements MouseListener, MouseMotionListe
 				}
 			}
 		}
+		// else
 		return getYValueAt(xVal);
 	}