for (Vertex vertex : graph.getVertices())
{
Point2D point = layout.transform(vertex);
System.out.println("point of " + vertex + " is " + point);
Dimension size = vertexDimensionGetter.get(vertex);
shapes.add(new RectangleWithRef(new Rectangle2D.Double(point.getX(),
point.getY(), size.width, size.height), vertex));
}
boolean optimize = true;
// Optimize layout
if (optimize)
{
// Sort by X-Coordinates
for (RectangleWithRef rect : shapes)
{
rect.setSortType(RectangleWithRef.Sorting.X);
}
Collections.sort(shapes);
// Part1 translate overlapping recangles
int size = shapes.size();
for (int i = 0; i < size; i++)
{
RectangleWithRef rectangle = shapes.get(i);
int xTranslation = 0;
for (int j = i + 1; j < size; j++)
{
RectangleWithRef rectangle2 = shapes.get(j);
if (rectangle2.x > rectangle.getRightX())
{
break;
} else if (rectangle2.intersects(rectangle))
{
// move all followin components to the right
for (int k = j; k < size; k++)
{
shapes.get(k).translateRight(rectangle.width);
}
break;
}
}
}
int vertexDistance = 40;// distance between vertices
int lineDistance = 5;// distance between lines and other objects
// Part2 compress X
RectangleWithRef rectangle0 = shapes.get(0);
if (rectangle0.x > vertexDistance)
rectangle0.x = vertexDistance;
for (int i = 1; i < size; i++)
{
RectangleWithRef rectangle = shapes.get(i - 1);
RectangleWithRef rectangle2 = shapes.get(i);
if (rectangle2.x > rectangle.getRightX() + vertexDistance)
{
if (rectangle2.y > rectangle.y + rectangle.height
+ vertexDistance)
rectangle2.x = rectangle.x;
else if (rectangle2.getBottomY() < rectangle.y - vertexDistance)
rectangle2.x = rectangle.x;
else
rectangle2.x = rectangle.getRightX() + vertexDistance;
}
}
// Part3 compress Y
if (rectangle0.y > vertexDistance)
rectangle0.y = vertexDistance;
for (int i = 1; i < size; i++)
{
RectangleWithRef rectangle = shapes.get(i - 1);
RectangleWithRef rectangle2 = shapes.get(i);
if (rectangle2.y > rectangle.getBottomY() + vertexDistance)
{
rectangle2.y = rectangle.getBottomY() + vertexDistance;
}