Java Reference
In-Depth Information
1
// Fig. 13.16: Metrics.java
2
// Displaying font metrics.
3
import javax.swing.JFrame;
4
5
public class Metrics
6
{
7
// execute application
8
public static void main(String[] args)
9
{
10
// create frame for MetricsJPanel
11
JFrame frame = new JFrame( "Demonstrating FontMetrics" );
12
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
13
14
MetricsJPanel metricsJPanel = new MetricsJPanel();
15
frame.add(metricsJPanel);
16
frame.setSize( 510 , 240 );
17
frame.setVisible( true );
18
}
19
} // end class Metrics
Fig. 13.16 | Displaying font metrics.
Line 16 of Fig. 13.15 creates and sets the current drawing font to a SansSerif , bold,
12-point font. Line 17 uses Graphics method getFontMetrics to obtain the FontMetrics
object for the current font. Line 18 outputs the String representation of the Font returned
by g.getFont() . Lines 19-22 use FontMetric methods to obtain the ascent, descent,
height and leading for the font.
Line 24 creates a new Serif , italic, 14-point font. Line 25 uses a second version of
Graphics method getFontMetrics , which accepts a Font argument and returns a corre-
sponding FontMetrics object. Lines 28-31 obtain the ascent, descent, height and leading
for the font. The font metrics are slightly different for the two fonts.
13.5 Drawing Lines, Rectangles and Ovals
This section presents Graphics methods for drawing lines, rectangles and ovals. The
methods and their parameters are summarized in Fig. 13.17. For each drawing method
that requires a width and height parameter, the width and height must be nonnegative
values. Otherwise, the shape will not display.
 
 
Search WWH ::




Custom Search