package lib; import java.awt.Color; import java.util.ArrayList; import bk.LayoutType; import graph.LayeredGraphNode; /** * Ein simpler Algorithmus zum Setzen der Knotenkoordinaten * Die Knoten werden in einem Raster ausgerichtet, so dass alle Knoten den gleichen Abstand zwischen sich haben (wenn sie gleich groß sind) * @author kolja * */ public class SimpleNodePlacement { /** * the actual algorithm * @param graph the graph where the node positions are to be set. */ public static void placeNodes( LayeredGraphNode graph ) { float hue = 0; // color hue float saturation = 1f; // color saturation float brightness = 0.75f; // color brightness for( LayeredGraphNode n : graph.getContainedNodes() ) { n.setColor( Color.getHSBColor( hue, saturation, brightness ), null ); hue += 0.15f; placeNodes( n ); } int curY = 0; double maxWidth = 0, maxHeight = 0; for (LayeredGraphNode node : graph.getContainedNodes()) { // Suche die maximale Knotengröße maxWidth = Math.max(node.getWidth( LayoutType.TOP_BOTTOM_LEFT ), maxWidth); maxHeight = Math.max(node.getHeight( LayoutType.TOP_BOTTOM_LEFT ), maxHeight); } ArrayList< ArrayList< LayeredGraphNode > > layers = graph.getContainedLayers(); for (ArrayList layer : layers) { // Gehe alle Layer durch maxHeight = 0; for (LayeredGraphNode node : layer) { // Gehe alle Knoten durch maxHeight = Math.max(node.getHeight( LayoutType.TOP_BOTTOM_LEFT ), maxHeight); } int curX = 0; for (LayeredGraphNode node : layer) { // Gehe alle Knoten durch node.setX( curX + maxWidth / 2 - node.getWidth( LayoutType.TOP_BOTTOM_LEFT ) / 2, false, null ); node.setY( curY + maxHeight / 2 - node.getHeight( LayoutType.TOP_BOTTOM_LEFT ) / 2, null ); // Position setzen curX += maxWidth + 20; } curY += maxHeight + 20; } } }