123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- package de.tu_darmstadt.tk.SmartHomeNetworkSim.view;
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.event.ComponentEvent;
- import java.awt.event.ComponentListener;
- import java.util.Collection;
- import javax.swing.JPanel;
- import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.Controller;
- import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Connection;
- import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Model;
- import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.SmartDevice;
- @SuppressWarnings("serial")
- public class VisualisationPanel extends JPanel {
- /**
- * Smart Home model which is visualized
- */
- 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
- *
- * @param model
- * Model to visualize
- * @param control
- * Control, which changes the model
- */
- public VisualisationPanel(Model model, Controller control) {
- super();
- this.model = model;
- 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
- public void componentMoved(ComponentEvent e) {
- }
- @Override
- public void componentHidden(ComponentEvent e) {
- }
- });
- }
-
- @Override
- public void paint(Graphics g) {
- // paint white background
- g.setColor(Color.white);
- g.fillRect(0, 0, this.getWidth(), this.getHeight());
-
- paintConnections(g);
-
- paintDevices(g);
- }
- /**
- * Paints the smart devices of the Model
- *
- * @param g
- */
- public void paintDevices(Graphics g) {
- for (SmartDevice s : model.getDevices()) {
- int x = s.getX();
- int y = s.getY();
- if (s == interactor.dragged) {
- // Update visualization of dragged object
- x = interactor.dragged_x;
- y = interactor.dragged_y;
- }
- g.setColor(Color.WHITE);
- g.fillOval(x - visualisationRadius, y - visualisationRadius, 2*visualisationRadius-1, 2*visualisationRadius-1);
- g.setColor(Color.BLACK);
- g.drawOval(x - visualisationRadius, y - visualisationRadius, 2*visualisationRadius-1, 2*visualisationRadius-1);
- g.setColor(Color.BLUE);
- g.drawOval(x - visualisationRadius+2, y - visualisationRadius+2, 2*visualisationRadius-5, 2*visualisationRadius-5);
- g.setColor(Color.BLACK);
- g.drawString(s.getName(), x - g.getFontMetrics().stringWidth(s.getName()) / 2, y + visualisationRadius+11);
- }
- }
-
- /**
- * Paints the Connections
- * @param g
- */
- private void paintConnections(Graphics g){
- g.setColor(Color.RED);
- //For all Connections
- for (SmartDevice s : model.getDevices())
- for(Connection c: s.getConnections()){
- //Draw just once, if the device is the source
- if(c.getSource() == s){
- /**
- * All Devices that are part of the connection
- */
- Collection<SmartDevice> d = c.getParticipants();
- if(d.size() == 2){
-
- for(SmartDevice sd: d)
- if(s!=sd){
- //Check if dragged object
- int s_x,s_y,sd_x,sd_y;
- if(s == interactor.dragged){
- s_x = interactor.dragged_x;
- s_y = interactor.dragged_y;
- }else{
- s_x = s.getX();
- s_y = s.getY();
- }
- if(sd == interactor.dragged){
- sd_x = interactor.dragged_x;
- sd_y = interactor.dragged_y;
- }else{
- sd_x = sd.getX();
- sd_y = sd.getY();
- }
- g.drawLine(s_x, s_y, sd_x, sd_y);
- }
- }else if(d.size() == 1){
-
- } else if(d.size() <= 0){
- //Invalid Connection
- }else{
- //Draw MultiConnection
- }
- }
- }
- }
- /**
- * @return the visualisationRadius
- */
- public int getVisualisationRadius() {
- return visualisationRadius;
- }
- /**
- * @param visualisationRadius the visualisationRadius to set
- */
- public void setVisualisationRadius(int visualisationRadius) {
- this.visualisationRadius = visualisationRadius;
- }
- }
|