SimpleNodePlacement.java 2.1 KB

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