InitializeNodePositions.java 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package Algorithms;
  2. import java.util.ArrayList;
  3. import Model.LayeredGraphNode;
  4. /**
  5. * Ein simpler Algorithmus zum Setzen der Knotenkoordinaten
  6. * Die Knoten werden in einem Raster ausgerichtet, so dass alle Knoten den gleichen Abstand zwischen sich haben (wenn sie gleich groß sind)
  7. * @author kolja
  8. *
  9. */
  10. public class InitializeNodePositions {
  11. /**
  12. * the actual algorithm
  13. * @param graph the graph where the node positions are to be set.
  14. */
  15. public static void placeNodes( LayeredGraphNode graph ) {
  16. RandomColorGenerator cGen = new RandomColorGenerator();
  17. for( LayeredGraphNode n : graph.getContainedNodes() )
  18. {
  19. n.setWidth( 40 );
  20. n.setHeight( 40 );
  21. n.setColor( cGen.generateColor() );
  22. placeNodes( n );
  23. }
  24. int curY = 0;
  25. double maxWidth = 0, maxHeight = 0;
  26. for (LayeredGraphNode node : graph.getContainedNodes()) { // Suche die maximale Knotengröße
  27. maxWidth = Math.max(node.getWidth(), maxWidth);
  28. maxHeight = Math.max(node.getHeight(), maxHeight);
  29. }
  30. ArrayList< ArrayList< LayeredGraphNode > > layers = graph.getContainedLayers();
  31. for (ArrayList<LayeredGraphNode> layer : layers) { // Gehe alle Layer durch
  32. maxHeight = 0;
  33. for (LayeredGraphNode node : layer) { // Gehe alle Knoten durch
  34. maxHeight = Math.max(node.getHeight(), maxHeight);
  35. }
  36. int curX = 0;
  37. for (LayeredGraphNode node : layer) { // Gehe alle Knoten durch
  38. node.setX(curX + maxWidth / 2 - node.getWidth() / 2, false);
  39. node.setY(curY + maxHeight / 2 - node.getHeight() / 2); // Position setzen
  40. curX += maxWidth + 25;
  41. }
  42. curY += maxHeight + 25;
  43. }
  44. }
  45. }