Skip to content

Commit

Permalink
Housekeeping
Browse files Browse the repository at this point in the history
  • Loading branch information
behrangsa committed Oct 23, 2023
1 parent d0c64f6 commit 90529ba
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 53 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.behrang.algorithm.tree;

import java.awt.*;
import java.awt.Dimension;
import java.awt.geom.Dimension2D;
import java.util.concurrent.atomic.AtomicReference;

Expand All @@ -11,7 +11,7 @@ public static <T> Dimension2D calculateTreeDimension(Node<T> root) {
final AtomicReference<Float> maxX = new AtomicReference<>(Float.MIN_VALUE);
final AtomicReference<Float> maxY = new AtomicReference<>(Float.MIN_VALUE);

TreeTraversal.preorder(root, n -> {
Functions.preorder(root, n -> {
if (n.getX() < minX.get()) {
minX.set(n.getX());
}
Expand Down Expand Up @@ -44,7 +44,7 @@ public static <T> Dimension2D calculateMaxNodeDimension(Node<T> root) {
final AtomicReference<Float> width = new AtomicReference<>(Float.MIN_VALUE);
final AtomicReference<Float> height = new AtomicReference<>(Float.MIN_VALUE);

TreeTraversal.preorder(root, n -> {
Functions.preorder(root, n -> {
if (width.get() < n.getWidth()) {
width.set(n.getWidth());
}
Expand Down
10 changes: 4 additions & 6 deletions src/main/java/org/behrang/algorithm/tree/Functions.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,11 @@ private static Node<?> leftmostDescendantAtDepth(Node<?> node, int level, int de
}
}

public static <T> void prePostOrder(Node<T> node, Consumer<Node<T>> preConsumer, Consumer<Node<T>> postConsumer) {
preConsumer.accept(node);
public static <T> void preorder(Node<T> root, Consumer<Node<T>> consumer) {
consumer.accept(root);

node.getChildren().forEach(n -> {
prePostOrder(n, preConsumer, postConsumer);
root.getChildren().forEach(ch -> {
preorder(ch, consumer);
});

postConsumer.accept(node);
}
}
15 changes: 0 additions & 15 deletions src/main/java/org/behrang/algorithm/tree/TreeTraversal.java

This file was deleted.

9 changes: 6 additions & 3 deletions src/main/java/org/behrang/algorithm/tree/demo/DemoFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

import org.behrang.algorithm.tree.Node;

import javax.swing.*;
import java.awt.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;

import static org.behrang.algorithm.tree.data.SampleTreeGenerator.newMultiformInstance;

Expand All @@ -17,7 +18,9 @@ public DemoFrame(Node<String> root) {
}

public static void main(String[] args) {
var demoFrame = new DemoFrame(newMultiformInstance(40, 80, 40, 80));
var root = newMultiformInstance(40, 80, 40, 80);
var demoFrame = new DemoFrame(root);

demoFrame.setTitle("Walker Algorithm Demo");
demoFrame.setSize(1280, 1024);
demoFrame.setResizable(false);
Expand Down
59 changes: 33 additions & 26 deletions src/main/java/org/behrang/algorithm/tree/demo/TreePanel.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,42 @@
package org.behrang.algorithm.tree.demo;

import org.behrang.algorithm.tree.Functions;
import org.behrang.algorithm.tree.Node;
import org.behrang.algorithm.tree.TreeTraversal;
import org.behrang.algorithm.tree.WalkerAlgorithm;

import javax.swing.*;
import java.awt.*;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;

import static java.awt.RenderingHints.KEY_ANTIALIASING;
import static java.awt.RenderingHints.KEY_TEXT_ANTIALIASING;
import static java.awt.RenderingHints.VALUE_ANTIALIAS_ON;
import static java.awt.RenderingHints.VALUE_TEXT_ANTIALIAS_GASP;
import static org.behrang.algorithm.tree.DimensionCalculator.calculateMaxNodeDimension;

public class TreePanel<T> extends JPanel {

private static final float PADDING = 5.0f;

private Node<T> root;

private WalkerAlgorithm<T> walker;

public TreePanel(Node<T> root) {
setLayout(null);
setRoot(root);

initWalker();
}

public void setRoot(Node<T> root) {
this.root = root;
initWalker();
repaint();
}

Expand All @@ -32,36 +48,29 @@ protected void paintComponent(Graphics g) {
}

Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON
);
g2.setRenderingHint(KEY_ANTIALIASING, VALUE_ANTIALIAS_ON);
g2.setRenderingHint(KEY_TEXT_ANTIALIASING, VALUE_TEXT_ANTIALIAS_GASP);

g2.setRenderingHint(
RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_GASP
);
walker.position(root);

Functions.preorder(root, n -> {
drawNode(g2, n);
});
}

void initWalker() {
var dim = calculateMaxNodeDimension(root);

var walker = new WalkerAlgorithm<T>(
walker = new WalkerAlgorithm<>(
(float) dim.getWidth(),
(float) dim.getWidth(),
20,
20,
(float) (dim.getHeight() * 3)
);

walker.position(root);
TreeTraversal.preorder(root, n -> {
drawNode(g2, n);
});
}

void drawNode(Graphics2D g, Node<T> node) {
float labelMarginX = 12.0f;
float labelMarginY = 1.0f;

g.setColor(Color.BLACK);
Rectangle2D rect = new Rectangle2D.Double(
node.getX(),
Expand All @@ -74,16 +83,14 @@ void drawNode(Graphics2D g, Node<T> node) {
String label = node.getValue().toString();

g.setColor(Color.DARK_GRAY);
Font font = new Font(Font.SERIF, Font.ITALIC, 24);
Font font = new Font(Font.SERIF, Font.PLAIN, 24);
g.setFont(font);

FontMetrics fontMetrics = g.getFontMetrics(font);
Rectangle2D labelBounds = fontMetrics.getStringBounds(label, g);
float x = node.getX() + PADDING;
float y = node.getY() + fontMetrics.getAscent();

g.drawString(label,
(float) (node.getX() + labelMarginX),
(float) (node.getY() + labelMarginY + labelBounds.getHeight())
);
g.drawString(label, x, y);

if (node.hasParent()) {
Node<T> parent = node.getParent();
Expand Down

0 comments on commit 90529ba

Please sign in to comment.