Bläddra i källkod

Switch state veränderung

Kevin Trometer 7 år sedan
förälder
incheckning
e52d0aea1c
3 ändrade filer med 21 tillägg och 10 borttagningar
  1. 11 3
      src/classes/HolonSwitch.java
  2. 1 1
      src/ui/controller/CategoryController.java
  3. 9 6
      src/ui/view/MyCanvas.java

+ 11 - 3
src/classes/HolonSwitch.java

@@ -20,19 +20,22 @@ public class HolonSwitch extends CpsObject {
 
 	public HolonSwitch(String ObjName) {
 		super(ObjName);
-		setState(false);
+		setState(true);
+		setActiveAt(true);
 	}
 
 	public HolonSwitch(String ObjName, String obj) {
 		super(ObjName);
 		super.setName(obj);
-		setState(false);
+		setState(true);
 		setActiveAt(true);
 	}
 
 	public HolonSwitch(CpsObject obj) {
 		super(obj);
-		setState(((HolonSwitch) obj).getState());
+		super.setName(obj.getName());
+		setState(true);
+		setActiveAt(true);
 	}
 
 	public void switchState() {
@@ -50,6 +53,11 @@ public class HolonSwitch extends CpsObject {
 
 	public void setState(boolean state) {
 		this.active = state;
+		if (this.active == true) {
+			setImage("/Images/switch-on.png");
+		} else {
+			setImage("/Images/switch-off.png");
+		}
 	}
 
 	/**

+ 1 - 1
src/ui/controller/CategoryController.java

@@ -33,7 +33,7 @@ public class CategoryController {
 		addNewHolonObject(mpC.searchCategory("Energy"), "Power Plant", new ArrayList<HolonElement>(), "/Images/power-plant.png");
 		addNewHolonObject(mpC.searchCategory("Building"), "House", new ArrayList<HolonElement>(), "/Images/home-2.png");
 		addNewHolonTransformer(mpC.searchCategory("Component"), "Transformer", "/Images/transformer-1.png");
-		addNewHolonSwitch(mpC.searchCategory("Component"), "Switch", "/Images/switch-off.png");
+		addNewHolonSwitch(mpC.searchCategory("Component"), "Switch", "/Images/switch-on.png");
 
 	}
 

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

@@ -166,7 +166,15 @@ class MyCanvas extends JPanel implements MouseListener, MouseMotionListener {
 			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(),
@@ -197,11 +205,6 @@ class MyCanvas extends JPanel implements MouseListener, MouseMotionListener {
 
 	@Override
 	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) {
-			((HolonSwitch) tempCps).switchState();
-		}
-		repaint();
 	}
 
 	@Override