In layer 1, the underline text attribute that is commented out does not
place the underscore low enough. In layer 2, I have constructed an
underscore that I can move around. The object is to underscore any character
of the string buttonText as indicated by the underscoreIndex. The offsets
you see near the bottom work well on a string of 8 characters but fail on
strings of other lengths. How can I figure out where character x is relative
to the LayerComposition so that I can accurately place the underscore in the
right place?
// Layer 1
// Create a layer for the text with antialiasing
AttributedString styledText = new AttributedString(buttonText);
styledText.addAttribute(TextAttribute.FAMILY, "Verdana");
styledText.addAttribute(TextAttribute.SIZE, new Float(10.5));
styledText.addAttribute(TextAttribute.WEIGHT,
TextAttribute.WEIGHT_BOLD);
styledText.addAttribute(TextAttribute.FOREGROUND, Color.white);
// styledText.addAttribute(TextAttribute.UNDERLINE,
TextAttribute.UNDERLINE_ON, 1, 2);
// styledText.addAttribute(TextAttribute.UNDERLINE,
TextAttribute.UNDERLINE_LOW_TWO_PIXEL, this.underscoreIndex,
this.underscoreIndex + 1);
TextLayer textLayer = new TextLayer(cmp,
new AttributedString[]{styledText},
new FillRenderer(textColor),
new Position(Anchor.CENTER, 2000,
2000),
size.width,
TextAlignment.CENTER);
textLayer.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
// Layer 2
// Turn the text into glyph shape in order to use metrics
Font font = new Font("Verdana", Font.BOLD, 10);
FontRenderContext frc = new FontRenderContext(null, true, true);
GlyphVector glyphVector = font.createGlyphVector(frc, buttonText);
Point2D pos = glyphVector.getGlyphPosition(underscoreIndex);
System.out.println("Position = " + pos);
System.out.println("Bounds = " + textLayer.getBounds());
Rectangle2D underscore = new Rectangle2D.Double( (pos.getX() + 21.8 +
(underscoreIndex/2)), (pos.getY() + 24.5), 8.0, 1.0 );
Renderer underscoreRenderer = new FillRenderer(Color.white);
ShapeLayer underscoreLayer = new ShapeLayer(cmp, underscore,
underscoreRenderer);
David Anderson
User Interface Design Engineer
A picture is worth a thousand words...
===========================================================================
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".