HolarchyPanel.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. package ui.view.holarchy;
  2. import java.awt.Color;
  3. import java.awt.Graphics;
  4. import java.awt.Graphics2D;
  5. import java.awt.event.MouseEvent;
  6. import java.awt.event.MouseListener;
  7. import java.awt.geom.Ellipse2D;
  8. import java.util.ArrayList;
  9. import java.util.HashMap;
  10. import java.util.List;
  11. import java.util.Map;
  12. import javax.swing.JLabel;
  13. import javax.swing.JPanel;
  14. import javax.swing.JSeparator;
  15. import javax.swing.JSlider;
  16. import javax.swing.SwingConstants;
  17. import javax.swing.event.ChangeEvent;
  18. import javax.swing.event.ChangeListener;
  19. import classes.Holon;
  20. import ui.controller.Control;
  21. import util.Random;
  22. public class HolarchyPanel extends JPanel {
  23. Control control;
  24. HolarchyWindow parentFrame;
  25. JLabel layerLabelTitle;
  26. JSlider slider;
  27. JSeparator sep, sep2;
  28. int displayedLayer;
  29. int minRadius = 24;
  30. Holon root;
  31. ArrayList<Map<Ellipse2D, Map<Ellipse2D, Holon>>> ellipsesChild;
  32. ArrayList<Map<Ellipse2D, Holon>> ellipses;
  33. ArrayList<ArrayList<Holon>> layeredHolons;
  34. ArrayList<Ellipse2D> surroundings;
  35. Map<Holon, Color> coloredHolons;
  36. public HolarchyPanel(HolarchyWindow parentFrame, Control control) {
  37. this.control = control;
  38. this.parentFrame = parentFrame;
  39. this.root = control.getModel().getStateHolon();
  40. this.setBounds(0, 0, parentFrame.getWidth()*4/5, parentFrame.getHeight());
  41. this.setLayout(null);
  42. this.setBackground(Color.white);
  43. this.addMouseListener(mouseListener);
  44. this.layerLabelTitle = new JLabel("Layer");
  45. this.layerLabelTitle.setBounds(5, 5, 50, 20);
  46. this.layerLabelTitle.setHorizontalAlignment(SwingConstants.CENTER);
  47. this.add(this.layerLabelTitle);
  48. this.slider = new JSlider();
  49. this.slider.setOrientation(SwingConstants.VERTICAL);
  50. this.slider.setBounds(5, 30, 50, 200);
  51. this.slider.setPaintLabels(true);
  52. this.slider.setPaintTicks(true);
  53. this.slider.setSnapToTicks(true);
  54. this.slider.addChangeListener(this.sliderChangeListener);
  55. this.slider.setValue(0);
  56. this.slider.setInverted(true);
  57. this.slider.setFocusable(false);
  58. this.slider.setBackground(Color.white);
  59. this.add(slider);
  60. this.sep = new JSeparator(SwingConstants.VERTICAL);
  61. this.sep.setBounds(60, 0, 1, 235);
  62. this.add(this.sep);
  63. this.sep2 = new JSeparator(SwingConstants.HORIZONTAL);
  64. this.sep2.setBounds(0, 235, 60, 1);
  65. this.add(this.sep2);
  66. this.ellipses = new ArrayList();
  67. this.ellipsesChild = new ArrayList();
  68. this.displayedLayer = 0;
  69. this.layeredHolons = new ArrayList();
  70. this.surroundings = new ArrayList();
  71. this.coloredHolons = new HashMap();
  72. getAllHolons();
  73. setSliderMax(this.layeredHolons.size());
  74. createAllLayers();
  75. generateColorSet();
  76. }
  77. public void paintComponent(Graphics g) {
  78. super.paintComponent(g);
  79. paintLayer(g, this.displayedLayer);
  80. }
  81. public void displayLayer(int layer) {
  82. this.displayedLayer = layer;
  83. this.repaint();
  84. }
  85. public void paintLayer(Graphics g, int layer) {
  86. this.displayedLayer = layer;
  87. Graphics2D g2 = (Graphics2D) g;
  88. //resize panel
  89. Ellipse2D surrounding = this.surroundings.get(this.displayedLayer);
  90. if(surrounding.getWidth() > this.getWidth()) {
  91. this.setSize((int) surrounding.getWidth(), this.getHeight());
  92. // this.parentFrame.resizeScrollPane((int) surrounding.getWidth(), this.getHeight());
  93. }
  94. if(surrounding.getHeight() > this.getHeight()) {
  95. this.setSize(this.getWidth(), (int) surrounding.getHeight());
  96. // this.parentFrame.resizeScrollPane(this.getWidth(), (int) surrounding.getHeight());
  97. }
  98. // System.out.println("painting layer "+layer+"#elements="+this.ellipses.get(layer).size());
  99. for(Ellipse2D e : this.ellipses.get(this.displayedLayer).keySet()) {
  100. // System.out.println("paint ellipse");
  101. //draw ellipse
  102. g2.setColor( this.coloredHolons.get(this.ellipses.get(this.displayedLayer).get(e)) );
  103. g2.draw(e);
  104. //draw child ellipses and add them to global list
  105. for(Ellipse2D e2 : this.ellipsesChild.get(layer).get(e).keySet()) {
  106. g2.setColor( this.coloredHolons.get(this.ellipsesChild.get(layer).get(e).get(e2)) );
  107. g2.draw(e2);
  108. }
  109. }
  110. }
  111. public void createAllLayers() {
  112. for(int layer=0; layer<this.layeredHolons.size(); layer++) {
  113. // System.out.println("creating layer "+layer+" #elements="+this.layeredHolons.get(layer).size());
  114. this.ellipsesChild.add(layer, new HashMap());
  115. // System.out.println("creating ellipse");
  116. //estimate the required area, center if it is smaller than the panels size
  117. Ellipse2D surrounding = estimateSurrounding(60, 0, layer);
  118. double newX = surrounding.getX(), newY = surrounding.getY();
  119. if(surrounding.getWidth() <= this.getWidth()) {
  120. double diff = this.getWidth() - surrounding.getWidth();
  121. newX = diff/2;
  122. }
  123. if(surrounding.getHeight() <= this.getHeight()) {
  124. double diff = this.getHeight() - surrounding.getHeight();
  125. newY = diff/2;
  126. }
  127. surrounding = new Ellipse2D.Double(newX, newY, surrounding.getWidth(), surrounding.getHeight());
  128. this.surroundings.add(layer, surrounding);
  129. //create ellipse for each holon
  130. this.ellipses.add(layer, placeHolons(this.layeredHolons.get(layer), surrounding, true));
  131. // System.out.println("paint surrounding width="+surrounding.getWidth()+" height="+surrounding.getHeight());
  132. // System.out.println("pending holons "+this.ellipses.get(layer).size());
  133. for(Ellipse2D e : this.ellipses.get(layer).keySet()) {
  134. //draw child ellipses and add them to global list
  135. Map<Ellipse2D, Holon> ellipses2 = placeHolons(ellipses.get(layer).get(e).childHolons, e, false);
  136. this.ellipsesChild.get(layer).put(e, ellipses2);
  137. }
  138. }
  139. }
  140. /**
  141. * randomly place an ellipse for each holon in holons inside the given ellipse without overlapping
  142. * @param holons
  143. * @param surrounding
  144. * @param drawChildren
  145. * @return
  146. */
  147. public Map<Ellipse2D, Holon> placeHolons(List<Holon> holons, Ellipse2D surrounding, boolean drawChildren) {
  148. double startX = surrounding.getX();
  149. double startY = surrounding.getY();
  150. double width = surrounding.getWidth();
  151. double height = surrounding.getHeight();
  152. Map<Ellipse2D, Holon> shapes = new HashMap();
  153. int abord = 0;
  154. int i = 0;
  155. while(shapes.size() < holons.size() && abord < 1000) {
  156. Holon h = holons.get(i);
  157. int radius = this.minRadius;
  158. if(drawChildren)
  159. radius = this.minRadius * (h.getChildCount()+1) + 10;
  160. //randomly place an ellipse for the holon
  161. double x = startX + (width * Math.random());
  162. double y = startY + (height * Math.random());
  163. //check if ellipse is inside the surrounding
  164. boolean overlap = !surrounding.contains(x, y, radius*2, radius*2);
  165. if(overlap) {
  166. continue;
  167. }
  168. Ellipse2D e = new Ellipse2D.Double(x-5, y-5, radius*2+10, radius*2+10);
  169. //check whether or not the random ellipse overlaps with an existing one
  170. for(Ellipse2D e2 : shapes.keySet()) {
  171. if(e.intersects(e2.getX()-5, e2.getY()-5, e2.getWidth()+10, e2.getHeight()+10)) {
  172. overlap = true;
  173. break;
  174. }
  175. }
  176. // System.out.println("try to add ellipse "+i+" x="+e.getX()+" y="+e.getY());
  177. abord++;
  178. //if not add the ellipse to the already existing ones
  179. if(!overlap) {
  180. shapes.put(e, h);
  181. // System.out.println("added ellipse "+i+" at ("+e.getX()+", "+e.getY()+")");
  182. i++;
  183. abord = 0;
  184. }
  185. }
  186. return shapes;
  187. }
  188. public Ellipse2D estimateSurrounding(int x, int y, int layer) {
  189. if(this.layeredHolons == null || this.layeredHolons.size() < layer+1)
  190. return new Ellipse2D.Double(x, y, Integer.MAX_VALUE, Integer.MAX_VALUE);
  191. int w = 0;
  192. for(Holon h : this.layeredHolons.get(layer)) {
  193. w += this.minRadius * (h.getChildCount()+1);
  194. }
  195. w = w * 4 + 10;
  196. return new Ellipse2D.Double(x, y, w, w);
  197. }
  198. /**
  199. * @deprecated
  200. * calculate distance between the two specified ellipses and check if they overlap
  201. * @param e1
  202. * @param e2
  203. * @return
  204. */
  205. public boolean overlap(Ellipse2D e1, Ellipse2D e2) {
  206. int dist = (int) Math.sqrt( Math.pow((e1.getCenterX()+e2.getCenterX()), 2) + Math.pow((e1.getCenterY()+e2.getCenterY()), 2) );
  207. if(dist < e1.getWidth()/2 + e2.getWidth()/2 + 10)
  208. return true;
  209. return false;
  210. }
  211. public void getAllHolons() {
  212. this.layeredHolons.add(this.root.childHolons);
  213. //TODO: potential infinit loop
  214. while(true) {
  215. //new layer
  216. ArrayList<Holon> children = new ArrayList();
  217. for(Holon h : this.layeredHolons.get(this.layeredHolons.size()-1)) {
  218. children.addAll(h.childHolons);
  219. }
  220. if(children.size() > 0) {
  221. this.layeredHolons.add(children);
  222. } else {
  223. break;
  224. }
  225. }
  226. }
  227. public void setSliderMax(int n) {
  228. if(this.slider == null || n < 0)
  229. return;
  230. this.slider.setMaximum(n-1);
  231. if(n < 11) {
  232. this.slider.setMajorTickSpacing(1);
  233. } else if (n < 101) {
  234. this.slider.setMajorTickSpacing(10);
  235. this.slider.setMinorTickSpacing(1);
  236. } else {
  237. this.slider.setMajorTickSpacing(100);
  238. this.slider.setMinorTickSpacing(10);
  239. }
  240. }
  241. /**
  242. * generate a distinct color for each Holon in each layer
  243. */
  244. public void generateColorSet() {
  245. for(int layer=0; layer<this.layeredHolons.size(); layer++) {
  246. int numColors = 0;
  247. numColors = this.layeredHolons.get(layer).size();
  248. if(numColors <= 0)
  249. continue;
  250. // System.out.println("generating colors for "+numColors+" holons");
  251. ArrayList<Color> colors = new ArrayList();
  252. float hue = Random.nextFloatInRange(0, 1);
  253. for(float i = 0; i < 360; i += 360 / numColors) {
  254. hue += 0.618033988749895f;
  255. float saturation = 0.5f; //90 + Random.nextFloatInRange(0, 10);
  256. float lightness = 0.95f; //50 + Random.nextFloatInRange(0, 10);
  257. Color c = Color.getHSBColor(hue%1, saturation, lightness);
  258. colors.add(c);
  259. // System.out.println("generated color "+c.toString()+" by h "+hue+" sat "+saturation+" light "+lightness);
  260. }
  261. for(int j=0;j<this.layeredHolons.get(layer).size(); j++) {
  262. this.coloredHolons.put(this.layeredHolons.get(layer).get(j), colors.get(j));
  263. // System.out.println("generated color "+colors.get(j).toString()+" for holon "+this.layeredHolons.get(layer).get(j));
  264. }
  265. }
  266. }
  267. MouseListener mouseListener = new MouseListener() {
  268. @Override
  269. public void mouseClicked(MouseEvent arg0) {
  270. // System.out.println("mouse click at ("+arg0.getX()+","+arg0.getY()+")");
  271. //Get clicked Holon
  272. for(Ellipse2D e : ellipses.get(displayedLayer).keySet()) {
  273. if(e.contains(arg0.getX(), arg0.getY())) {
  274. //found ellipse
  275. boolean child = false;
  276. //check if one its children was clicked
  277. for(Ellipse2D e2 : ellipsesChild.get(displayedLayer).get(e).keySet()) {
  278. if(e2.contains(arg0.getX(), arg0.getY())) {
  279. //child was clicked
  280. child = true;
  281. parentFrame.displayHolonInfos(ellipsesChild.get(displayedLayer).get(e).get(e2));
  282. // System.out.println("child holon "+ellipsesChild.get(e).get(e2).name+" was clicked");
  283. break;
  284. }
  285. }
  286. if(!child) {
  287. //no child was clicked
  288. parentFrame.displayHolonInfos(ellipses.get(displayedLayer).get(e));
  289. // System.out.println("holon "+ellipses.get(e).name+" was clicked");
  290. }
  291. break;
  292. }
  293. }
  294. }
  295. @Override
  296. public void mouseEntered(MouseEvent arg0) {
  297. //not important
  298. }
  299. @Override
  300. public void mouseExited(MouseEvent arg0) {
  301. //not important
  302. }
  303. @Override
  304. public void mousePressed(MouseEvent arg0) {
  305. //not important
  306. }
  307. @Override
  308. public void mouseReleased(MouseEvent arg0) {
  309. //not important
  310. }
  311. };
  312. ChangeListener sliderChangeListener = new ChangeListener() {
  313. @Override
  314. public void stateChanged(ChangeEvent arg0) {
  315. JSlider source = (JSlider) arg0.getSource();
  316. if(!source.getValueIsAdjusting()) {
  317. displayLayer(source.getValue());
  318. parentFrame.clearInfos();
  319. }
  320. }
  321. };
  322. }