We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Java Graphics2D e getFontRenderContext()
Page Index Toggle Pages: 1
Java Graphics2D e getFontRenderContext() (Read 941 times)
Java Graphics2D e getFontRenderContext()
Aug 14th, 2005, 4:18pm
 
To get the property of a font I'd like to use the getFontRenderContext() of a Graphics2D, any idea on how to do it (and maybe if it is possible) inside processing? I tried to create a new Graphic2D object but I obtained the error:

The costructor "Graphics2D();" in type "java.awt.Graphics2D" has protected access and is not accessible here.


Thanks, chr
Re: Java Graphics2D e getFontRenderContext()
Reply #1 - Aug 14th, 2005, 9:17pm
 
the faq has info about how to get the Graphics2D object:
http://processing.org/faq/bugs.html#java2d

then, g2.getFontRenderContext() will do it.

note that processing is not using regular "java" fonts, so strictly speaking, this isn't going to do much for you.

as of rev 92 and later, i've started adding code so that it *will* use the java version of a font behind the scenes, when the JAVA2D renderer is in use (the default situation), but not in P3D or OPENGL.

so when the native font is available, the variable g.textFontNative will be non-null, and point to a java.awt.Font object of the current font, and the FontRenderContext stuff may actually mean a little more.

also, g.textFontNativeMetrics will contain a FontMetrics object, when available.

* note that this sort of thing is most likely NOT going to work when you export this for the web and try to use it on other people's machines *
Re: Java Graphics2D e getFontRenderContext()
Reply #2 - Aug 15th, 2005, 12:48am
 
well, actually my intent is to take the metric from a loaded Font something like:
Code:

loadedFont = loadFont("DIN-Medium-24.vlw");
Font bigfont = loadedFont.font.deriveFont(AffineTransform.getScaleInstance(30.0, 30.0));
GlyphVector gv = bigfont.createGlyphVector(Graphics2DInstance.getFontRenderContext(),"test");


This should work regardless the renderer, shouldn't it?


I'll check the FAQ, many thanks, chr
Re: Java Graphics2D e getFontRenderContext()
Reply #3 - Aug 15th, 2005, 12:59am
 
yup, see fjen's post on the alpha board:
http://processing.org/discourse/yabb/YaBB.cgi?board=Syntax;action=display;num=1105733642;start=4

also implemented in textCharImpl() here:
http://dev.processing.org/processing/opengl/PGraphicsGL.java
(don't mind the mess, that code is in progress)
Re: Java Graphics2D e getFontRenderContext()
Reply #4 - Aug 15th, 2005, 1:04am
 
In fact it seems to work well on-line with a loaded font, of course with the default renderer Smiley

do you have any link to documentation that explains the rendering issues in Java (so not only the p5 ones) to understand better the logic behind it? In my ignorance I'm wondering why I can't instantiate a Graphic2D object inside a OpenGL render Smiley

Thanks a lot Ben, chr
Re: Java Graphics2D e getFontRenderContext()
Reply #5 - Aug 15th, 2005, 1:11am
 
of course it works on your own machine, you're missing my point. you're not guaranteed that any other users will have that font installed, so on their machines, bigfont.font will be null and no error will be passed.

Graphics2D is a complex beast, and has to be associated with a canvas or component. the proper way to get a g2d is to get it from one that's been created. see PGraphicsGL.java for how to get it from its canvas, or PGraphics2.java for how to get it from the g2 object.
Re: Java Graphics2D e getFontRenderContext()
Reply #6 - Aug 15th, 2005, 1:26am
 
So you are saing that there is no chance to use a loaded font to get the Shape Sad
It sounds strange to me since at the end there should be the Font object :S

A bit confused, chr
Re: Java Graphics2D e getFontRenderContext()
Reply #7 - Aug 15th, 2005, 1:31am
 
in anycase fjen example is very similar on what I'm doing, and since it doesn't use Graphics2D it seems to work perfectly with OPENGL too, thanks for the link Wink
Page Index Toggle Pages: 1