InitializeNodePositions.java 2.1 KB

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