0

I am trying to draw a Tree using swing:

This is the function that is doing the looping:

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

And this is the node draw function:

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

I tried using TimeUnit.MILLISECONDS.sleep to stop the process , but with no effect. just a delay between the start and an instant draw of the tree.

I tried using the timer functions ,i read the documentation , but i couldn't figure it out .

Also this is the GUI class :

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, y+40, 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);

} }

  • 1) Method names should NOT start with an upper case character. 2) Maybe: https://stackoverflow.com/questions/64196198/bubble-sort-animation will give you some ideas on how to implement animation. You would need to modify your code to implement on of the suggested approaches. – camickr Oct 16 '20 at 17:23
  • *"Trying to delay the painting process between tree nodes.."* .. Why? **General tips:** 1) For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) See [Detection/fix for the hanging close bracket of a code block](http://meta.stackexchange.com/q/251795/155831) for a problem I could no longer be bothered fixing. Please [edit] the question to add 4 space chars ahead of the closing `}` at the end of the code. – Andrew Thompson Oct 17 '20 at 00:25
  • See an [example](https://stackoverflow.com/a/50781879/3992939) of updating GUI (DFS visualization) using s SwingWorker. Here is another [example](https://stackoverflow.com/a/49158109/3992939) – c0der Oct 19 '20 at 08:13

0 Answers0