|
How to draw a text with mixed styles on a component |
|
|
This Java tips demonstrates a method of drawing a text with mixed style on a Java component.
The text can be drawn by overwriting the paint() function of the component. Here a developer
may add a new font and background color to a part of the text. Different styles may be
applied on different parts of text according to the need of the developer. This involves
setting the attributes of the different text parts.
// Apply styles to text from start to end which
// may be defined by the developer
AttributedString textstr = new AttributedString("text");
textstr.addAttribute(TextAttribute.FONT, font, start, end);
textstr.addAttribute(TextAttribute.BACKGROUND, color, start, end);
// Draw mixed-style text
TextLayout layout = new TextLayout(textstr.getIterator(),
g2d.getFontRenderContext());
layout.draw(g2d, x, y);
|
Related Tips
|
Page 1 of 0 ( 0 comments )
You can share your information about this topic using the form below!
Please do not post your questions with this form! Thanks.