Java Reference
In-Depth Information
Method
Description
FontMetrics methods
public int getAscent() Returns the ascent of a font in points.
public int getDescent() Returns the descent of a font in points.
public int getLeading() Returns the leading of a font in points.
public int getHeight() Returns the height of a font in points.
Graphics methods for getting a Font 's FontMetrics
public FontMetrics getFontMetrics()
Returns the FontMetrics object for the current drawing Font .
public FontMetrics getFontMetrics(Font f)
Returns the FontMetrics object for the specified Font argument.
Fig. 13.14 | FontMetrics and Graphics methods for obtaining font metrics.
1
// Fig. 13.15: MetricsJPanel.java
2
// FontMetrics and Graphics methods useful for obtaining font metrics.
3
import java.awt.Font;
4
5
import java.awt.FontMetrics;
import java.awt.Graphics;
6
import javax.swing.JPanel;
7
8
public class MetricsJPanel extends JPanel
9
{
10
// display font metrics
11
@Override
12
public void paintComponent(Graphics g)
13
{
14
super .paintComponent(g);
15
16
g.setFont( new Font( "SansSerif" , Font.BOLD , 12 ));
17
FontMetrics metrics = g.getFontMetrics();
g.getFont()
metrics.getAscent()
metrics.getDescent()
metrics.getHeight()
metrics.getLeading()
18
g.drawString( "Current font: " +
, 10 , 30 );
19
g.drawString( "Ascent: " +
, 10 , 45 );
20
g.drawString( "Descent: " +
, 10 , 60 );
21
g.drawString( "Height: " +
, 10 , 75 );
22
g.drawString( "Leading: " +
, 10 , 90 );
23
24
Font font = new Font( "Serif" , Font.ITALIC , 14 );
25
metrics = g.getFontMetrics(font);
26
g.setFont(font);
27
g.drawString( "Current font: " + font, 10 , 120 );
28
g.drawString( "Ascent: " +
metrics.getAscent()
metrics.getDescent()
metrics.getHeight()
metrics.getLeading()
, 10 , 135 );
29
g.drawString( "Descent: " +
, 10 , 150 );
30
g.drawString( "Height: " +
, 10 , 165 );
31
g.drawString( "Leading: " +
, 10 , 180 );
32
}
33
} // end class MetricsJPanel
Fig. 13.15 | FontMetrics and Graphics methods useful for obtaining font metrics.
 
Search WWH ::




Custom Search