![]() ![]() ![]() |
Start of Tutorial > Start of Trail > Start of Lesson | Search |
You can display a text string with any font available on your system in any size and style that you choose. To determine what fonts are available on your system, you can call theGraphicsEnvironment.getAvailableFontFamilyNames
method. This method returns an array of strings that contains the family names of the available fonts. Any of the strings, along with a size and style argument, can be used to create a newFont
object. After creating aFont
object, you can reset its font family name, size or style to create a custom font.
The following applet allows you to change the font, size, and style of the displayed text.
The complete code for this applet is in
This is a picture of the applet's GUI. To run the applet, click the picture. The applet will appear in a new browser window.FontSelection.java
.
The
getAvailableFontFamilyNames
method ofGraphicsEnvironment
returns the font family names of the fonts available on your system.The initialGraphicsEnvironment gEnv = GraphicsEnvironment.getLocalGraphicsEnvironment(); String envfonts[] = gEnv.getAvailableFontFamilyNames(); Vector vector = new Vector(); for ( int i = 1; i < envfonts.length; i++ ) { vector.addElement(envfonts[i]); }Font
object is created with styleFont.PLAIN
and size 10. The other available styles are ITALIC, BOLD and BOLD+ITALIC.A newFont thisFont; ... thisFont = new Font("Arial", Font.PLAIN, 10);Font
is created from the specified font name, style and size.To use the same font family but change one or both of the style and size attributes, you can call one of thepublic void changeFont(String f, int st, String si){ Integer newSize = new Integer(si); int size = newSize.intValue(); thisFont = new Font(f, st, size); repaint(); }deriveFont
methods.To control the font used to render text, you set the font attribute in the
Graphics2D
context before rendering. The font attribute is set by passing aFont
object to thesetFont
method. In this example, the font attribute is set to the newly constructedFont
object and then the string is drawn in the center of the Component using the specified font. In thepaint
method, the font attribute of theGraphics2D
context is set to the newFont
. The string is drawn in the middle of the component with the new font.g2.setFont(thisFont); String change = "Pick a font, size, and style to change me"; FontMetrics metrics = g2.getFontMetrics(); int width = metrics.stringWidth( change ); int height = metrics.getHeight(); g2.drawString( change, w/2-width/2, h/2-height/2 );
Note: Due to bug #4155852
, FontSelection might not work properly for all font names returned from the call to getFontFamilyNames. The sample might not respond to changes in size or style and the text might not show up at all when some fontnames are chosen. In general, Courier and Helvetica work fine. In the meantime, check back periodically to see if these problems have been fixed.
![]() ![]() ![]() |
Start of Tutorial > Start of Trail > Start of Lesson | Search |