瀏覽代碼

switch graph ansatz

Kevin Trometer 8 年之前
父節點
當前提交
291daeef44
共有 1 個文件被更改,包括 63 次插入9 次删除
  1. 63 9
      src/ui/view/UnitGraph.java

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

@@ -120,7 +120,7 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 							(int) (pointList.get(i).getY() * scaleY - recSize.getY() / 2), (int) recSize.getX(),
 							(int) recSize.getY());
 				}
-				
+
 				// Iteration Value
 				if (arrayOfFloats != null) {
 					g2.drawString("" + arrayOfFloats[model.getCurIteration()],
@@ -143,14 +143,16 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 				fillArrayofBooleans();
 
 				// Draw the Lines
-				g2.setStroke(new BasicStroke(2));
+				g2.setStroke(new BasicStroke(1));
 				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, pointList.get(i).getY() * scaleY,
+							pointList.get(i + 1).getX() * scaleX, pointList.get(i + 1).getY() * scaleY);
 					graphCurve.append(line, true);
 				}
 				g2.draw(graphCurve);
-
+				
+				/*
 				// Draw the Points
 				g2.setColor(Color.BLUE);
 				for (int i = 0; i < pointList.size() - 0; i++) {
@@ -158,10 +160,11 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 							(int) (pointList.get(i).getY() * scaleY - recSize.getY() / 2), (int) recSize.getX(),
 							(int) recSize.getY());
 				}
+				*/
 				
 				// Iteration Value
-				if (arrayOfFloats != null) {
-					g2.drawString("" + arrayOfFloats[model.getCurIteration()],
+				if (arrayOfBooleans != null) {
+					g2.drawString("" + arrayOfBooleans[model.getCurIteration()],
 							(model.getCurIteration()) * this.getWidth() / (model.getIterations() - 1) + 2,
 							this.getHeight() - 10);
 				}
@@ -260,6 +263,7 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 		// Click on Point
 		tempP = null;
 		if (pointList != null) {
+			// look if a point was clicked
 			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) {
@@ -304,6 +308,56 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 	 * @param e
 	 */
 	public void switchPressed(MouseEvent e) {
+		boolean added = false;
+		boolean deletePoint = false;
+
+		double x = e.getX() / scaleX;
+		double y = e.getY() / scaleY;
+
+		// 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 (e.getButton() == MouseEvent.BUTTON3) {
+						tempP = p;
+						deletePoint = true;
+					} else {
+						pointDrag = true;
+						tempP = p;
+					}
+				}
+			}
+			// 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;
+						}
+							
+						//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;
+					}
+				}
+			}
+			repaint();
+		}
 		// TODO Siwtch pressed zeugs hier hin
 	}
 
@@ -429,8 +483,8 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 		isElement = false;
 		// First time clicked on the Element
 		if (pointList.isEmpty()) {
-			pointList.addFirst(new Point(0, (int) (this.getHeight()/scaleY)));
-			pointList.addLast(new Point((int) (this.getWidth() / scaleX), (int) (this.getHeight()/scaleY)));
+			pointList.addFirst(new Point(0, 0));
+			pointList.addLast(new Point((int) (this.getWidth() / scaleX), 0));
 		}
 		repaint();
 	}
@@ -474,7 +528,7 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 			arrayOfBooleans[i] = true;
 		}
 	}
-	
+
 	/**
 	 * Fills the Arrays of each HolonElement
 	 */