I'm doing an applet that needs to draw several lines of arbitrarily scaled text in a
rectangular frame.
I need to be able to dynamically resize the font, dragging and scaling the box+text
around the screen. I have written code that does this, but it's a little slower than
I'd like (I'm finding that it is faster to generate gifs and use drawImage(), which
surprises me).
So I wonder if anyone would care to look at a fragment of my code and let me know if
I'm missing anything - I think my technique for figuring out the bounding box of the
paragraph and the kerning is a little awkward, and I wonder if there aren't any faster
calls I could be making. I'm fairly new to graphics2D and fonts, so it's likely that
I'm doing something silly.
Any ideas are appreciated.
Thanks.
Matt
/////////////////////////////////
// I CALL THESE DURING SETUP TO HAVE ACCESS TO THE FONT NAME LIST
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
String envfonts[] = ge.getAvailableFontFamilyNames();
// THE REST IS DONE DURING THE ANIMATION LOOP
Font f = new Font(textFontName,fontStyle, fontSize);
AffineTransform at = new AffineTransform();
GlyphVector gv[] = new GlyphVector[10];
at.scale(scale, scale);
FontRenderContext frc = new FontRenderContext(at, false, false);
int i;
int rw=0;
int rh=0;
Rectangle2D r=null;
// CREATE GVs AND FIGURE OUT BOUNDING BOX
for (i = 0 ; i < textLines.length; i++) {
gv[i] = f.createGlyphVector(frc, textLines[i]);
r = gv[i].getLogicalBounds();
rh += r.getHeight();
if ((int)r.getWidth() > rw)
rw = (int)r.getWidth();
}
// FIGURE OUT LEADING
int leading = (int)(r.getHeight()/8);
rh += leading*(2+i);
rw += leading*2;
// DRAW FRAME
int frame[] = new int[4];
frame[0]=(int) center.x-(rw/2)-leading;
frame[1]=(int) center.y-(rh/2)-leading;
frame[2]=(int)(rw+2*leading);
frame[3]=(int)(rh+2*leading);
_gc2.drawRect(frame[0], frame[1], frame[2], frame[3]);
// RENDER TEXT
for (i = 0 ; i < textLines.length; i++) {
if (textLines[i] == null) break;
_gc2.drawGlyphVector(gv[i], (center.x-rw/2)+leading,
(center.y-rh/2)+leading+(int)((i+1)*r.getHeight()+i*leading) );
}
//////////////////////////////
===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".