VisualisationPanel.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. package de.tu_darmstadt.tk.SmartHomeNetworkSim.view;
  2. import java.awt.Color;
  3. import java.awt.Graphics;
  4. import java.awt.event.ComponentEvent;
  5. import java.awt.event.ComponentListener;
  6. import java.util.Collection;
  7. import javax.swing.JPanel;
  8. import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.Controller;
  9. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Connection;
  10. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Model;
  11. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.SmartDevice;
  12. @SuppressWarnings("serial")
  13. public class VisualisationPanel extends JPanel {
  14. /**
  15. * Smart Home model which is visualized
  16. */
  17. private Model model;
  18. /**
  19. * Controller to notify when the model changes
  20. */
  21. private Controller control;
  22. /**
  23. * Listener which processes the GUI Interactions
  24. */
  25. private VisualisationInteractor interactor;
  26. /**
  27. * Radius of a circle around the SmartDevice (including middlepoint)
  28. */
  29. private int visualisationRadius = 17;
  30. /**
  31. * Initializes the Visualisation Panel
  32. *
  33. * @param model
  34. * Model to visualize
  35. * @param control
  36. * Control, which changes the model
  37. */
  38. public VisualisationPanel(Model model, Controller control) {
  39. super();
  40. this.model = model;
  41. this.control = control;
  42. this.interactor = new VisualisationInteractor(model, control, this);
  43. this.addMouseMotionListener(interactor);
  44. this.addMouseListener(interactor);
  45. }
  46. /**
  47. * Performs further initializations, which have to be performed, after the model was made visible
  48. */
  49. public void delayedInit(){
  50. this.addComponentListener(new ComponentListener() {
  51. @Override
  52. public void componentShown(ComponentEvent e) {
  53. }
  54. @Override
  55. public void componentResized(ComponentEvent e) {
  56. control.setDimension(getWidth(), getHeight(), model.getDepth(), true);
  57. repaint();
  58. }
  59. @Override
  60. public void componentMoved(ComponentEvent e) {
  61. }
  62. @Override
  63. public void componentHidden(ComponentEvent e) {
  64. }
  65. });
  66. }
  67. @Override
  68. public void paint(Graphics g) {
  69. // paint white background
  70. g.setColor(Color.white);
  71. g.fillRect(0, 0, this.getWidth(), this.getHeight());
  72. paintConnections(g);
  73. paintDevices(g);
  74. }
  75. /**
  76. * Paints the smart devices of the Model
  77. *
  78. * @param g
  79. */
  80. public void paintDevices(Graphics g) {
  81. for (SmartDevice s : model.getDevices()) {
  82. int x = s.getX();
  83. int y = s.getY();
  84. if (s == interactor.dragged) {
  85. // Update visualization of dragged object
  86. x = interactor.dragged_x;
  87. y = interactor.dragged_y;
  88. }
  89. g.setColor(Color.WHITE);
  90. g.fillOval(x - visualisationRadius, y - visualisationRadius, 2*visualisationRadius-1, 2*visualisationRadius-1);
  91. g.setColor(Color.BLACK);
  92. g.drawOval(x - visualisationRadius, y - visualisationRadius, 2*visualisationRadius-1, 2*visualisationRadius-1);
  93. g.setColor(Color.BLUE);
  94. g.drawOval(x - visualisationRadius+2, y - visualisationRadius+2, 2*visualisationRadius-5, 2*visualisationRadius-5);
  95. g.setColor(Color.BLACK);
  96. g.drawString(s.getName(), x - g.getFontMetrics().stringWidth(s.getName()) / 2, y + visualisationRadius+11);
  97. }
  98. }
  99. /**
  100. * Paints the Connections
  101. * @param g
  102. */
  103. private void paintConnections(Graphics g){
  104. g.setColor(Color.RED);
  105. //For all Connections
  106. for (SmartDevice s : model.getDevices())
  107. for(Connection c: s.getConnections()){
  108. //Draw just once, if the device is the source
  109. if(c.getSource() == s){
  110. /**
  111. * All Devices that are part of the connection
  112. */
  113. Collection<SmartDevice> d = c.getParticipants();
  114. if(d.size() == 2){
  115. for(SmartDevice sd: d)
  116. if(s!=sd){
  117. //Check if dragged object
  118. int s_x,s_y,sd_x,sd_y;
  119. if(s == interactor.dragged){
  120. s_x = interactor.dragged_x;
  121. s_y = interactor.dragged_y;
  122. }else{
  123. s_x = s.getX();
  124. s_y = s.getY();
  125. }
  126. if(sd == interactor.dragged){
  127. sd_x = interactor.dragged_x;
  128. sd_y = interactor.dragged_y;
  129. }else{
  130. sd_x = sd.getX();
  131. sd_y = sd.getY();
  132. }
  133. g.drawLine(s_x, s_y, sd_x, sd_y);
  134. }
  135. }else if(d.size() == 1){
  136. } else if(d.size() <= 0){
  137. //Invalid Connection
  138. }else{
  139. //Draw MultiConnection
  140. }
  141. }
  142. }
  143. }
  144. /**
  145. * @return the visualisationRadius
  146. */
  147. public int getVisualisationRadius() {
  148. return visualisationRadius;
  149. }
  150. /**
  151. * @param visualisationRadius the visualisationRadius to set
  152. */
  153. public void setVisualisationRadius(int visualisationRadius) {
  154. this.visualisationRadius = visualisationRadius;
  155. }
  156. }