尝试延迟Java中树节点之间的绘制过程

问题描述

我正在尝试使用秋千绘制一棵树:

这是执行循环的功能

public void AfisareDSF(Nod n,Graphics g) throws InterruptedException
{
    if(this.root.equals(n))
    {
        n.DeseneazaNod(g);
    }
    for(int i=0;i<n.children.size();i++)
    {
        n.children.get(i).DeseneazaNod(g);
        n.children.get(i).DeseneazaLegaturaCuParinte(g);
        this.AfisareDSF(n.children.get(i),g);
    }
}

这是节点绘制功能

public void DeseneazaNod(Graphics g)
    {
        g.drawRect(nodGrafic.OriginX,nodGrafic.OriginY,nodGrafic.SquareSize,nodGrafic.SquareSize);
        g.drawString(info,nodGrafic.LetterX,nodGrafic.LetterY);
    }

我尝试使用TimeUnit.MILLISECONDS.sleep来停止该过程,但没有任何效果。只是之间的延迟 树的开始和即时绘制。

我尝试使用计时器功能,我阅读了文档,但无法弄清楚。

这也是GUI类:

public class DFS_GUI extends JPanel{    
public static int Width = 700;
public static int Height = 500;
public static Tree arbore;

public static void CreareNodGrafic(int x,int y,Nod n)
{
    n.CreareNodGrafic(x,y);
    y=y+50;
    if (n.children.size() % 2 == 0)
    {
        int latSize = (int)((float)((n.size+1) / 2.0f) * (10*DFS_GUI.arbore.ramificatie)/2);
         x = x - latSize;
        for(int i=0;i<n.children.size();i++)
        {
            DFS_GUI.CreareNodGrafic(x,y+40,n.children.get(i));
            x=x+n.size*(10*DFS_GUI.arbore.ramificatie/2);
            }
        
    }
    else
    {
        int latSize = (n.size)* (int)(10*DFS_GUI.arbore.ramificatie/2.2f);
        if(n.children.size()!=1)
         x = x - latSize;
        for(int i=0;i<n.children.size();i++)
        {
            DFS_GUI.CreareNodGrafic(x,n.children.get(i));
            
            x=x+n.size*(int)(10*DFS_GUI.arbore.ramificatie/2.2f);
            }
        
    }
}
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.setFont(new Font("TimesRoman",Font.PLAIN,20));
    try {
        arbore.AfisareDSF(arbore.root,g);
    } catch (InterruptedException e) {
        // Todo Auto-generated catch block
        e.printstacktrace();
    }
         
    
}

public static void Start(Tree startTree) {
    JFrame jFrame = new JFrame();
    jFrame.add(new DFS_GUI());
    jFrame.setSize(DFS_GUI.Width,DFS_GUI.Height);
    jFrame.setVisible(true);
    jFrame.setDefaultCloSEOperation(JFrame.disPOSE_ON_CLOSE);
    DFS_GUI.arbore = startTree;
    int X = DFS_GUI.Width / 2;
    int Y = DFS_GUI.Height / 10;
    DFS_GUI.CreareNodGrafic(X,Y,DFS_GUI.arbore.root);

} }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)