gpt4 book ai didi

java - 在树的图形表示中画线

转载 作者:行者123 更新时间:2023-12-01 16:52:22 25 4
gpt4 key购买 nike

我正在尝试使用 awt 和 swing 绘制二叉树。我可以显示树的节点,但无法绘制从父节点到子节点的线。我使用以下类:Node(代表节点)、treeGUI、DrawTree 和 Main。这是不带行的输出。

Árvore binária

我的目的是显示从父节点到子节点的线。我尝试使用 Graphics 类中的 drawLine 方法,这是输出:

Arvore

drawTree 方法定义节点值在屏幕中的位置,并将节点的位置存储在 ArrayList 中。 drawLine 方法绘制线条。我认为这些行是这样的,因为这些值按照特定的顺序存储在 ArrayList 中。我尝试了各种方法以正确的方式绘制线条,但均不成功。如何绘制从 parent 到 child 的界限?

public class TreeGUI extends JFrame {

private JPanel contentPane;
public Node node;
public DrawTree drawer;

/**
* Create the frame.
*/
public TreeGUI(Node node) {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 500, 500);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
drawer = new DrawTree(node);
contentPane.add(drawer);
setContentPane(contentPane);
this.node = node;
setVisible(true);
}

}

class DrawTree extends JPanel{

public Node node;
public static ArrayList listX = new ArrayList();
public static ArrayList listY = new ArrayList();


public DrawTree(Node node){
this.node = node;
}

@Override
protected void paintComponent(Graphics g) {
// TODO Auto-generated method stub
g.setFont(new Font("Tahoma", Font.BOLD, 20));
DrawTree(g, 0, getWidth(), 0, getHeight() / node.getheight(node), node);
listX.clear();
listY.clear();
}

public void DrawTree(Graphics g, int StartWidth, int EndWidth, int StartHeight, int Level, Node node) {
String data = String.valueOf(node.getValue());
g.setFont(new Font("Tahoma", Font.BOLD, 20));
FontMetrics fm = g.getFontMetrics();
int dataWidth = fm.stringWidth(data);

g.drawString(data, (StartWidth + EndWidth) / 2 - dataWidth / 2, StartHeight + Level / 2);
listX.add((StartWidth + EndWidth) / 2 - dataWidth / 2);
listY.add(StartHeight + Level / 2);
drawLine(g, node);

if (node.getLeft() != null) {
DrawTree(g, StartWidth, (StartWidth + EndWidth) / 2, StartHeight + Level, Level, node.getLeft());
}
if (node.getRight() != null)
DrawTree(g, (StartWidth + EndWidth) / 2, EndWidth, StartHeight + Level, Level, node.getRight());
}

public void drawLine(Graphics g, Node node){
for (int i=1; i < listY.size(); i++)
g.drawLine((int)listX.get(i-1), (int)listY.get(i-1), (int)listX.get(i), (int)listY.get(i));
}

}

主要方法

public static void main(String[] args) {
Node raiz = null;
raiz = raiz.insert(raiz, 35);
raiz.insert(raiz, 25);
raiz.insert(raiz, 75);
raiz.insert(raiz, 30);
raiz.insert(raiz, 20);
raiz.insert(raiz, 12);
raiz.insert(raiz, 6);
raiz.insert(raiz, 23);
raiz.insert(raiz, 90);
TreeGUI gui = new TreeGUI(raiz);
}

最佳答案

您可以让 DrawTree 函数返回其打印文本的位置。然后让父级从其当前位置到子级的 DrawTree 函数返回的位置绘制一条线。这将允许您摆脱列表。

public Point DrawTree(Graphics g, int StartWidth, int EndWidth, int StartHeight, int Level, Node node)
{
String data = String.valueOf(node.getValue());
g.setFont(new Font("Tahoma", Font.BOLD, 20));
FontMetrics fm = g.getFontMetrics();
int dataWidth = fm.stringWidth(data);

// Calculate position to draw text string
Point textPos = new Point((StartWidth + EndWidth) / 2 - dataWidth / 2, StartHeight + Level / 2);
g.drawString(data, textPos.x, textPos.y);

if (node.getLeft() != null) {
Point child1 = DrawTree(g, StartWidth, (StartWidth + EndWidth) / 2, StartHeight + Level, Level, node.getLeft());
// Draw line from this node to child node
drawLine(g, textPos, child1);
}
if (node.getRight() != null) {
Point child2 = DrawTree(g, (StartWidth + EndWidth) / 2, EndWidth, StartHeight + Level, Level, node.getRight());
// Draw line from this node to child node
drawLine(g, textPos, child2);
}
// Return position for parent to use
return textPos;
}

public void drawLine(Graphics g, Point p1, Point p2)
{
g.drawLine(p1.x, p1.y, p2.x, p2.y);
}

关于java - 在树的图形表示中画线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37490664/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com