-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
303 additions
and
98 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
src/main/java/org/behrang/algorithm/tree/DimensionCalculator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package org.behrang.algorithm.tree; | ||
|
||
import java.awt.*; | ||
import java.awt.geom.Dimension2D; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
public class DimensionCalculator { | ||
public static <T> Dimension2D calculateTreeDimension(Node<T> root) { | ||
final AtomicReference<Double> minX = new AtomicReference<>(Double.MAX_VALUE); | ||
final AtomicReference<Double> minY = new AtomicReference<>(Double.MAX_VALUE); | ||
final AtomicReference<Double> maxX = new AtomicReference<>(Double.MIN_VALUE); | ||
final AtomicReference<Double> maxY = new AtomicReference<>(Double.MIN_VALUE); | ||
|
||
TreeTraversal.preorder(root, n -> { | ||
if (n.getX() < minX.get()) { | ||
minX.set(n.getX()); | ||
} | ||
|
||
if (n.getY() < minY.get()) { | ||
minY.set(n.getY()); | ||
} | ||
|
||
double x2 = n.getX() + n.getWidth(); | ||
if (x2 > maxX.get()) { | ||
maxX.set(x2); | ||
} | ||
|
||
double y2 = n.getY() + n.getHeight(); | ||
if (y2 > maxY.get()) { | ||
maxY.set(y2); | ||
} | ||
}); | ||
|
||
double width = (maxX.get() - minX.get()); | ||
double height = (maxY.get() - minY.get()); | ||
|
||
Dimension dim = new Dimension(); | ||
dim.setSize(width, height); | ||
|
||
return dim; | ||
} | ||
|
||
public static <T> Dimension2D calculateMaxNodeDimension(Node<T> root) { | ||
final AtomicReference<Double> width = new AtomicReference<>(Double.MIN_VALUE); | ||
final AtomicReference<Double> height = new AtomicReference<>(Double.MIN_VALUE); | ||
|
||
TreeTraversal.preorder(root, n -> { | ||
if (width.get() < n.getWidth()) { | ||
width.set(n.getWidth()); | ||
} | ||
|
||
if (height.get() < n.getHeight()) { | ||
height.set(n.getHeight()); | ||
} | ||
}); | ||
|
||
Dimension dim = new Dimension(); | ||
dim.setSize(width.get(), height.get()); | ||
|
||
return dim; | ||
} | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/org/behrang/algorithm/tree/TreeTraversal.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package org.behrang.algorithm.tree; | ||
|
||
import org.behrang.algorithm.tree.Node; | ||
|
||
import java.util.function.Consumer; | ||
|
||
public class TreeTraversal { | ||
public static <T> void preorder(Node<T> root, Consumer<Node<T>> consumer) { | ||
consumer.accept(root); | ||
|
||
root.getChildren().forEach(ch -> { | ||
preorder(ch, consumer); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
src/main/java/org/behrang/algorithm/tree/demo/DemoFrame.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package org.behrang.algorithm.tree.demo; | ||
|
||
import javax.swing.*; | ||
import java.awt.*; | ||
|
||
public class DemoFrame extends JFrame { | ||
|
||
private JPanel mainPanel; | ||
|
||
public DemoFrame() { | ||
mainPanel = new TreePanel<>(SampleTree.newUniformInstance(64, 64)); | ||
add(mainPanel, BorderLayout.CENTER); | ||
} | ||
|
||
public static void main(String[] args) { | ||
DemoFrame demoFrame = new DemoFrame(); | ||
demoFrame.setTitle("Walker Algorithm Demo"); | ||
demoFrame.setSize(1024, 768); | ||
demoFrame.setResizable(false); | ||
demoFrame.setLocationByPlatform(true); | ||
demoFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
demoFrame.setVisible(true); | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
src/main/java/org/behrang/algorithm/tree/demo/SampleTree.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package org.behrang.algorithm.tree.demo; | ||
|
||
import org.behrang.algorithm.tree.Node; | ||
|
||
public final class SampleTree { | ||
|
||
private SampleTree() { | ||
} | ||
|
||
public static Node<String> newUniformInstance() { | ||
return newUniformInstance(2, 2); | ||
} | ||
|
||
public static Node<String> newUniformInstance(double nodeWidth, double nodeHeight) { | ||
Node<String> a, b, c, d, e, f, g, h, i, j, k, l, m, n, o; | ||
|
||
o = node("O", nodeWidth, nodeHeight); | ||
e = node("E", nodeWidth, nodeHeight); | ||
f = node("F", nodeWidth, nodeHeight); | ||
n = node("N", nodeWidth, nodeHeight); | ||
link(o, e, f, n); | ||
|
||
a = node("A", nodeWidth, nodeHeight); | ||
d = node("D", nodeWidth, nodeHeight); | ||
link(e, a, d); | ||
|
||
b = node("B", nodeWidth, nodeHeight); | ||
c = node("C", nodeWidth, nodeHeight); | ||
link(d, b, c); | ||
|
||
g = node("G", nodeWidth, nodeHeight); | ||
m = node("M", nodeWidth, nodeHeight); | ||
link(n, g, m); | ||
|
||
h = node("H", nodeWidth, nodeHeight); | ||
i = node("I", nodeWidth, nodeHeight); | ||
j = node("J", nodeWidth, nodeHeight); | ||
k = node("K", nodeWidth, nodeHeight); | ||
l = node("L", nodeWidth, nodeHeight); | ||
link(m, h, i, j, k, l); | ||
|
||
return o; | ||
} | ||
|
||
static Node<String> node(String value, double nodeWidth, double nodeHeight) { | ||
Node<String> node = new Node<>(value); | ||
node.setWidth(nodeWidth); | ||
node.setHeight(nodeHeight); | ||
|
||
return node; | ||
} | ||
|
||
@SafeVarargs | ||
static void link(Node<String> parent, Node<String>... children) { | ||
for (var child : children) { | ||
parent.getChildren().add(child); | ||
child.setParent(parent); | ||
} | ||
} | ||
} |
91 changes: 91 additions & 0 deletions
91
src/main/java/org/behrang/algorithm/tree/demo/TreePanel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package org.behrang.algorithm.tree.demo; | ||
|
||
import org.behrang.algorithm.tree.Node; | ||
import org.behrang.algorithm.tree.TreeTraversal; | ||
import org.behrang.algorithm.tree.WalkerAlgorithm; | ||
|
||
import javax.swing.*; | ||
import javax.swing.border.EmptyBorder; | ||
import java.awt.*; | ||
import java.awt.geom.Line2D; | ||
import java.awt.geom.Rectangle2D; | ||
|
||
import static org.behrang.algorithm.tree.DimensionCalculator.calculateMaxNodeDimension; | ||
|
||
public class TreePanel<T> extends JPanel { | ||
|
||
private Node<T> root; | ||
|
||
public TreePanel(Node<T> root) { | ||
setLayout(null); | ||
setRoot(root); | ||
} | ||
|
||
public void setRoot(Node<T> root) { | ||
this.root = root; | ||
repaint(); | ||
} | ||
|
||
@Override | ||
protected void paintComponent(Graphics g) { | ||
super.paintComponent(g); | ||
if (root == null) { | ||
return; | ||
} | ||
|
||
var dim = calculateMaxNodeDimension(root); | ||
|
||
var walker = new WalkerAlgorithm<T>( | ||
dim.getWidth(), | ||
dim.getWidth(), | ||
20, | ||
20, | ||
dim.getHeight() * 2 | ||
); | ||
|
||
walker.position(root); | ||
|
||
var g2 = (Graphics2D) g; | ||
TreeTraversal.preorder(root, n -> { | ||
drawNode(g2, n); | ||
}); | ||
} | ||
|
||
void drawNode(Graphics2D g, Node<T> node) { | ||
double labelMarginX = 12.0; | ||
double labelMarginY = 1.0; | ||
|
||
g.setColor(Color.BLACK); | ||
Rectangle2D rect = new Rectangle2D.Double( | ||
node.getX(), | ||
node.getY(), | ||
node.getWidth(), | ||
node.getHeight() | ||
); | ||
g.draw(rect); | ||
|
||
String label = node.getValue().toString(); | ||
|
||
g.setColor(Color.DARK_GRAY); | ||
Font font = new Font(Font.SERIF, Font.ITALIC, 24); | ||
g.setFont(font); | ||
|
||
FontMetrics fontMetrics = g.getFontMetrics(font); | ||
Rectangle2D labelBounds = fontMetrics.getStringBounds(label, g); | ||
|
||
g.drawString(label, | ||
(float) (node.getX() + labelMarginX), | ||
(float) (node.getY() + labelMarginY + labelBounds.getHeight()) | ||
); | ||
|
||
if (node.hasParent()) { | ||
Node<T> parent = node.getParent(); | ||
double x0 = parent.getX() + parent.getWidth() / 2; | ||
double y0 = parent.getY() + parent.getHeight(); | ||
double x1 = node.getX() + node.getWidth() / 2; | ||
double y1 = node.getY(); | ||
Line2D line2D = new Line2D.Double(x0, y0, x1, y1); | ||
g.draw(line2D); | ||
} | ||
} | ||
} |
Oops, something went wrong.