11 Replies Latest reply on Oct 16, 2006 12:16 PM by 807607

    binary search tree (dictionary) applet... best way to draw nodes?

      Hey, 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));

      // method called to get the latest tree and repaint it
      public void setTree()
           list = bst.returnTree();

      // draw the tree
      public void paintComponent (Graphics page)
      super.paintComponent (page);

      setPreferredSize(new Dimension(WIDTH + 200, HEIGHT + 100));

      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.
        • 1. Re: binary search tree (dictionary) applet... best way to draw nodes?
          Any reason why you shouldn't use JTree? It's not the easiest of the swing compound gadgets to set up - but it's a lot easier than writing your own.
          • 2. Re: binary search tree (dictionary) applet... best way to draw nodes?
            we hafta write our own code for pretty much everything...
            • 3. Re: binary search tree (dictionary) applet... best way to draw nodes?
              we hafta write our own code for pretty much
              I don't see you implementing an ArrayList.
              • 4. Re: binary search tree (dictionary) applet... best way to draw nodes?
                I am implementing an arrayList.
                ArrayList list;
                list = bst.returnTree();

                bst is my instance of BinarySearchTree, and returnTree() is a method in class BinarySearchTree that returns an arrayList of the nodes in a levelOrder traversal...
                • 5. Re: binary search tree (dictionary) applet... best way to draw nodes?
                  When you tell us you have to work within arbitary restrictions on which library classes you can, or cannot use it's not very easy to tell you how to proceed without knowing those rules.

                  What makes JLabel available and JTree not, for example?
                  • 6. Re: binary search tree (dictionary) applet... best way to draw nodes?
                    OK, so heres what we needed to do:

                    interface BST
                    -methods to be implemented by class BinarySearchTree
                    class DictionaryEntry
                    -holds values for the word, and its definition
                    class BinaryTreeNode
                    -holds the information from DictionaryEntry for each node
                    class BinarySearchTree
                    -creates the tree, and handles all operations
                    class Dictionary
                    -main class, extends JApplet
                    -has a nested class TextPanel
                    -JTextfields for entering word and definition
                    -has a nested class ButtonPanel
                    -buttons for:
                    DisplayTree(shows the drawn tree)
                    -has nested class Canvas
                    -look above to see whats in that class

                    So basically, I don't really know what the JTree class does, but I'm not sure if that'd be the best way for me to go about this. Everything we do is completely from scratch. We had to code all the operations in BinarySearchTree, and we have to code the drawing of our picture from scratch as well. I'm just confused as to how I actually DRAW the nodes on the canvas, preferably using JLabel's or even Buttons.

                    The other problem I'm thinking about now is how to calculate the position of the nodes on the canvas.
                    I want the nodes to rectangles of width 60px, and height 30px, with the word in the middle.
                    The root node I want painted at ((WIDTH/2)-30, 20), so it would be in the middle of the canvas 20 pixels from the top. I want the next row to start at 80px below the bottom of the node above, which would be at y=130. Now I want this level to have its left node in the middle of the left half of the canvas, and the right node in the middle of the right half.
                    so root.left would be painted at ((WIDTH/4)-30,130)
                    and root.right would be painted at ((3WIDTH/4)-30,130)
                    i want to continue this pattern so that the next row will have a y value of 240 while the first node in the row is in the middle of the first quarter of the canvas, the 2nd in the 2nd quarter, and so on....
                    Any ideas for how to calculate these positions without explicitly defining the positions for ever possible node?

                    *Also, I think I might add null nodes where necesarry to have a full complete tree                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
                    • 7. Re: binary search tree (dictionary) applet... best way to draw nodes?
                      and i want to thank you guys for taking the time to help me, i really appreciate it... im a CompSci major at Villanova, and I've had to learn a lot in a short period of time. I didn't have any programming background, but I have been doing art all my life, so I've been able to use my creativity to my benefit in programs. However, that doesnt always help and sometimes I get really stuck with coding.
                      • 8. Re: binary search tree (dictionary) applet... best way to draw nodes?
                        I don't really know what the JTree class
                        It displays trees, like Windows explorer does but with whatever nodes you want.
                        but I'm not sure if that'd be the best way for
                        me to go about this. Everything we do is completely
                        from scratch.
                        Nothing you do in Java is "Completely from scratch". You just can't avoid using library classes.

                        As an excercise you might be asked to "reinvent the wheel", by writing your own code for the functions of one library class or another, but being required to, for example, write your own binary tree doesn't mean you can't use standard Swing gadgets to display it.

                        Actually, learning your way arround the library is probably 90% of what you need to be proficient in Java.
                        • 9. Re: binary search tree (dictionary) applet... best way to draw nodes?
                          I was looking at the Java Sun API online for JTree, and got kind of confused.

                          it says a JTree is:
                          "A control that displays a set of hierarchical data as an outline."

                          pretty much all of the examples ive found are for other kinds of trees rather than Binary Search Tree's...

                          do you know how to paint a JLabel on to a Graphics page?
                          • 10. Re: binary search tree (dictionary) applet... best way to draw nodes?
                            A binary search tree is a kind of tree, and you could display it with JTree, but you need to either make your nodes implement TreeNode, or have a TreeNode associated with them.

                            In generally you use JLabels in a container, which will contain them amongst other swing components (including JTrees). If you are doing your own drawing you are more likely to use the text drawing methods of the graphics context. It is possible, to call the paint method of JLabel (which isn't a child) from the paint method of another object, for example that's how JTables draw their cells. Set the bounds first.

                            In the case of a tree display there would be lines, arrows etc. to draw as well as text.
                            • 11. Re: binary search tree (dictionary) applet... best way to draw nodes?
                              i got a method to work in drawing the nodes on the canvas..
                                       public void MakeNode(BinaryTreeNode t, Graphics g)
                                          int x = 10;
                                          int y = 10;
                                          JLabel node;
                                          node = new JLabel();
                                          node.setSize(new Dimension(60,30));
                                          String word;
                                          word = t.getItem().toString();
                                            g.drawRect(x+2, y+2, 56,26);
                                          g.drawString(word, x+5, y+20);
                              and then my paintComponent method looks like the following:
                               public void paintComponent (Graphics page) 
                                          String one = "What";
                                          super.paintComponent (page);
                                                  Comparable j = (Comparable) one;
                                          BinaryTreeNode n = new BinaryTreeNode(j);
                                          setPreferredSize(new Dimension(WIDTH, HEIGHT));
                              the only problem im facing now is figuring out how to figure out the x and y positions as i get down the tree.. im not really worrying about drawing the lines between nodes yet. how do you think i should go about finding the positions?