non exixting graphics object

I am trying to render an n-ary graph on an ordinary JPanel. The value of all nodes are represented by a string, which is shown in the graph using a arbitrary font. My problem is that the location of all nodes depends on the value of the string (i.e. the length and height of the actual text). To calculate the this lengtg and height I need to use the Graphics.getFont() and Graphics.getFontRenderContext() methods. This implies that I have a Graphics object which I only know how to retrieve from the paint or paintComponent methods (since JComponent.getGraphics() only returns null until the first actual rendering of that component). Since I want to calculate the position of all nodes before I do any real rendering (i.e. before the first call to paint or paintComponent), how do I go about getting an instance of a Graphics-object?

[842 byte] By [avstandstuff] at [2007-9-30 19:05:32]
# 1

1. Have you tried doing that calculation in your paintComponent method? You could cache the results so that

only the first call to the method does the crunching.

2. Do you know the font? would calling Component's getFont give you the correct font? And as for FontRenderContext,

you can usually just construct one -- you should know whether you are using text antialiasing and

fractional metrics or not...

DrLaszloJamf at 2007-7-6 23:15:28 > top of Java-index,Java Essentials,Java Programming...
# 2

> 1. Have you tried doing that calculation in your paintComponent method? You could cache the results so > that only the first call to the method does the crunching.

>

Yes, but the graph is interactive so nodes can be opened and collapsed, which means that the calculations must be done again. It is possible to achieve a sollution this way by setting a flag of when to do the crunching or not. However, this solution becomes quite messy for me since there is a lot of interaction with other objects that depends on the position of all nodes...

> 2. Do you know the font? would calling Component's getFont give you the correct font? And as for

> FontRenderContext,

> you can usually just construct one -- you should know whether you are using text antialiasing and

> fractional metrics or not...

The font is arbitrary, and might change via user input during the execution of the graph...

avstandstuff at 2007-7-6 23:15:28 > top of Java-index,Java Essentials,Java Programming...
# 3
> The font is arbitrary, and might change via user input during the execution of the graph...But where do you store that information? See where I'm going?
DrLaszloJamf at 2007-7-6 23:15:28 > top of Java-index,Java Essentials,Java Programming...
# 4

> > The font is arbitrary, and might change via user

> input during the execution of the graph...

>

> But where do you store that information? See where I'm

> going?

>

I see what you are aiming for, but that information is stored in another 'input'-object... However it is possible to retrieve that information. But then, how do I create a FontRenderContext when I do not know anything about the AffineTransform-object that should have the same value in both the Graphics-Object and the FontRenderContext-object? What would solve my problem in a good way (looking at how my code is written) is if I could understand when the Graphics-object is created (i.e. when JComponent.getGraphics != null) in my JComponent. Or if there is a way of creating a Graphics-object in any other way...

avstandstuff at 2007-7-6 23:15:28 > top of Java-index,Java Essentials,Java Programming...
# 5
Trying using null for the AffineTransform passed to FontRenderContext's constructor. I think theAffineTransform is ignored anyway, in what you are trying to do.
DrLaszloJamf at 2007-7-6 23:15:28 > top of Java-index,Java Essentials,Java Programming...