Browse Source

rewritten MyCanvas, so that the objects zoom from their center location

Kevin Trometer 7 years ago
parent
commit
f2c2c93d5c
2 changed files with 51 additions and 63 deletions
  1. 0 1
      src/ui/view/GUI.java
  2. 51 62
      src/ui/view/MyCanvas.java

+ 0 - 1
src/ui/view/GUI.java

@@ -675,7 +675,6 @@ public class GUI<E> implements CategoryListener {
 			@Override
 			public void stateChanged(ChangeEvent e) {
 				controller.setScale(sizeSlider.getValue());
-				tree.setRowHeight(50);
 				canvas.repaint();
 			}
 		});

+ 51 - 62
src/ui/view/MyCanvas.java

@@ -434,8 +434,7 @@ public class MyCanvas extends JPanel implements MouseListener, MouseMotionListen
 		if (drawEdge) {
 			g2.setColor(Color.BLACK);
 			g2.setStroke(new BasicStroke(2));
-			g2.drawLine(tempCps.getPosition().x + controller.getScaleDiv2(),
-					tempCps.getPosition().y + controller.getScaleDiv2(), x, y);
+			g2.drawLine(tempCps.getPosition().x, tempCps.getPosition().y, x, y);
 		}
 
 		for (CpsEdge con : model.getEdgesOnCanvas()) {
@@ -445,7 +444,7 @@ public class MyCanvas extends JPanel implements MouseListener, MouseMotionListen
 					if (con.getState()) {
 						g2.setColor(Color.GREEN);
 						if (con.getCapacity() != -1) {
-							g2.setStroke(new BasicStroke(Math.min(((con.getFlow() / con.getCapacity() * 3)+1), 4)));
+							g2.setStroke(new BasicStroke(Math.min(((con.getFlow() / con.getCapacity() * 3) + 1), 4)));
 						}
 					} else {
 						g2.setColor(Color.RED);
@@ -455,10 +454,8 @@ public class MyCanvas extends JPanel implements MouseListener, MouseMotionListen
 					g2.setColor(Color.DARK_GRAY);
 					g2.setStroke(new BasicStroke(2));
 				}
-				g2.drawLine(con.getA().getPosition().x + controller.getScaleDiv2(),
-						con.getA().getPosition().y + controller.getScaleDiv2(),
-						con.getB().getPosition().x + controller.getScaleDiv2(),
-						con.getB().getPosition().y + controller.getScaleDiv2());
+				g2.drawLine(con.getA().getPosition().x, con.getA().getPosition().y, con.getB().getPosition().x,
+						con.getB().getPosition().y);
 
 				if (con.getCapacity() == -1) {
 					maxCap = Character.toString('\u221e');
@@ -470,16 +467,11 @@ public class MyCanvas extends JPanel implements MouseListener, MouseMotionListen
 				if (showedInformation[0]) {
 					if (con.getConnected() == 0 || con.getConnected() == 1) {
 						g2.drawString(con.getFlow() + "/" + maxCap,
-								(con.getA().getPosition().x + con.getB().getPosition().x) / 2
-										+ controller.getScaleDiv2(),
-								(con.getA().getPosition().y + con.getB().getPosition().y) / 2
-										+ controller.getScaleDiv2());
+								(con.getA().getPosition().x + con.getB().getPosition().x) / 2,
+								(con.getA().getPosition().y + con.getB().getPosition().y) / 2);
 					} else {
-						g2.drawString("not connected",
-								(con.getA().getPosition().x + con.getB().getPosition().x) / 2
-										+ controller.getScaleDiv2(),
-								(con.getA().getPosition().y + con.getB().getPosition().y) / 2
-										+ controller.getScaleDiv2());
+						g2.drawString("not connected", (con.getA().getPosition().x + con.getB().getPosition().x) / 2,
+								(con.getA().getPosition().y + con.getB().getPosition().y) / 2);
 					}
 				}
 			}
@@ -490,7 +482,7 @@ public class MyCanvas extends JPanel implements MouseListener, MouseMotionListen
 			g2.setColor(Color.BLUE);
 			for (CpsEdge con : model.getEdgesOnCanvas()) {
 				if (con.getFlow() <= con.getCapacity()) {
-					g2.setStroke(new BasicStroke(Math.min(((con.getFlow() / con.getCapacity() * 3)+1), 4)));
+					g2.setStroke(new BasicStroke(Math.min(((con.getFlow() / con.getCapacity() * 3) + 1), 4)));
 				} else {
 					g2.setStroke(new BasicStroke(2));
 				}
@@ -499,10 +491,8 @@ public class MyCanvas extends JPanel implements MouseListener, MouseMotionListen
 						|| con.getB().getId() == model.getSelectedObjectID()
 						|| model.getSelectedCpsObjects().contains(con.getB())
 						|| tempSelected.contains(con.getB()) && con != edgeHighlight) {
-					g2.drawLine(con.getA().getPosition().x + controller.getScaleDiv2(),
-							con.getA().getPosition().y + controller.getScaleDiv2(),
-							con.getB().getPosition().x + controller.getScaleDiv2(),
-							con.getB().getPosition().y + controller.getScaleDiv2());
+					g2.drawLine(con.getA().getPosition().x, con.getA().getPosition().y, con.getB().getPosition().x,
+							con.getB().getPosition().y);
 
 					if (con.getCapacity() == -1) {
 						maxCap = Character.toString('\u221e');
@@ -514,16 +504,12 @@ public class MyCanvas extends JPanel implements MouseListener, MouseMotionListen
 					if (showedInformation[0]) {
 						if (con.getConnected() == 0 || con.getConnected() == 1) {
 							g2.drawString(con.getFlow() + "/" + maxCap,
-									(con.getA().getPosition().x + con.getB().getPosition().x) / 2
-											+ controller.getScaleDiv2(),
-									(con.getA().getPosition().y + con.getB().getPosition().y) / 2
-											+ controller.getScaleDiv2());
+									(con.getA().getPosition().x + con.getB().getPosition().x) / 2,
+									(con.getA().getPosition().y + con.getB().getPosition().y) / 2);
 						} else {
 							g2.drawString("not connected",
-									(con.getA().getPosition().x + con.getB().getPosition().x) / 2
-											+ controller.getScaleDiv2(),
-									(con.getA().getPosition().y + con.getB().getPosition().y) / 2
-											+ controller.getScaleDiv2());
+									(con.getA().getPosition().x + con.getB().getPosition().x) / 2,
+									(con.getA().getPosition().y + con.getB().getPosition().y) / 2);
 						}
 					}
 				}
@@ -531,14 +517,13 @@ public class MyCanvas extends JPanel implements MouseListener, MouseMotionListen
 		} else if (edgeHighlight != null) {
 			g2.setColor(Color.BLUE);
 			if (edgeHighlight.getFlow() <= edgeHighlight.getCapacity()) {
-				g2.setStroke(new BasicStroke(Math.min(((edgeHighlight.getFlow() / edgeHighlight.getCapacity() * 3)+1), 4)));
+				g2.setStroke(new BasicStroke(
+						Math.min(((edgeHighlight.getFlow() / edgeHighlight.getCapacity() * 3) + 1), 4)));
 			} else {
 				g2.setStroke(new BasicStroke(2));
 			}
-			g2.drawLine(edgeHighlight.getA().getPosition().x + controller.getScaleDiv2(),
-					edgeHighlight.getA().getPosition().y + controller.getScaleDiv2(),
-					edgeHighlight.getB().getPosition().x + controller.getScaleDiv2(),
-					edgeHighlight.getB().getPosition().y + controller.getScaleDiv2());
+			g2.drawLine(edgeHighlight.getA().getPosition().x, edgeHighlight.getA().getPosition().y,
+					edgeHighlight.getB().getPosition().x, edgeHighlight.getB().getPosition().y);
 
 			if (edgeHighlight.getCapacity() == -1) {
 				maxCap = Character.toString('\u221e');
@@ -549,10 +534,8 @@ public class MyCanvas extends JPanel implements MouseListener, MouseMotionListen
 			}
 			if (showedInformation[0]) {
 				g2.drawString(edgeHighlight.getFlow() + "/" + maxCap,
-						(edgeHighlight.getA().getPosition().x + edgeHighlight.getB().getPosition().x) / 2
-								+ controller.getScaleDiv2(),
-						(edgeHighlight.getA().getPosition().y + edgeHighlight.getB().getPosition().y) / 2
-								+ controller.getScaleDiv2());
+						(edgeHighlight.getA().getPosition().x + edgeHighlight.getB().getPosition().x) / 2,
+						(edgeHighlight.getA().getPosition().y + edgeHighlight.getB().getPosition().y) / 2);
 			}
 		}
 
@@ -562,8 +545,8 @@ public class MyCanvas extends JPanel implements MouseListener, MouseMotionListen
 			if (showedInformation[3]) {
 				g2.setColor(cps.getBorderColor());
 				if (g2.getColor() != Color.WHITE) {
-					g2.fillRect((int) (cps.getPosition().x - scalediv20 - 3),
-							(int) (cps.getPosition().y - scalediv20 - 3),
+					g2.fillRect((int) (cps.getPosition().x - controller.getScaleDiv2() - scalediv20 - 3),
+							(int) (cps.getPosition().y - controller.getScaleDiv2() - scalediv20 - 3),
 							(int) (controller.getScale() + ((scalediv20 + 3) * 2)),
 							(int) (controller.getScale() + ((scalediv20 + 3) * 2)));
 				}
@@ -584,25 +567,29 @@ public class MyCanvas extends JPanel implements MouseListener, MouseMotionListen
 				if ((cps == tempCps && model.getSelectedCpsObjects().size() == 0 && tempSelected.size() == 0)
 						|| model.getSelectedCpsObjects().contains(cps) || tempSelected.contains(cps)) {
 					g2.setColor(Color.BLUE);
-					g2.fillRect((int) (cps.getPosition().x - scalediv20), (int) (cps.getPosition().y - scalediv20),
+					g2.fillRect((int) (cps.getPosition().x - controller.getScaleDiv2() - scalediv20),
+							(int) (cps.getPosition().y - controller.getScaleDiv2() - scalediv20),
 							(int) (controller.getScale() + (scalediv20 * 2)),
 							(int) (controller.getScale() + (scalediv20 * 2)));
 					if (showedInformation[1] && cps instanceof HolonObject) {
 						g2.setColor(Color.BLACK);
 						float totalEnergy = ((HolonObject) cps).getCurrentEnergyAtTimeStep(model.getCurIteration());
-						g2.drawString(Float.toString(totalEnergy), cps.getPosition().x, cps.getPosition().y - 10);
+						g2.drawString(Float.toString(totalEnergy), cps.getPosition().x - controller.getScaleDiv2(),
+								cps.getPosition().y - controller.getScaleDiv2() - 10);
 					}
 				} else if (cps instanceof HolonObject) {
 					g2.setColor(((HolonObject) cps).getColor());
 
-					g2.fillRect((int) (cps.getPosition().x - scalediv20), (int) (cps.getPosition().y - scalediv20),
+					g2.fillRect((int) (cps.getPosition().x - controller.getScaleDiv2() - scalediv20),
+							(int) (cps.getPosition().y - controller.getScaleDiv2() - scalediv20),
 							(int) (controller.getScale() + (scalediv20 * 2)),
 							(int) (controller.getScale() + (scalediv20 * 2)));
 
 					if (showedInformation[1]) {
 						g2.setColor(Color.BLACK);
 						float totalEnergy = ((HolonObject) cps).getCurrentEnergyAtTimeStep(model.getCurIteration());
-						g2.drawString(Float.toString(totalEnergy), cps.getPosition().x, cps.getPosition().y - 10);
+						g2.drawString(Float.toString(totalEnergy), cps.getPosition().x - controller.getScaleDiv2(),
+								cps.getPosition().y - controller.getScaleDiv2() - 10);
 					}
 				}
 				// draw image
@@ -613,7 +600,8 @@ public class MyCanvas extends JPanel implements MouseListener, MouseMotionListen
 					img = new ImageIcon(this.getClass().getResource(cps.getImage())).getImage();
 				}
 			}
-			g2.drawImage(img, cps.getPosition().x, cps.getPosition().y, controller.getScale(), controller.getScale(),
+			g2.drawImage(img, cps.getPosition().x - controller.getScaleDiv2(),
+					cps.getPosition().y - controller.getScaleDiv2(), controller.getScale(), controller.getScale(),
 					null);
 
 		}
@@ -647,7 +635,7 @@ public class MyCanvas extends JPanel implements MouseListener, MouseMotionListen
 			g2.drawString(toolTipText, fixXPos + 2, fixYPos + 12);
 		}
 
-		// Dragg Highlighting
+		// Dragged marker Highlighting
 		if (doMark) {
 			g2.setColor(Color.BLACK);
 			g2.setStroke(new BasicStroke(1));
@@ -693,8 +681,8 @@ public class MyCanvas extends JPanel implements MouseListener, MouseMotionListen
 		controller.setSelecteEdge(null);
 		// Object Selection
 		for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
-			cx = cps.getPosition().x;
-			cy = cps.getPosition().y;
+			cx = cps.getPosition().x - controller.getScaleDiv2();
+			cy = cps.getPosition().y - controller.getScaleDiv2();
 			if (x - controller.getScale() <= cx && y - controller.getScale() <= cy && x >= cx && y >= cy) {
 				tempCps = cps;
 				controller.addTextToConsole("Selected: ", Color.BLACK, 12, false, false, false);
@@ -842,8 +830,8 @@ public class MyCanvas extends JPanel implements MouseListener, MouseMotionListen
 				dragged = true;
 				float xDist, yDist; // Distance
 
-				x = e.getX() - controller.getScaleDiv2();
-				y = e.getY() - controller.getScaleDiv2();
+				x = e.getX();
+				y = e.getY();
 
 				// Make sure its in bounds
 				if (e.getX() < controller.getScaleDiv2())
@@ -863,8 +851,8 @@ public class MyCanvas extends JPanel implements MouseListener, MouseMotionListen
 				// TipText Position and name
 				toolTip = true;
 				toolTipText = tempCps.getName() + ", " + tempCps.getId();
-				toolTipPos.x = tempCps.getPosition().x;
-				toolTipPos.y = tempCps.getPosition().y + model.getScale();
+				toolTipPos.x = tempCps.getPosition().x - controller.getScaleDiv2();
+				toolTipPos.y = tempCps.getPosition().y + controller.getScaleDiv2();
 
 				// All Selected Objects
 				for (AbstractCpsObject cps : model.getSelectedCpsObjects()) {
@@ -906,8 +894,7 @@ public class MyCanvas extends JPanel implements MouseListener, MouseMotionListen
 					y2 = sy;
 				}
 				if (x1 <= cps.getPosition().x + model.getScaleDiv2() && y1 <= cps.getPosition().y + model.getScaleDiv2()
-						&& x2 >= cps.getPosition().x + model.getScaleDiv2()
-						&& y2 >= cps.getPosition().y + model.getScaleDiv2()) {
+						&& x2 >= cps.getPosition().x && y2 >= cps.getPosition().y) {
 					tempSelected.add(cps);
 
 				}
@@ -925,12 +912,12 @@ public class MyCanvas extends JPanel implements MouseListener, MouseMotionListen
 		// Everytghing for the tooltip :)
 		boolean on = false;
 		for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
-			cx = cps.getPosition().x;
-			cy = cps.getPosition().y;
+			cx = cps.getPosition().x - controller.getScaleDiv2();
+			cy = cps.getPosition().y - controller.getScaleDiv2();
 			if (x - controller.getScale() <= cx && y - controller.getScale() <= cy && x >= cx && y >= cy) {
 				on = true;
-				toolTipPos.x = cps.getPosition().x;
-				toolTipPos.y = cps.getPosition().y + model.getScale();
+				toolTipPos.x = cps.getPosition().x - controller.getScaleDiv2();
+				toolTipPos.y = cps.getPosition().y + controller.getScaleDiv2();
 				toolTipText = cps.getName() + ", " + cps.getId();
 			}
 		}
@@ -954,8 +941,8 @@ public class MyCanvas extends JPanel implements MouseListener, MouseMotionListen
 		AbstractCpsObject tempCPS = null;
 
 		for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
-			cx = cps.getPosition().x;
-			cy = cps.getPosition().y;
+			cx = cps.getPosition().x - controller.getScaleDiv2();
+			cy = cps.getPosition().y - controller.getScaleDiv2();
 			if (x - controller.getScale() <= cx && y - controller.getScale() <= cy && x >= cx && y >= cy
 					&& cps != tempCps) {
 				node = false;
@@ -995,7 +982,7 @@ public class MyCanvas extends JPanel implements MouseListener, MouseMotionListen
 
 				CpsNode n = new CpsNode("Node");
 
-				n.setPosition(x - model.getScaleDiv2(), y - model.getScaleDiv2());
+				n.setPosition(x, y);
 				controller.addObjectCanvas(n);
 
 				AbstractCpsObject r, k;
@@ -1019,7 +1006,7 @@ public class MyCanvas extends JPanel implements MouseListener, MouseMotionListen
 		if (node) {
 			CpsNode n = new CpsNode("Node");
 
-			n.setPosition(x - model.getScaleDiv2(), y - model.getScaleDiv2());
+			n.setPosition(x, y);
 			controller.addObjectCanvas(n);
 
 			e = new CpsEdge(n, tempCps, model.getMaxCapacity());
@@ -1045,6 +1032,8 @@ public class MyCanvas extends JPanel implements MouseListener, MouseMotionListen
 	 * @return CpsEdge the Mouse is on, null if the mouse is not on an Edge
 	 */
 	public CpsEdge mousePositionOnEdge(int x, int y) {
+		x += controller.getScaleDiv2();
+		y += controller.getScaleDiv2();
 		int lx, ly, hx, hy;
 		for (CpsEdge p : model.getEdgesOnCanvas()) {
 			Line2D l = new Line2D.Float(p.getA().getPosition().x, p.getA().getPosition().y, p.getB().getPosition().x,