Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2009
    Posts
    2
    Rep Power
    0

    Default drawing in JPanel ,beyond the co-ordinates

    hey guys,i have been trying to make a binary tree GUI representation,I am facing a problem in the following code
    class GuiPanel extends JPanel{
    int x=200,y=10;
    Tree current_node=root;
    void print3(Tree present,int x,int y,Graphics g){
    int temp=present.GetKey();
    String tempp=Integer.toString(temp);
    g.drawString(tempp,x,y);
    if(present.GetHas_Left()){
    print3(present.GetLeft(),x-50,y+50,g);
    g.drawLine(x, y, x-50, y+50);
    }
    if(present.GetHas_Right()){
    print3(present.GetRight(),x+50,y+50,g);
    g.drawLine(x, y, x+50, y+50);
    }
    }

    after a few entries,the x-50 term goes in the negative and no painting is seen.How do I approach this problem
    Last edited by anandnarayan891; 04-04-2009 at 05:56 PM.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    after a few entries,the x-50 term goes in the negative and no painting is seen.How do I approach this problem
    Make up a method in which you traverse the tree, lay it out as you go and save the maxima/minima values, ie, minX, minY, maxX, maxY.
    Then you can use these to adjust the root in the view so that all of the graph will be visible.

  3. #3
    Join Date
    Apr 2009
    Posts
    2
    Rep Power
    0

    Default

    The user enters the value..So its a dynamic graph..Even if i Put the root at the best possible co-ordinates the same error will happen at a later stage right?

  4. #4
    Bluefox815 is offline Member
    Join Date
    Feb 2008
    Location
    Oregon, USA
    Posts
    49
    Rep Power
    0

    Default

    What is a Tree object? Is this a class you defined yourself? You should also format your code better, put it in a pair of code tags ( [code] code goes here [/code] ) and show any import statements you used.

  5. #5
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    The user enters the value..So its a dynamic graph..Even if i Put the root at the best possible co-ordinates the same error will happen at a later stage right?
    Not necessarily. Depends on how you put it together.
    When the user adds/deletes/moves a node recalculate your layout and repaint. The goal is to locate the root so as to center the tree. If the (required) tree size exceeds the view bounds then you'll want to keep it (the component) in a JScrollPane, set a preferredSize for the component that will allow all of the tree to be comfortably visible, and call revalidate on it (the component).
    The information for the preferredSize of the tree and the location of the root relative to this overall size is determined in the layout method you will build. Call this method every time there is a user edit or componentResized event.

Similar Threads

  1. Looking for help on drawing stuff in a jPanel
    By Gatts79 in forum AWT / Swing
    Replies: 3
    Last Post: 08-28-2009, 07:00 PM
  2. drawing to a JPanel
    By diggitydoggz in forum New To Java
    Replies: 11
    Last Post: 03-09-2009, 08:42 AM
  3. Drawing points on a JPanel
    By josephdcoleman in forum New To Java
    Replies: 6
    Last Post: 02-25-2009, 04:47 PM
  4. Adding JPanels to JFrames based on x-y co-ordinates
    By aneesahamedaa in forum AWT / Swing
    Replies: 4
    Last Post: 07-24-2008, 11:41 PM
  5. X&Y Coordinate Drawing on jPanel
    By BHCluster in forum Java 2D
    Replies: 2
    Last Post: 03-27-2008, 11:47 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •