|
How to draw text on a Component |
|
|
This Java tip demostrates how to draw a text on a component face. The text can be drawn by
overwriting the paint() function of the component.
public void paint(Graphics g) {
// Set the font for example Lucida Sans Typewriter if it is
// different from the default font
String family = "Lucida Sans Typewriter";
int style = Font.PLAIN;
int size = 18;
Font font = new Font(family, style, size);
g.setFont(font);
// Draw a text such that its base line is at x, y
int x = 10;
int y = 10;
g.drawString("Text", x, y);
// Draw a text such that the top-left corner is at x, y
x = 10;
y = 30;
FontMetrics fontMetrics = g.getFontMetrics();
g.drawString("text", x, y+fontMetrics.getAscent());
}
|
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.