Browse Source

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

dominik.rieder 7 năm trước cách đây
mục cha
commit
21fec0eaec
4 tập tin đã thay đổi với 94 bổ sung39 xóa
  1. 31 11
      src/ui/view/AddObjectPopUp.java
  2. 11 1
      src/ui/view/GUI.java
  3. 41 19
      src/ui/view/MyCanvas.java
  4. 11 8
      src/ui/view/UnitGraph.java

+ 31 - 11
src/ui/view/AddObjectPopUp.java

@@ -57,6 +57,7 @@ public class AddObjectPopUp extends JDialog {
 	private String givenCategory;
 	private JLabel lblImagePreview;
 	private CpsObject toEdit;
+	private boolean imageChanged = false;
 
 	/**
 	 * Launch the application.
@@ -129,6 +130,11 @@ public class AddObjectPopUp extends JDialog {
 			});
 
 		}
+		{
+			lblImagePreview = new JLabel("");
+			lblImagePreview.setBounds(295, 3, 50, 50);
+			contentPanel.add(lblImagePreview);
+		}
 		{
 			sourcePath = new JTextField();
 			sourcePath.addKeyListener(new KeyListener() {
@@ -150,13 +156,27 @@ public class AddObjectPopUp extends JDialog {
 				}
 			});
 			if (edit) {
-				sourcePath.setText(obj.getImage());
-				if (sourcePath.getText().startsWith("/Images/")) {
-					sourcePath
-							.setText(System.getProperty("user.home") + "/praktikum-holons/res/" + sourcePath.getText());
+				File checkPath = new File(obj.getImage());
+				if (checkPath.exists()) {
+					selectedFile = new File(obj.getImage());
+				} else {
+					selectedFile = new File(this.getClass().getResource(obj.getImage()).getFile());
 				}
-				filePath = sourcePath.getText();
-				selectedFile = new File(sourcePath.getText());
+				filePath = selectedFile.getAbsolutePath();
+				sourcePath.setText(filePath);
+				ImageIcon icon = new ImageIcon(
+						new ImageIcon(filePath).getImage().getScaledInstance(50, 50, Image.SCALE_SMOOTH));
+				if(icon != null)
+				lblImagePreview.setIcon(icon);
+			
+				
+//				sourcePath.setText(obj.getImage());
+//				if (sourcePath.getText().startsWith("/Images/")) {
+//					sourcePath
+//							.setText(System.getProperty("user.home") + "/praktikum-holons/res/" + sourcePath.getText());
+//				}
+//				filePath = sourcePath.getText();
+//				selectedFile = new File(sourcePath.getText());
 			}
 			sourcePath.setBounds(148, 77, 271, 20);
 			contentPanel.add(sourcePath);
@@ -212,11 +232,7 @@ public class AddObjectPopUp extends JDialog {
 			btnNewButton.setBounds(270, 182, 142, 27);
 			contentPanel.add(btnNewButton);
 		}
-		{
-			lblImagePreview = new JLabel("");
-			lblImagePreview.setBounds(295, 3, 50, 50);
-			contentPanel.add(lblImagePreview);
-		}
+		
 		{
 			JPanel buttonPane = new JPanel();
 			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
@@ -228,7 +244,10 @@ public class AddObjectPopUp extends JDialog {
 						Component frame = null;
 						if (objectName.getText().length() > 0) {
 							if (sourcePath.getText().equals(filePath)) {
+								imagePath = filePath;
+								if(imageChanged)
 								copieFile();
+								imageChanged = false;
 								// theObject = new
 								// HolonObject(objectName.getText());
 								// theObject.setElements(hElements);
@@ -293,6 +312,7 @@ public class AddObjectPopUp extends JDialog {
 			ImageIcon icon = new ImageIcon(
 					new ImageIcon(filePath).getImage().getScaledInstance(50, 50, Image.SCALE_SMOOTH));
 			lblImagePreview.setIcon(icon);
+			imageChanged = true;
 		} else {
 			System.out.println("Failed to Load");
 		}

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

@@ -896,6 +896,17 @@ public class GUI<E> implements CategoryListener {
 					try {
 						controller.loadFile(file.getAbsolutePath());
 						canvas.repaint();
+						ArrayList<HolonElement> tempList = new ArrayList<>();
+						for (CpsObject cps : model.getObjectsOnCanvas()) {
+							if (cps instanceof HolonObject) {
+								for (HolonElement h : ((HolonObject)cps).getElements()) {
+									tempList.add(h);
+									unitGraph.repaintWithNewElement(tempList);
+									tempList.remove(0);
+								}
+							}
+						}
+						unitGraph.empty();
 						tree.repaint();
 					} catch (IOException e) {
 						// TODO Auto-generated catch block
@@ -941,7 +952,6 @@ public class GUI<E> implements CategoryListener {
 			public void stateChanged(ChangeEvent e) {
 				int i = model.getCurIteration();
 				controller.calculateStateForTimeStep(i);
-				canvas.repaint();
 				unitGraph.repaint();
 			}
 		});

+ 41 - 19
src/ui/view/MyCanvas.java

@@ -47,13 +47,14 @@ public class MyCanvas extends JPanel implements MouseListener, MouseMotionListen
 	private Model model;
 	private final Control controller;
 	Graphics2D g2; // For Painting
-	private int cx;
-	private int cy;
+	private int cx, cy, sx, sy;
+
 	ArrayList<HolonElement> dataSelected = new ArrayList<HolonElement>();
 
 	private boolean dragging = false; // for dragging
 	private boolean drawEdge = false; // for drawing edges
 	private boolean click = false; // for double click
+	private boolean doMark = false; // for double click
 	public CpsObject tempCps = null;
 	private Rectangle selectRect = new Rectangle();
 	private CpsEdge edgeHighlight = null;
@@ -166,15 +167,14 @@ public class MyCanvas extends JPanel implements MouseListener, MouseMotionListen
 			if (cps.getID() == model.getSelectedObjectID() && controller.searchByID(model.getSelectedObjectID()) != null
 					&& controller.searchByID(model.getSelectedObjectID()) instanceof CpsNode) {
 				img = new ImageIcon(this.getClass().getResource("/Images/node_selected.png")).getImage();
-			}
-			else {
+			} else {
 				if (cps instanceof HolonSwitch) {
 					if (((HolonSwitch) cps).getActiveAt()[model.getCurIteration()]) {
 						((HolonSwitch) cps).setState(true);
 					} else {
 						((HolonSwitch) cps).setState(false);
 					}
-				} 
+				}
 				if (cps == tempCps) {
 					g2.setColor(Color.BLUE);
 					g2.fillRect((int) selectRect.getX(), (int) selectRect.getY(), (int) selectRect.getWidth(),
@@ -183,10 +183,10 @@ public class MyCanvas extends JPanel implements MouseListener, MouseMotionListen
 					if (((HolonObject) cps).getSupplied()) {
 						g2.setColor(Color.GREEN);
 					} else {
-						g2.setColor(Color.RED);
+						g2.setColor(Color.YELLOW);
 					}
 					g2.fillRect(cps.getPosition().x - (controller.getScale() / 20),
-							cps.getPosition().y - (controller.getScale() / 20), 
+							cps.getPosition().y - (controller.getScale() / 20),
 							controller.getScale() + ((controller.getScale() / 20) * 2),
 							controller.getScale() + ((controller.getScale() / 20) * 2));
 				}
@@ -199,8 +199,23 @@ public class MyCanvas extends JPanel implements MouseListener, MouseMotionListen
 			}
 			g2.drawImage(img, cps.getPosition().x, cps.getPosition().y, controller.getScale(), controller.getScale(),
 					null);
+		}
 
+		// Dragg Highlighting
+		if (doMark) {
+			g2.setColor(Color.BLACK);
+			g2.setStroke(new BasicStroke(1));
+			if (sx > x && sy > y) {
+				g2.drawRect(x, y, sx - x, sy - y);
+			} else if (sx < x && sy < y) {
+				g2.drawRect(sx, sy, x - sx, y - sy);
+			} else if (sx >= x) {
+				g2.drawRect(x, sy, sx - x, y - sy);
+			} else if (sy >= y) {
+				g2.drawRect(sx, y, x - sx, sy - y);
+			}
 		}
+
 	}
 
 	@Override
@@ -227,15 +242,16 @@ public class MyCanvas extends JPanel implements MouseListener, MouseMotionListen
 			cy = cps.getPosition().y;
 			if (x - controller.getScale() <= cx && y - controller.getScale() <= cy && x >= cx && y >= cy) {
 				tempCps = cps;
+				dragging = true;
 				// If drawing an Edge (CTRL down)
-				if (e.isControlDown()) {
-					drawEdge = true;
-					drawEdge = true;
-				}
 				if (tempCps.getClass() == HolonObject.class) {
 					HolonObject tempObj = ((HolonObject) tempCps);
 					dataSelected = tempObj.getElements();
 				}
+				if (e.isControlDown()) {
+					drawEdge = true;
+					dragging = false;
+				}
 			}
 		}
 		// Edge Selection
@@ -243,6 +259,12 @@ public class MyCanvas extends JPanel implements MouseListener, MouseMotionListen
 			edgeHighlight = mousePositionOnEdge(x, y);
 			controller.setSelecteEdge(edgeHighlight);
 		}
+
+		if (edgeHighlight == null && tempCps == null) {
+			sx = e.getX();
+			sy = e.getY();
+			doMark = true;
+		}
 		// Object Selection Highlighting (selectRect)
 		objectSelectionHighlighting();
 
@@ -274,17 +296,16 @@ public class MyCanvas extends JPanel implements MouseListener, MouseMotionListen
 			popmenu.show(e.getComponent(), e.getX(), e.getY());
 		}
 
+		doMark = false;
 		repaint();
 	}
 
 	@Override
 	public void mouseDragged(MouseEvent e) {
 		// If Edge is drawn
-		if (drawEdge) {
-			x = e.getX();
-			y = e.getY();
-			repaint();
-		} else {
+		x = e.getX();
+		y = e.getY();
+		if (dragging) {
 			try {
 				// Au�erhalb des Randes gedragged?
 				x = e.getX() - controller.getScaleDiv2();
@@ -309,6 +330,7 @@ public class MyCanvas extends JPanel implements MouseListener, MouseMotionListen
 
 			}
 		}
+		repaint();
 	}
 
 	@Override
@@ -416,11 +438,11 @@ public class MyCanvas extends JPanel implements MouseListener, MouseMotionListen
 				k = p.getB();
 
 				e = new CpsEdge(n, tempCps);
-				
+
 				e1 = new CpsEdge(n, r);
-				
+
 				e2 = new CpsEdge(n, k);
-				
+
 				p.getA().getConnections().remove(p);
 				p.getB().getConnections().remove(p);
 

+ 11 - 8
src/ui/view/UnitGraph.java

@@ -103,6 +103,8 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 		}
 
 		if (isElement) {
+			// array fillen
+			fillArrayofValue();
 			if (arrayOfFloats != null) {
 				// Draw the Lines
 				g2.setStroke(new BasicStroke(2));
@@ -173,6 +175,9 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 		g2.setStroke(new BasicStroke(1));
 		g2.drawLine((model.getCurIteration()) * this.getWidth() / (model.getIterations() - 1), 0,
 				(model.getCurIteration()) * this.getWidth() / (model.getIterations() - 1), this.getHeight());
+		
+		//algorithmus
+		controller.calculateStateForTimeStep(model.getCurIteration());
 	}
 
 	@Override
@@ -402,10 +407,6 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 			pointDrag = false;
 			tempP = null;
 		}
-		if (isElement) {
-			// array fillen
-			fillArrayofValue();
-		}
 	}
 
 	public void componentResized(ComponentEvent e) {
@@ -583,12 +584,14 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 			MAXIMUM = he.getEnergy();
 			he.setGraphPoints((LinkedList<Point>) pointList.clone());
 			for (int i = 0; i < arrayOfFloats.length; i++) {
-				if (he.getEnergy() >=0 ) {
-					he.getEnergyAt()[i] = convertToValueY(getYValueAt_2((int) (i * width / (model.getIterations() - 1))));	
+				if (he.getEnergy() >= 0) {
+					he.getEnergyAt()[i] = convertToValueY(
+							getYValueAt_2((int) (i * width / (model.getIterations() - 1))));
 				} else {
-					he.getEnergyAt()[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();
 		}