|
@@ -2,11 +2,12 @@ package de.tu_darmstadt.tk.SmartHomeNetworkSim.view;
|
|
|
|
|
|
import java.awt.Color;
|
|
import java.awt.Color;
|
|
import java.awt.Graphics;
|
|
import java.awt.Graphics;
|
|
-import java.awt.event.MouseEvent;
|
|
+import java.awt.event.ComponentEvent;
|
|
-import java.awt.event.MouseMotionListener;
|
|
+import java.awt.event.ComponentListener;
|
|
|
|
|
|
import javax.swing.JPanel;
|
|
import javax.swing.JPanel;
|
|
|
|
|
|
|
|
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.Controller;
|
|
import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Model;
|
|
import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Model;
|
|
import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.SmartDevice;
|
|
import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.SmartDevice;
|
|
|
|
|
|
@@ -17,60 +18,113 @@ public class VisualisationPanel extends JPanel {
|
|
* Smart Home model which is visualized
|
|
* Smart Home model which is visualized
|
|
*/
|
|
*/
|
|
private Model model;
|
|
private Model model;
|
|
-
|
|
+
|
|
|
|
+
|
|
|
|
+ * Controller to notify when the model changes
|
|
|
|
+ */
|
|
|
|
+ private Controller control;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ * Listener which processes the GUI Interactions
|
|
|
|
+ */
|
|
|
|
+ private VisualisationInteractor interactor;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ * Radius of a circle around the SmartDevice (including middlepoint)
|
|
|
|
+ */
|
|
|
|
+ private int visualisationRadius = 17;
|
|
|
|
+
|
|
|
|
|
|
* Initializes the Visualisation Panel
|
|
* Initializes the Visualisation Panel
|
|
- * @param model Model to visualize
|
|
+ *
|
|
|
|
+ * @param model
|
|
|
|
+ * Model to visualize
|
|
|
|
+ * @param control
|
|
|
|
+ * Control, which changes the model
|
|
*/
|
|
*/
|
|
- public VisualisationPanel(Model model) {
|
|
+ public VisualisationPanel(Model model, Controller control) {
|
|
super();
|
|
super();
|
|
|
|
+
|
|
this.model = model;
|
|
this.model = model;
|
|
- this.addMouseMotionListener(new MouseMotionListener() {
|
|
+ this.control = control;
|
|
-
|
|
+
|
|
|
|
+ this.interactor = new VisualisationInteractor(model, control, this);
|
|
|
|
+
|
|
|
|
+ this.addMouseMotionListener(interactor);
|
|
|
|
+ this.addMouseListener(interactor);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ * Performs further initializations, which have to be performed, after the model was made visible
|
|
|
|
+ */
|
|
|
|
+ public void delayedInit(){
|
|
|
|
+ this.addComponentListener(new ComponentListener() {
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void componentShown(ComponentEvent e) {
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void componentResized(ComponentEvent e) {
|
|
|
|
+ control.setDimension(getWidth(), getHeight(), model.getDepth(), true);
|
|
|
|
+ repaint();
|
|
|
|
+ }
|
|
|
|
+
|
|
@Override
|
|
@Override
|
|
- public void mouseMoved(MouseEvent e) {
|
|
+ public void componentMoved(ComponentEvent e) {
|
|
- if( e != null && e.getPoint() != null){
|
|
|
|
- SmartDevice move = model.getDevices().get(0);
|
|
|
|
- if(e.getX()>15 && e.getX()<getWidth()-16)
|
|
|
|
- move.setX(e.getX());
|
|
|
|
- if(e.getY()>15 && e.getY()<getHeight()-16)
|
|
|
|
- move.setY(e.getY());
|
|
|
|
- repaint();
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
}
|
|
}
|
|
-
|
|
+
|
|
@Override
|
|
@Override
|
|
- public void mouseDragged(MouseEvent e) {
|
|
+ public void componentHidden(ComponentEvent e) {
|
|
-
|
|
|
|
-
|
|
|
|
}
|
|
}
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public void paint(Graphics g) {
|
|
public void paint(Graphics g) {
|
|
- super.paint(g);
|
|
+
|
|
-
|
|
|
|
g.setColor(Color.white);
|
|
g.setColor(Color.white);
|
|
g.fillRect(0, 0, this.getWidth(), this.getHeight());
|
|
g.fillRect(0, 0, this.getWidth(), this.getHeight());
|
|
-
|
|
+
|
|
paintDevices(g);
|
|
paintDevices(g);
|
|
}
|
|
}
|
|
-
|
|
+
|
|
|
|
|
|
* Paints the smart devices of the Model
|
|
* Paints the smart devices of the Model
|
|
|
|
+ *
|
|
* @param g
|
|
* @param g
|
|
*/
|
|
*/
|
|
- public void paintDevices(Graphics g){
|
|
+ public void paintDevices(Graphics g) {
|
|
-
|
|
+
|
|
- for(SmartDevice s:model.getDevices()){
|
|
+ for (SmartDevice s : model.getDevices()) {
|
|
|
|
+ int x = s.getX();
|
|
|
|
+ int y = s.getY();
|
|
|
|
+ if (s == interactor.dragged) {
|
|
|
|
+
|
|
|
|
+ x = interactor.dragged_x;
|
|
|
|
+ y = interactor.dragged_y;
|
|
|
|
+ }
|
|
g.setColor(Color.BLACK);
|
|
g.setColor(Color.BLACK);
|
|
- g.drawOval(s.getX()-16, s.getY()-16, 32, 32);
|
|
+ g.drawOval(x - visualisationRadius, y - visualisationRadius, 2*visualisationRadius-1, 2*visualisationRadius-1);
|
|
g.setColor(Color.BLUE);
|
|
g.setColor(Color.BLUE);
|
|
- g.drawOval(s.getX()-14, s.getY()-14, 28, 28);
|
|
+ g.drawOval(x - visualisationRadius+2, y - visualisationRadius+2, 2*visualisationRadius-5, 2*visualisationRadius-5);
|
|
g.setColor(Color.BLACK);
|
|
g.setColor(Color.BLACK);
|
|
- g.drawString(s.getName(), s.getX()-g.getFontMetrics().stringWidth(s.getName())/2, s.getY()+26);
|
|
+ g.drawString(s.getName(), x - g.getFontMetrics().stringWidth(s.getName()) / 2, y + visualisationRadius+11);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ * @return the visualisationRadius
|
|
|
|
+ */
|
|
|
|
+ public int getVisualisationRadius() {
|
|
|
|
+ return visualisationRadius;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ * @param visualisationRadius the visualisationRadius to set
|
|
|
|
+ */
|
|
|
|
+ public void setVisualisationRadius(int visualisationRadius) {
|
|
|
|
+ this.visualisationRadius = visualisationRadius;
|
|
|
|
+ }
|
|
|
|
+
|
|
}
|
|
}
|