Procházet zdrojové kódy

Merge branch 'Ohne_Drag_and_Drop' of https://git.tk.informatik.tu-darmstadt.de/carlos.garcia/praktikum-holons into Ohne_Drag_and_Drop

Jessey Widhalm před 8 roky
rodič
revize
6a56b8b460
1 změnil soubory, kde provedl 38 přidání a 29 odebrání
  1. 38 29
      src/ui/view/UnitGraph.java

+ 38 - 29
src/ui/view/UnitGraph.java

@@ -19,6 +19,8 @@ import java.awt.Point;
 
 import javax.swing.JPanel;
 
+import com.sun.org.apache.xerces.internal.util.SynchronizedSymbolTable;
+
 import classes.HolonElement;
 import ui.controller.Control;
 import ui.model.Model;
@@ -102,9 +104,6 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 
 		if (isElement) {
 			if (arrayOfFloats != null) {
-				// array fillen
-				fillArrayofValue();
-
 				// Draw the Lines
 				g2.setStroke(new BasicStroke(2));
 				g2.setColor(Color.BLACK);
@@ -339,7 +338,7 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 		tempP = null;
 		if (pointList != null) {
 			for (Point p : pointList) {
-				if (x >= p.getX() - dist && x <= p.getX() + dist) {
+				if (x >= p.getX() - dist * 2 && x <= p.getX() + dist * 2) {
 					if (e.getButton() == MouseEvent.BUTTON3) {
 						tempP = p;
 						deletePoint = true;
@@ -353,21 +352,21 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 			if (!pointDrag && e.getButton() != MouseEvent.BUTTON3 && x != 0 && x != width) {
 				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));
+						// double p1, p2 um location der points zu bestimmen
+						double p1 = pointList.get(i - 1).getX();
+						double p2 = pointList.get(i).getX();
+						// Punkte hinzufügen, je nachdem ob true oder false
+						if (pointList.get(i - 1).getY() != 0 && pointList.get(i).getY() != 0) {
+							pointList.add(i, new Point((int) ((x + p2) / 2 + dist), (int) height - 1));
+							pointList.add(i, new Point((int) ((x + p2) / 2 + dist), 0));
+							pointList.add(i, new Point((int) ((x + p1) / 2 - dist), 0));
+							pointList.add(i, new Point((int) ((x + p1) / 2 - 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));
+						} else if (pointList.get(i - 1).getY() == 0 && pointList.get(i).getY() == 0) {
+							pointList.add(i, new Point((int) ((x + p2) / 2 + dist), 0));
+							pointList.add(i, new Point((int) ((x + p2) / 2 + dist), (int) height - 1));
+							pointList.add(i, new Point((int) ((x + p1) / 2 - dist), (int) height - 1));
+							pointList.add(i, new Point((int) ((x + p1) / 2 - dist), 0));
 							added = true;
 						}
 					}
@@ -377,15 +376,20 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 			// Delete a Point
 			if (deletePoint && tempP.getX() != 0
 					&& (tempP.getX() != this.getWidth() / scaleX || tempP != pointList.getLast())) {
-				System.out.println("y: " + tempP.getY());
-				// pointList.remove(tempP);
+				int i = pointList.indexOf(tempP);
+				if (tempP.getY() == 0) {
+					pointList.remove(i);
+					pointList.remove(i - 1);
+					pointList.remove(i - 2);
+					pointList.remove(i - 3);
+				}
+				if (tempP.getY() == height - 1) {
+					pointList.remove(i + 2);
+					pointList.remove(i + 1);
+					pointList.remove(i);
+					pointList.remove(i - 1);
+				}
 			}
-			// Delete This for
-			/*
-			 * for (int i = 0; i < pointList.size(); i++) {
-			 * System.out.println(pointList.get(i).getX() + ", " +
-			 * pointList.get(i).getY()); }
-			 */
 
 			repaint();
 		}
@@ -398,6 +402,10 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 			pointDrag = false;
 			tempP = null;
 		}
+		if (isElement) {
+			// array fillen
+			fillArrayofValue();
+		}
 	}
 
 	public void componentResized(ComponentEvent e) {
@@ -569,14 +577,15 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 	/**
 	 * Fills the Arrays of each HolonElement
 	 */
+	@SuppressWarnings("unchecked")
 	public void fillArrayofValue() {
 		for (HolonElement he : tempElements) {
 			MAXIMUM = he.getEnergy();
-			arrayOfFloats = he.getEnergyAt();
-			he.setGraphPoints(pointList);
+			he.setGraphPoints((LinkedList<Point>) pointList.clone());
 			for (int i = 0; i < arrayOfFloats.length; i++) {
-				arrayOfFloats[i] = convertToValueY(getYValueAt_2((int) (i * width / (model.getIterations() - 1))));
+				he.getEnergyAt()[i] = convertToValueY(getYValueAt_2((int) (i * width / (model.getIterations() - 1))));
 			}
+			arrayOfFloats = he.getEnergyAt();
 		}
 	}