public void paintComponent(Graphics g) {
// Paint background
super.paintComponent(g);
g.drawOval(10, 225, 8, 8);
g.drawLine(12, 229, 40, 229);
g.drawLine(40, 189, 40, 269);
g.drawLine(40, 189, 50, 189);
g.drawLine(40, 269, 50, 269);
g.drawString("S\u2081", 52, 194);
g.drawString("S\u2082", 52, 274);
g.drawLine(88, 189, 104, 189);
g.drawLine(88, 269, 104, 269);
g.drawLine(104, 174, 104, 203);
g.drawLine(104, 254, 104, 283);
}
package graph;
import java.util.ArrayList;
import java.util.Iterator;
public class Node {
/**
* Positionsangaben:
*/
int width = 50;
int height = 50;
int x;
int y;
/**
* Eine Liste der Kinder
*/
ArrayList<Node> children = new ArrayList<Node>();
/**
* Fügt ein neues Kind dazu
*
* @param child
*/
public void add(Node child) {
children.add(child);
}
/**
* Getter für alle Kinder
*
* @return
*/
public ArrayList<Node> getChildren() {
return children;
}
/**
* Berechnet die Höhe die alle Kinder zeichnerisch einnehmen. Sollten keine
* Kinder vorhanden sein, ist das Ergebnis gleich der eigenen Höhe.
*
* @param space
* @return
*/
public int getHeightOfChildren(int space) {
int height = 0;
int childHeight;
if (!children.isEmpty()) {
Iterator<Node> iterator = children.iterator();
while (iterator.hasNext()) {
Node child = iterator.next();
childHeight = child.getHeightOfChildren(space);
if (childHeight == 0) {
childHeight = child.getHeight();
}
height += childHeight + space;
}
height -= space;
} else {
height = this.height;
}
return height;
}
/**
* Berechnet die eigene Position und die der Kinder, relativ zu Punkt
* (sx|sy), mit Rücksicht auf den Platz, den die Kinder brauchen.
*
* @param sx
* @param sy
* @param spaceX
* @param spaceY
*/
public void calculatePosition(int sx, int sy, int spaceX, int spaceY) {
int childrenHeight = getHeightOfChildren(spaceY);
x = sx;
y = sy + (childrenHeight - height) / 2;
// Alle Kinder berrechnen
Iterator<Node> iterator = children.iterator();
while (iterator.hasNext()) {
Node child = iterator.next();
child.calculatePosition(sx + width + spaceX, sy, spaceX, spaceY);
sy += child.getHeightOfChildren(spaceY) + spaceY;
}
}
public int getHeight() {
return height;
}
public int getWidth() {
return width;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
}
package graph;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.Iterator;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class GraphPanel extends JPanel {
private static final long serialVersionUID = 1L;
/**
* Das Ursprungs-Blatt. Hier ist die ganze Baumstruktur drin.
*/
Node root = new Node();
/**
* Erstellt eine Test-Baumstruktur.
*/
public void init() {
Node node1 = new Node();
Node node2 = new Node();
Node node3 = new Node();
node3.add(new Node());
node3.add(new Node());
node3.add(new Node());
node3.add(new Node());
node2.add(new Node());
node2.add(new Node());
node1.add(new Node());
node1.add(node2);
node1.add(node3);
root.add(node1);
root.add(new Node());
root.add(new Node());
root.calculatePosition(10, 10, 20, 10);
}
@Override
public void paint(Graphics g) {
drawNodes(root, (Graphics2D) g);
}
/**
* Zechnet genau ein Node an seiner Position.
*
* @param node
* @param g
*/
public void drawNode(Node node, Graphics2D g) {
g.setColor(Color.blue);
g.drawRect(node.getX(), node.getY(), node.getWidth(), node.getHeight());
}
/**
* Zeichnet das Node und alle seine Kinder.
*
* @param node
* @param g
*/
public void drawNodes(Node node, Graphics2D g) {
drawNode(node, g);
Iterator<Node> iterator = node.getChildren().iterator();
while (iterator.hasNext()) {
Node child = iterator.next();
drawNodes(child, g);
drawLineBetween(node, child, g);
}
}
/**
* Zeichnet die Linie zwischen zwei Nodes.
*
*
* @param start
* @param end
* @param g
*/
public void drawLineBetween(Node start, Node end, Graphics2D g) {
int startX = start.getX() + start.getWidth();
int endX = end.getX();
int centerX = startX + (endX - startX) / 2;
int startY = start.getY() + start.getHeight() / 2;
int endY = end.getY() + end.getHeight() / 2;
g.setColor(Color.black);
g.drawLine(startX, startY, centerX, startY);
g.drawLine(centerX, startY, centerX, endY);
g.drawLine(centerX, endY, endX, endY);
}
public static void main(String[] args) {
GraphPanel graphPanel = new GraphPanel();
graphPanel.init();
JFrame frame = new JFrame();
frame.add(graphPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1024, 768);
frame.setVisible(true);
}
}