The Artima Developer Community
Sponsored Link

Java Answers Forum
Drawing a Tree

5 replies on 1 page. Most recent reply: May 16, 2006 1:45 PM by shizukesa ...

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 5 replies on 1 page
shizukesa ...

Posts: 3
Nickname: shizukesa
Registered: Mar, 2006

Drawing a Tree Posted: May 13, 2006 2:37 PM
Reply to this message Reply
Advertisement
I have to write a program that draws a binary tree but I'm stuck. What I did so far is work out the coordinates for every node in the tree. Is there any way I can use the coordinates to draw the tree?
I'm too tired to think anymore.


James Watson

Posts: 2024
Nickname: watson
Registered: Sep, 2005

Re: Drawing a Tree Posted: May 15, 2006 6:53 AM
Reply to this message Reply
> I have to write a program that draws a binary tree but I'm
> stuck. What I did so far is work out the coordinates for
> every node in the tree. Is there any way I can use the
> coordinates to draw the tree?

Yes. Figuring out the coordnites is the hard part. Now just use a Swing Canvas (assuming you are using Swing) to draw circles at each of these coordinates and lines connecting them. Hint, circles and lines are already implemented in Swing.

If you post some code that you are struggling with, I will point out problems and suggest improvements.

shizukesa ...

Posts: 3
Nickname: shizukesa
Registered: Mar, 2006

Re: Drawing a Tree Posted: May 15, 2006 11:48 AM
Reply to this message Reply
public void drawTree(Graphics2D g){
for (int i=A_SIZE-1;i>=0;i--){//Loops through array that contains an object with the coordinates of each leave in the tree
Rectangle r = new Rectangle(coordinates.getX()*50,coordinates.getY()*50,55,20);
g.draw(r);
g.drawString((String)coordinates.getPos().element(), r.x + 4, r.y + 15);

g.drawLine( r.x-10, r.y+50, r.x + r.width/2 , r.y+20);
g.drawLine( r.x+70, r.y+50, r.x + r.width/2, r.y+20);
}
}

Thank you for your help, this is is what I managed to do so far, I used rectangles for my nodes, but I find it difficult to connect the lines to the rectangles, cuz it seems that they have to get shorter when there is more nodes. Also I dont want lines coming from external nodes. I know this is basic stuff but I'm through thinking, have too much else on my mind.
I appreciate your help
(PS: The coordinates I use for the lines is just temporary, I'm still trying to figure out how I'd write it so that it adjust as the nodes get more)

--shiz

Bob Dobalina

Posts: 16
Nickname: hiredgoon
Registered: Apr, 2005

Re: Drawing a Tree Posted: May 15, 2006 7:48 PM
Reply to this message Reply
Use recursion.

James Watson

Posts: 2024
Nickname: watson
Registered: Sep, 2005

Re: Drawing a Tree Posted: May 16, 2006 6:38 AM
Reply to this message Reply
I think part of your problem is that you need to walk the tree in order to draw the lines properly.

If you draw a line from the parent to each of it's children, you should be able to calculate the length of the line based on the positions of the parent and child.

Also when you post your code, use the tags as shown on the right of the edit area. There is a java tag.

shizukesa ...

Posts: 3
Nickname: shizukesa
Registered: Mar, 2006

Re: Drawing a Tree Posted: May 16, 2006 1:45 PM
Reply to this message Reply
> I think part of your problem is that you need to walk the
> tree in order to draw the lines properly.
>
> If you draw a line from the parent to each of it's
> children, you should be able to calculate the length of
> the line based on the positions of the parent and child.
>
> Also when you post your code, use the tags as shown on the
> right of the edit area. There is a java tag.

thank you for replying on my post, I got the drawing thing right.

Flat View: This topic has 5 replies on 1 page
Topic: enum best practices? Previous Topic   Next Topic Topic: Iterface method name?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use