依次在JPanel上绘制BinaryTree的节点

时间:2018-12-25 08:56:22

标签: java jpanel

Click to see my visual frame

我不希望同时绘制所有节点。我如何依次绘制它们及其链接(不要清除旧的节点和链接),直到所有节点和链接都位于JPanel上。我遍历树中的节点并将其存储到名为“ tab”的数组中,现在我只处理该数组“ tab”。

我试图将Thread.sleep放入paintcomponent()中,但是它没有按预期工作。

请问有人可以帮助我吗?

这是我的代码:

package com.phamkhanh.visual;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import com.phamkhanh.core.BinarySearchTree;
import com.phamkhanh.core.Node;

public class VisualBinarySearchTree extends BinarySearchTree{

    /*---- Fields ----*/

    Node[] tab;
    static int i = 0;
    JFrame fenetre = null;
    JPanel pan1 = null;


    /*---- Constructors ----*/
    public VisualBinarySearchTree()
    {
        this.tab = null;
        this.fenetre = new JFrame();

        this.fenetre.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.fenetre.setTitle("Binary Search Tree");
        this.fenetre.setSize(new Dimension(1500, 600));
        this.fenetre.setResizable(true);


    }



    public void drawTree()
    {
        this.tab = new Node[numberOfNodes];
        this.getReady(this.getRoot());
        DrawingPanel panel = new DrawingPanel(tab, numberOfNodes);
        panel.setLayout(new GridLayout(0, 1));
        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setViewportView(panel);

        scrollPane.getVerticalScrollBar().setMaximum(10000);
        scrollPane.getHorizontalScrollBar().setMaximum(10000);
        scrollPane.getHorizontalScrollBar().setValue(4200);

        scrollPane.getVerticalScrollBar().setUnitIncrement(30);
        scrollPane.getHorizontalScrollBar().setUnitIncrement(30);

        this.fenetre.add(scrollPane);
        this.fenetre.setVisible(true);
        this.fenetre.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                fenetre.dispose();
            }
        });

    }


    /*----Inner class DrawingTree----*/
    /* A class to draw the tree inside tree the JPanel*/
    @SuppressWarnings("serial")
    class DrawingPanel extends JPanel
    {

        Node[] copietab;
        int n = 0;

        DrawingPanel(Node[] nodes, int n)
        {
            copietab = nodes.clone();
            this.n = n;
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(new Dimension(10000, 10000));
        }

        @Override
        public void paintComponent(Graphics g)
        {

            BasicStroke ligt_stroke = new BasicStroke(2);
            BasicStroke dark_stroke = new BasicStroke(3);

            // cast to Graphics2D to use more functions
            Graphics2D g2 = (Graphics2D) g;

            super.paintComponent(g2);

            for(int i = 0; i < n; ++i)
            {
                // convert the value of a node to string
                String a = String.valueOf(copietab[i].getData());

                // Set stroke, color and then draw a circle at point(x, y)
                g2.setColor(Color.black);
                g2.setStroke(ligt_stroke);
                g2.drawOval(copietab[i].getX(), copietab[i].getY(), 40, 40);

                // Draw a string at point(x + 5, y + 25)
                g2.drawString(a, copietab[i].getX() + 5, copietab[i].getY() + 25);

                // set stroke for the line to be bold
                g2.setStroke(dark_stroke);

                // if the node has the right sub-node, set color for the the line and then draw it
                if(copietab[i].getRight() != null)
                {
                    g2.setColor(Color.red);
                    g2.drawLine(copietab[i].getX() + 20, copietab[i].getY() + 40, 
                            copietab[i].getRight().getX() + 20, copietab[i].getRight().getY());
                }

                // if the node has the left sub-node, set color for the the line and then draw it
                if(copietab[i].getLeft() != null)
                {
                    g2.setColor(Color.green);
                    g2.drawLine(copietab[i].getX() + 20, copietab[i].getY() + 40, 
                            copietab[i].getLeft().getX() + 20, copietab[i].getLeft().getY());
                }

            }

            //g2.dispose();

        }

    }

    /*----Utility function----*/
    // Traverse the tree and get all the Node and save it into the array of 
    //  nodes
    private void getReady(Node node)
    {

        tab[i] = node;
        ++i;
        if(node.getLeft() != null) 
            this.getReady(node.getLeft());
        if(node.getRight() != null) 
            this.getReady(node.getRight());
    }

}

0 个答案:

没有答案