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

# Conflicts:
#	src/ui/view/GUI.java
Teh-Hai Julian Zheng 8 years ago
parent
commit
28d628c7eb
3 changed files with 73 additions and 46 deletions
  1. 16 9
      src/classes/HolonSwitch.java
  2. 1 6
      src/ui/view/MyCanvas.java
  3. 56 31
      src/ui/view/UnitGraph.java

+ 16 - 9
src/classes/HolonSwitch.java

@@ -15,9 +15,9 @@ public class HolonSwitch extends CpsObject {
 	 * beginning, it starts with all values at energy
 	 */
 	boolean[] activeAt = new boolean[100];
-	//Points on the UnitGraph
+	// Points on the UnitGraph
 	LinkedList<Point> graphPoints = new LinkedList<>();
-	
+
 	public HolonSwitch(String ObjName) {
 		super(ObjName);
 		setState(false);
@@ -32,21 +32,26 @@ public class HolonSwitch extends CpsObject {
 
 	public HolonSwitch(CpsObject obj) {
 		super(obj);
-		setState(((HolonSwitch)obj).getState());
+		setState(((HolonSwitch) obj).getState());
 	}
 
 	public void switchState() {
+		if (this.active == true) {
+			setImage("/Images/switch-off.png");
+		} else {
+			setImage("/Images/switch-on.png");
+		}
 		this.active = !active;
 	}
 
 	public boolean getState() {
 		return this.active;
 	}
-	
-	public void setState(boolean state){
+
+	public void setState(boolean state) {
 		this.active = state;
 	}
-	
+
 	/**
 	 * @return the Graph Points
 	 */
@@ -55,11 +60,13 @@ public class HolonSwitch extends CpsObject {
 	}
 
 	/**
-	 * @param points, the Graph points
+	 * @param points,
+	 *            the Graph points
 	 */
 	public void setGraphPoints(LinkedList<Point> points) {
 		this.graphPoints = points;
 	}
+
 	/**
 	 * get the activeAt Array
 	 */
@@ -67,9 +74,9 @@ public class HolonSwitch extends CpsObject {
 		return activeAt;
 	}
 
-	
 	/**
-	 * @param active, the default value
+	 * @param active,
+	 *            the default value
 	 */
 	public void setActiveAt(boolean active) {
 		for (int i = 0; i < activeAt.length; i++) {

+ 1 - 6
src/ui/view/MyCanvas.java

@@ -176,12 +176,7 @@ class MyCanvas extends JPanel implements MouseListener, MouseMotionListener {
 	public void mouseClicked(MouseEvent e) {
 		// If double clicked on a Switch change the Image to on/off
 		if (doubleClick() && tempCps != null && tempCps.getClass() == HolonSwitch.class) {
-			System.out.println("trans double click");
-			if (tempCps.getImage().compareTo("/Images/switch-on.png") == 0) {
-				tempCps.setImage("/Images/switch-off.png");
-			} else {
-				tempCps.setImage("/Images/switch-on.png");
-			}
+				((HolonSwitch)tempCps).switchState();
 		}
 		repaint();
 	}

+ 56 - 31
src/ui/view/UnitGraph.java

@@ -21,6 +21,7 @@ import javax.swing.JPanel;
 import classes.HolonElement;
 import ui.controller.Control;
 import ui.model.Model;
+import classes.HolonSwitch;
 
 import java.awt.Cursor;
 
@@ -38,21 +39,23 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 	private double scaleX;
 	private double scaleY;
 
-	private float[] arrayOfValue = null;
+	private float[] arrayOfFloats = null;
+	private boolean[] arrayOfBooleans = null;
 
 	private double width = -1;
 	private double height = -1;
 
-	private boolean isElement = true;
-	private boolean isSwitch = true;
+	private boolean isElement = false;
+	private boolean isSwitch = false;
 
 	private HolonElement tempElement;
+	private HolonSwitch tempSwitch;
 	private Model model;
 	private Control controller;
 	GeneralPath graphCurve = new GeneralPath();
 
 	private boolean pointDrag = false;
-	private boolean init = false;
+	private boolean init = true;
 	private Point tempP = null;
 	private double x = 0, y = 0;
 	private int x1, x2, y1, y2, ctrlx1, ctrly1, ctrlx2, ctrly2;
@@ -85,19 +88,18 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 
 		graphCurve.reset();
 
-		if (isElement) {
-
-			// Draw the Vertical Lines
-			g2.setColor(Color.BLACK);
-			for (int i = 0; i <= this.getWidth(); i += 10) {
-				g2.drawLine(i, 0, i, this.getHeight());
-			}
+		// Draw the Vertical Lines
+		g2.setColor(Color.BLACK);
+		for (int i = 0; i <= this.getWidth(); i += 10) {
+			g2.drawLine(i, 0, i, this.getHeight());
+		}
 
-			for (int i = 0; i <= this.getHeight(); i += 5) {
-				g2.drawLine(0, i, this.getWidth(), i);
-			}
+		for (int i = 0; i <= this.getHeight(); i += 5) {
+			g2.drawLine(0, i, this.getWidth(), i);
+		}
 
-			if (arrayOfValue != null) {
+		if (isElement) {
+			if (arrayOfFloats != null) {
 				// array fillen
 				fillArrayofValue();
 
@@ -124,8 +126,8 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 			g2.drawLine((model.getCurIteration()) * this.getWidth() / (model.getIterations() - 1), 0,
 					(model.getCurIteration()) * this.getWidth() / (model.getIterations() - 1), this.getHeight());
 			// Iteration Value
-			if (arrayOfValue != null) {
-				g2.drawString("" + arrayOfValue[model.getCurIteration()],
+			if (arrayOfFloats != null) {
+				g2.drawString("" + arrayOfFloats[model.getCurIteration()],
 						(model.getCurIteration()) * this.getWidth() / (model.getIterations() - 1) + 2,
 						this.getHeight() - 10);
 			}
@@ -273,7 +275,7 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 	 * @param e
 	 */
 	public void switchPressed(MouseEvent e) {
-		//TODO Siwtch pressed zeugs hier hin 
+		// TODO Siwtch pressed zeugs hier hin
 	}
 
 	@Override
@@ -287,7 +289,6 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 	public void componentResized(ComponentEvent e) {
 		// Wenn ein anderes Element genommen wird
 		if (init) {
-			MAXIMUM = tempElement.getEnergy();
 			init = false;
 			// for scale on the first initialisation
 			if (width == -1 && height == -1) {
@@ -297,12 +298,6 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 
 			scaleX = this.getWidth() / width;
 			scaleY = this.getHeight() / height;
-
-			// First time clicked on the Element
-			if (pointList.isEmpty()) {
-				pointList.addFirst(new Point(0, 0));
-				pointList.addLast(new Point((int) (this.getWidth() / scaleX), 0));
-			}
 		}
 
 		// Scale
@@ -330,7 +325,11 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 	public void empty() {
 		pointList = null;
 		tempElement = null;
-		arrayOfValue = null;
+		tempSwitch = null;
+		arrayOfFloats = null;
+		arrayOfBooleans = null;
+		isSwitch = false;
+		isElement = false;
 		repaint();
 	}
 
@@ -373,11 +372,37 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 	 *            ele, which should be visualized
 	 */
 	public void repaintWithNewElement(HolonElement ele) {
-		arrayOfValue = ele.getEnergyAt();
+		arrayOfFloats = ele.getEnergyAt();
 		tempElement = ele;
 		pointList = ele.getGraphPoints();
-		init = true;
-		componentResized(null);
+		isSwitch = false;
+		isElement = true;
+		MAXIMUM = tempElement.getEnergy();
+		// First time clicked on the Element
+		if (pointList.isEmpty()) {
+			pointList.addFirst(new Point(0, 0));
+			pointList.addLast(new Point((int) (this.getWidth() / scaleX), 0));
+		}
+		repaint();
+	}
+	
+	/**
+	 * Visualize the HolonElement on the Graph
+	 * 
+	 * @param HolonElement
+	 *            ele, which should be visualized
+	 */
+	public void repaintWithNewSwitch(HolonSwitch s) {
+		arrayOfBooleans = s.getActiveAt();
+		tempSwitch = s;
+		pointList = s.getGraphPoints();
+		isSwitch = true;
+		isElement = false;
+		// First time clicked on the Element
+		if (pointList.isEmpty()) {
+			pointList.addFirst(new Point(0, 0));
+			pointList.addLast(new Point((int) (this.getWidth() / scaleX), 0));
+		}
 		repaint();
 	}
 
@@ -416,8 +441,8 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 	 * Fills the Arrays of each HolonElement
 	 */
 	public void fillArrayofValue() {
-		for (int i = 0; i < arrayOfValue.length; i++) {
-			arrayOfValue[i] = convertToValueY(getYValueAt_2((int) (i * width / (model.getIterations() - 1))));
+		for (int i = 0; i < arrayOfFloats.length; i++) {
+			arrayOfFloats[i] = convertToValueY(getYValueAt_2((int) (i * width / (model.getIterations() - 1))));
 		}
 	}