在树的图形表示中绘制线条

时间:2016-05-27 19:05:44

标签: java swing data-structures awt binary-tree

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

Árvore binária

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

Arvore

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

}

métodomain

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

1 个答案:

答案 0 :(得分:5)

您可以让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);
}