InitializeNodePositions.java 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. public static void placeNodes( LayeredGraphNode graph ) {
  12. RandomColorGenerator cGen = new RandomColorGenerator();
  13. for( LayeredGraphNode n : graph.getContainedNodes() )
  14. {
  15. n.setWidth( 40 );
  16. n.setHeight( 40 );
  17. n.setColor( cGen.generateColor() );
  18. placeNodes( n );
  19. }
  20. int curY = 0;
  21. double maxWidth = 0, maxHeight = 0;
  22. for (LayeredGraphNode node : graph.getContainedNodes()) { // Suche die maximale Knotengröße
  23. maxWidth = Math.max(node.getWidth(), maxWidth);
  24. maxHeight = Math.max(node.getHeight(), maxHeight);
  25. }
  26. ArrayList< ArrayList< LayeredGraphNode > > layers = graph.getContainedLayers();
  27. for (ArrayList<LayeredGraphNode> layer : layers) { // Gehe alle Layer durch
  28. maxHeight = 0;
  29. for (LayeredGraphNode node : layer) { // Gehe alle Knoten durch
  30. maxHeight = Math.max(node.getHeight(), maxHeight);
  31. }
  32. int curX = 0;
  33. for (LayeredGraphNode node : layer) { // Gehe alle Knoten durch
  34. node.setX(curX + maxWidth / 2 - node.getWidth() / 2, false);
  35. node.setY(curY + maxHeight / 2 - node.getHeight() / 2); // Position setzen
  36. curX += maxWidth + 25;
  37. }
  38. curY += maxHeight + 25;
  39. }
  40. }
  41. }