# Thread: drawing in JPanel ,beyond the co-ordinates

1. Member
Join Date
Apr 2009
Posts
2
Rep Power
0

## 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 04:56 PM.

2. 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. Member
Join Date
Apr 2009
Posts
2
Rep Power
0
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. Member
Join Date
Feb 2008
Location
Oregon, USA
Posts
49
Rep Power
0
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 ( &#91;code] code goes here &#91;/code] ) and show any import statements you used.

5. 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.

#### Posting Permissions

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