This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
Displaying a binary tree in graphic form in an applet
Rick Bass
Greenhorn
Joined: Mar 30, 2007
Posts: 1
posted
0
I have a binary tree and I need to display it in an applet. I was going to drawString for the root and then for the next line I was going to drawLine going left, skip some space, and draw a line to the right. Then on the next line I drawString for each child of the root. and so on so forth. But if the child is null...no line or string is drawn. I'm thinking I got to do a breadth first traversal, put each name in a queue, dequeue it and draw the necessary things. However, I can't get anywhere with drawing this tree.
Please help.
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35249
7
posted
0
Welcome to JavaRanch.
Since there seems to be nothing applet-specific in the question, I'll move it to the AWT/Swing/GUI forum. [ March 30, 2007: Message edited by: Ulf Dittmer ]
You are drawing a tree; you are presumably aware of the Swing classes used to draw a tree? Have a look in the Java tutorials, here, and see whether that is any help.
Craig Wood
Ranch Hand
Joined: Jan 14, 2004
Posts: 1535
posted
0
There are some GraphLayout examples in the demo folder of your sdk that show one way of showing a tree graph (sdk1.5/demo/plugin/applets/GraphLayout). There may be a number of ways of setting this up. One way I did this was to use the tree nodes to make up a set of graphNodes to be used for drawing. Make up a new set of graphNodes for each addition/removal of a node in your tree. Each of these graphNodes can keep an x, y location for itself and for each child. Use these to draw the graphNode and a line to each child as you move through the graphNodes. Use your binary tree methods to help figure out the location of each graphNode relative to its parent node on up to the tree root node. Then you can run through the graphNodes and calculate the width and height of the tree graph and use this to locate/center (and draw) the tree in your gui graphic component and to set the scrollbars on its parent JScrollPane.