浏览代码

Draws deleted MQTT Broker at average position of participants

Andreas T. Meyer-Berg 6 年之前
父节点
当前提交
0876fe5709
共有 1 个文件被更改,包括 30 次插入5 次删除
  1. 30 5
      src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/view/VisualisationPanel.java

+ 30 - 5
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/view/VisualisationPanel.java

@@ -156,6 +156,28 @@ public class VisualisationPanel extends JPanel {
 			Collection<Port> d = c.getParticipants();
 			if(c.getProtocol() instanceof MQTT_protocol){
 				Port broker = c.getProtocol().getDevicesWithRole(0).iterator().next();
+				int broker_x = 0, broker_y = 0;
+				if(broker==null){
+					/**
+					 * if broker was removed, paint broker at the average position
+					 */
+					for(Port p: c.getParticipants()){
+						if(p.getOwner()==interactor.dragged){
+							broker_x+=interactor.dragged_x;
+							broker_y+=interactor.dragged_y;
+						}else{
+
+							broker_x+=p.getOwner().getX();
+							broker_y+=p.getOwner().getY();
+						}
+					}
+					broker_x/=c.getParticipants().size();
+					broker_y/=c.getParticipants().size();
+					g.fillOval(broker_x-visualisationRadius/4, broker_y-visualisationRadius/4, visualisationRadius/2, visualisationRadius/2);
+				}else{
+					broker_x=broker.getOwner().getX();
+					broker_y=broker.getOwner().getY();
+				}
 				for(Port sd:d){
 					if(!model.getDevices().contains(sd.getOwner())){
 						SmartDevice removed = sd.getOwner();
@@ -169,11 +191,14 @@ public class VisualisationPanel extends JPanel {
 							s_x = interactor.dragged_x;
 							s_y = interactor.dragged_y;
 						} else if(broker==null){
-							s_x=model.getWidth()/2;
-							s_y=model.getHeight()/2;
+							/**
+							 * if broker was removed, paint edges to the previously calculated average position
+							 */
+							s_x=broker_x;
+							s_y=broker_y;
 						}else{
-							s_x = broker.getOwner().getX();
-							s_y = broker.getOwner().getY();
+							s_x = broker_x;
+							s_y = broker_y;
 						}
 						if (sd.getOwner() == interactor.dragged) {
 							sd_x = interactor.dragged_x;
@@ -234,7 +259,7 @@ public class VisualisationPanel extends JPanel {
 	/**
 	 * Returns the visualization radius of SmartDevices
 	 * 
-	 * @return the visualitation radius of SmartDevices
+	 * @return the visualization radius of SmartDevices
 	 */
 	public int getVisualisationRadius() {
 		return visualisationRadius;