binary search tree (dictionary) applet... best way to draw nodes?
807607Oct 16 2006 — edited Oct 16 2006Hey, I'm working on an applet where we maintain a dictionary using a Binary Search Tree, and each node holds the word and definition. In my applet class, theres a nested class Canvas that takes BinarySearchTree bst as a parameter and will draw the entire search tree as the user adds or removes words..
Anyways it looks like this:
class Canvas extends JPanel
{
ArrayList list;
Iterator listIterator;
int WIDTH = 600;
int HEIGHT = 400;
public Canvas(BinarySearchTree b)
{
bst = b;
list = new ArrayList();
setSize(new Dimension(WIDTH,HEIGHT));
setBackground(Color.red);
setBorder(null);
}
// method called to get the latest tree and repaint it
public void setTree()
{
list = bst.returnTree();
repaint();
}
// draw the tree
public void paintComponent (Graphics page)
{
super.paintComponent (page);
setPreferredSize(new Dimension(WIDTH + 200, HEIGHT + 100));
revalidate();
}
}
I kind of wanted to make each node a JLabel and then paint the JLabels onto the canvas. That way I figured it'd be a lot easier to add listeners so when you click on a node, a separate component will display the word and its definition. Any ideas on how I could implement this? Would I have to write a method that takes all the information needed and returns a JLabel with that info? And if so, would that method also be in the nested Canvas class, or in the BinaryTreeNode class? Any help would be greatly appreciated. I still have another week and a half, but I wanna make it as perfect as possible, so I can focus on the graphics more the last few days. Thanks a lot.
-Colin