Browse Source

switch properties anzeigen und mehr unitgraph zeugs für switch

Kevin Trometer 8 years ago
parent
commit
9a7787d8e7
4 changed files with 87 additions and 54 deletions
  1. 16 9
      src/classes/HolonSwitch.java
  2. 14 8
      src/ui/view/GUI.java
  3. 1 6
      src/ui/view/MyCanvas.java
  4. 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++) {

+ 14 - 8
src/ui/view/GUI.java

@@ -168,7 +168,7 @@ public class GUI<E> implements CategoryListener {
 	private int yValueElements = 0;
 
 	private MyCanvas canvas;
-	private UnitGraph unitGraph; // for testing, remove later
+	private UnitGraph unitGraph;
 	private final JSplitPane splitPane_3 = new JSplitPane();
 	private final JSlider sizeSlider = new JSlider();
 	private final JLabel lblImageSize = new JLabel("Image Size");
@@ -233,18 +233,18 @@ public class GUI<E> implements CategoryListener {
 			@Override
 			public void actionPerformed(ActionEvent e) {
 				JPanel myPanel = new JPanel();
-				JTextField field1 = new JTextField(""+canvas.getWidth()+"");
-				JTextField field2 = new JTextField(""+canvas.getHeight()+"");
+				JTextField field1 = new JTextField("" + canvas.getWidth() + "");
+				JTextField field2 = new JTextField("" + canvas.getHeight() + "");
 				myPanel.add(field1);
 				myPanel.add(field2);
 				JOptionPane.showMessageDialog(null, myPanel);
-		        canvas.setPreferredSize(new Dimension(Integer.parseInt(field1.getText()), Integer.parseInt(field2.getText())));
-		        canvas.repaint();
+				canvas.setPreferredSize(
+						new Dimension(Integer.parseInt(field1.getText()), Integer.parseInt(field2.getText())));
+				canvas.repaint();
 			}
 		});
-		
+
 		mnNewMenu_3.add(splitPane_3);
-		
 
 		sizeSlider.setMinimum(15);
 		sizeSlider.setMaximum(115);
@@ -738,7 +738,13 @@ public class GUI<E> implements CategoryListener {
 						deleteRows();
 						Object[] tempRatioPerc = { "Ratio Type", true };
 						tableModelProperties.addRow(tempRatioPerc);
-					}else{
+					} else if (temp instanceof HolonSwitch) {
+						deleteRows();
+						Object[] tempActive = { "Active", ((HolonSwitch) temp).getState() };
+						tableModelProperties.addRow(tempActive);
+						unitGraph.repaintWithNewSwitch((HolonSwitch) temp);
+						elementGraph.setText(temp.getName() + " ");
+					} else {
 						deleteRows();
 					}
 					tableModelProperties.setCellEditable(0, 1, true);

+ 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))));
 		}
 	}