Wednesday, 30 November 2011

Working with Font class


The AWT supports multiple type fonts. Fonts have emerged from the domain of traditional typesetting to become an important part of computer-generated documents and displays.

The AWT provides flexibility by abstracting font-manipulation operations and allowing for dynamic selection of fonts.

Fonts have a family name, a logical font name, and a face name.

The family name is the general name of the font, such as Courier , Arial,Times new Roman etc. The logical name specifies a category of font, such as Monospaced. The face name specifies a specific font, such as Courier Italic. Fonts are encapsulated by the Font class.

The Font class defines these variables:

Variable                                            Meaning

String name                                                 Name of the font
float pointSize                                    Size of the font in points
int size                                               Size of the font in points
int style                                             Font style

Creating Font:

To create new font consider following constructor.

Font(String fontName, int fontStyle, int pointSize)

Here, fontName specifies the name of the desired font. The name can be specified using either the logical or face name. All Java environments will support the following fonts: Dialog, DialogInput, Sans Serif, Serif, Monospaced, and Symbol. Dialog is the font used by system’s dialog boxes. Dialog is also the default font. It  can  be possible to use any other fonts supported by particular environment, but be careful—these other fonts may not be universally available.

The style of the font is specified by fontStyle. It may consist of one or more of these three constants: Font.PLAIN, Font.BOLD, and Font.ITALIC. To combine styles, OR them together. For example, Font.BOLD | Font.ITALIC specifies a bold, italics style.
The size, in points, of the font is specified by pointSize.


Methods

String getFamily( )

This method returns returns the name of the font family  which the invoking font belongs.

static Font getFont(String property)

This method returns the font associated with the system property specified by property. null is returned if property does not exist.

static Font getFont(String property, Font defaultFont)

This method returns returns the font associated with the system property specified by property. The font specified by defaultFont is returned if property does not exist.

String getFontName()

Returns the face name of the invoking font.

String getName( )

Returns the logical name of the invoking font.

int getSize( )

Returns the size, in points, of the invoking font.

int getStyle( )

Returns the style values of the invoking font.

boolean isBold( )

Returns true if the font includes the BOLD style value. Otherwise, false is returned.

boolean isItalic( )

Returns true if the font includes the ITALIC style value. Otherwise, false is returned.

boolean isPlain( )

Returns true if the font includes the PLAIN style value. Otherwise, false is returned.

String toString( )

Returns the string equivalent of the invoking font.


Example

import java.awt.*;
import java.applet.*;

/*
<applet code="FontMet" height=300 width=400>
</applet>
*/

public class FontMet extends Applet
{
Font f;
public void init()
{
    f=new Font("Monaspaced",Font.BOLD,20);
  setFont(f);
             }
public void paint(Graphics g)
{
g.drawString("HELLO",20,30);
//for get fontname
g.drawString("Font name:",20,70);
g.drawString(f.getFontName(),140,70);
    
     //for get fontsize
     int i=f.getSize();
     String s=Integer.toString(i);
     g.drawString("Font size:",20,120);   
     g.drawString(s,120,120);
  }
}

Posted by : Ruchita Pandya

No comments:

Post a Comment