Font Control
- Most font methods and font constants are part of class Font. Some methods of class Font and class Graphics are Class Font’s constructor takes three arguments—the font name, font style and font size.
- The font name is any font currently supported by the system where the program is running, such as standard Java fonts Monospaced, SansSerif and Serif.
- The font style is Font.PLAIN, Font.
- ITALIC or Font.BOLD (static constants of class Font).
public final static int PLAIN
// Font class A constant representing a plain font style.
public final static int BOLD
// Font class A constant representing a bold font style.
public final static int ITALIC
// Font class A constant representing an italic font style.
public Font( String name, int style, int size )
Creates a Font object with the specified font, style and size.
public int getStyle()
// Font class Returns an integer value indicating the current font style.
public int getSize()
// Font class Returns an integer value indicating the current font size.
public String getName()
// Font class Returns the current font name as a string.
public String getFamily()
// Font class Returns the font’s family name as a string.
public boolean isPlain()
// Font class Tests a font for a plain font style. Returns true if the font is plain.
public boolean isBold()
// Font class Tests a font for a bold font style. Returns true if the font is bold.
public boolean isItalic()
// Font class Tests a font for an italic font style. Returns true if the font is italic.
public Font getFont()
// Graphics class Returns a Font object reference representing the current font.
public void setFont(Font f)
// Graphics class Sets the current font to the font, style and size specified by the Font
Program
3 import java.awt.*;
4 import javax.swing.*;
5 import java.awt.event.*;
67
public class Fonts extends JFrame {
8 public Fonts()
9 {
10 super( "Using fonts" );
11
12 setSize( 420, 125 );
13 show();
14 }
15
16 public void paint( Graphics g )
17 {
18 // set current font to Serif (Times), bold, 12pt
19 // and draw a string
20 g.setFont( new Font( "Serif", Font.BOLD, 12 ) );
21 g.drawString( "Serif 12 point bold.", 20, 50 );
22
23 // set current font to Monospaced (Courier),
24 // italic, 24pt and draw a string
25 g.setFont( new Font( "Monospaced", Font.ITALIC, 24 ) );
26 g.drawString( "Monospaced 24 point italic.", 20, 70 );
27
28 // set current font to SansSerif (Helvetica),
29 // plain, 14pt and draw a string
30 g.setFont( new Font( "SansSerif", Font.PLAIN, 14 ) );
31 g.drawString( "SansSerif 14 point plain.", 20, 90 );
32
33 // set current font to Serif (times), bold/italic,
34 // 18pt and draw a string
35 g.setColor( Color.red );
36 g.setFont(
37 new Font( "Serif", Font.BOLD + Font.ITALIC, 18 ) );
38 g.drawString( g.getFont().getName() + " " +
39 g.getFont().getSize() +
40 " point bold italic.", 20, 110 );
41 }
42
43 public static void main( String args[] )
44 {
45 Fonts app = new Fonts();
46
47 app.addWindowListener(
48 new WindowAdapter() {
49 public void windowClosing( WindowEvent e )
50 {
51 System.exit( 0 );
52 }
53 }
54 );
55 }
56 }
Output
Buy It Now