# drawing in JPanel ,beyond the co-ordinates

• 04-04-2009, 04:53 PM
anandnarayan891
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
• 04-04-2009, 07:00 PM
hardwired
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.
• 04-04-2009, 07:58 PM
anandnarayan891
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?
• 04-04-2009, 08:46 PM
Bluefox815
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.
• 04-05-2009, 03:42 AM
hardwired
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.