我试图使用awt和swing来绘制二叉树。我可以显示树的节点,但不能绘制从父节点到子节点的线。我使用以下类:Node(代表节点),treeGUI,DrawTree和Main。
这是没有行的输出。

java - 用树的图形表示画线-LMLPHP

我的意图是显示从父节点到子节点的行。我尝试使用drawLine类中的Graphics metohd,这是输出:

java - 用树的图形表示画线-LMLPHP

方法drawTree定义节点值在屏幕中的位置,并将该节点的位置存储在ArrayLists中。 drawLine方法绘制线条。我认为这些行是这样的,因为这些值按此特定顺序存储在ArrayList中。我尝试了各种方法以正确的方式画线,但都没有成功。我如何画出父母与孩子的界线?

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);
}

07-24 22:17