Browse Source

switch unitgtaph, man kann ihn shcon benutzen, ist aber noch verbugged

Kevin Trometer 8 years ago
parent
commit
20c4c0e041
1 changed files with 59 additions and 37 deletions
  1. 59 37
      src/ui/view/UnitGraph.java

+ 59 - 37
src/ui/view/UnitGraph.java

@@ -143,7 +143,7 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 				fillArrayofBooleans();
 
 				// Draw the Lines
-				g2.setStroke(new BasicStroke(1));
+				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,
@@ -151,17 +151,15 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 					graphCurve.append(line, true);
 				}
 				g2.draw(graphCurve);
-				
+
 				/*
-				// 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());
-				}
-				*/
-				
+				 * // 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()); }
+				 */
+
 				// Iteration Value
 				if (arrayOfBooleans != null) {
 					g2.drawString("" + arrayOfBooleans[model.getCurIteration()],
@@ -219,7 +217,26 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 	 * @param e
 	 */
 	public void switchDragged(MouseEvent e) {
-		// TODO Switch dragged zeugs kommt hier hin
+		if (pointDrag && tempP != null && tempP != pointList.getFirst() && tempP != pointList.getLast()) {
+			int i = pointList.indexOf(tempP);
+			x = e.getX() / scaleX;
+
+			if (pointList.get(i + 1).getY() == tempP.getY()) {
+				// x
+				if (pointList.get(i + 1).getX() <= x || pointList.get(i - 2).getX() >= x) {
+					x = tempP.getX();
+				}
+				pointList.get(i - 1).setLocation(x, pointList.get(i - 1).getY());
+			} else {
+				// x
+				if (pointList.get(i + 2).getX() <= x || pointList.get(i - 1).getX() >= x) {
+					x = tempP.getX();
+				}
+				pointList.get(i + 1).setLocation(x, pointList.get(i + 1).getY());
+			}
+			tempP.setLocation(x, tempP.getY());
+			repaint();
+		}
 	}
 
 	@Override
@@ -314,12 +331,14 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 		double x = e.getX() / scaleX;
 		double y = e.getY() / scaleY;
 
+		// Halbe Iterations Distanz
+		double dist = (width / (model.getIterations() - 1)) / 2;
+
 		// Click on Point
 		tempP = null;
 		if (pointList != null) {
 			for (Point p : pointList) {
-				if (x >= p.getX() - recSize.getX() / 2 && y >= p.getY() - recSize.getY() / 2
-						&& x <= p.getX() + recSize.getX() / 2 && y <= p.getY() * scaleY + recSize.getY() / 2) {
+				if (x >= p.getX() - dist && x <= p.getX() + dist) {
 					if (e.getButton() == MouseEvent.BUTTON3) {
 						tempP = p;
 						deletePoint = true;
@@ -331,28 +350,25 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 			}
 			// New Point
 			if (!pointDrag && e.getButton() != MouseEvent.BUTTON3 && x != 0 && x != width) {
-				for (int i = 0; i < pointList.size(); i++) {
-					if (x < pointList.get(i).getX() && !added) {
-						double it = 0;
-						double dist = (1 * this.getWidth() / (model.getIterations() - 1))/scaleX/2;
-						//Iterationspuunkt in der Nähe vom Click holen
-						for (int j = 0; (j < model.getIterations() && it < x); j++) {
-							it = (j * this.getWidth() / (model.getIterations() - 1))/scaleX;
+				for (int i = 0; i < pointList.size() && !added; i++) {
+					if (x < pointList.get(i).getX() - dist) {
+						// Punkt hinzufügen, je nachdem ob es oberhalb oder
+						// unterhalb der hälfte ist
+						if (y < height / 2 && (pointList.get(i - 1).getY() == height - 1
+								&& pointList.get(i).getY() == height - 1)) {
+							pointList.add(i, new Point((int) (x + dist), (int) height - 1));
+							pointList.add(i, new Point((int) (x + dist), 0));
+							pointList.add(i, new Point((int) (x - dist), 0));
+							pointList.add(i, new Point((int) (x - dist), (int) height - 1));
+							added = true;
+						} else if (y >= height / 2 && (pointList.get(i - 1).getY() != height - 1
+								|| pointList.get(i).getY() != height - 1)) {
+							pointList.add(i, new Point((int) (x + dist), 0));
+							pointList.add(i, new Point((int) (x + dist), (int) height - 1));
+							pointList.add(i, new Point((int) (x - dist), (int) height - 1));
+							pointList.add(i, new Point((int) (x - dist), 0));
+							added = true;
 						}
-							
-						//Punkt hinzufügen, je nachdem ob es oberhalb oder unterhalb der hälfte ist
-						if (y < height / 2 && (pointList.get(i - 1).getY() != 0 || pointList.get(i).getY() != 0)) {
-							pointList.add(i, new Point((int) (it+dist), (int) height-1));
-							pointList.add(i, new Point((int) (it+dist), 0));
-							pointList.add(i, new Point((int) (it-dist), 0));
-							pointList.add(i, new Point((int) (it-dist), (int) height-1));
-						} else if (x >= height/2 && (pointList.get(i-1).getY() != height-1 || pointList.get(i ).getY() != height-1)) {
-							pointList.add(i, new Point((int) (it+dist), 0));
-							pointList.add(i, new Point((int) (it+dist), (int) height-1));
-							pointList.add(i, new Point((int) (it-dist), (int) height-1));
-							pointList.add(i, new Point((int) (it-dist), 0));
-						}
-						added = true;
 					}
 				}
 			}
@@ -484,7 +500,7 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 		// 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) (width), 0));
 		}
 		repaint();
 	}
@@ -525,7 +541,13 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 	 */
 	public void fillArrayofBooleans() {
 		for (int i = 0; i < arrayOfBooleans.length; i++) {
-			arrayOfBooleans[i] = true;
+			int t = (int) getYValueAt_2((int) (i * width / (model.getIterations() - 1)));
+			if (t == 0) {
+				arrayOfBooleans[i] = true;
+			} else {
+				arrayOfBooleans[i] = false;
+			}
+
 		}
 	}