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