Thursday, 27 February 2014

METHODS FOR FONTS IN GRAPHICS CLASS

WORKING WITH FONTS

Font Class in java applet with sample program..........

The AWT supports multiple type fonts. Within AWT, a font is specified by its name, style, and size. Each platform that supports Java provides a basic set of fonts. Beginning with Java 2, 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. The logical name specifies a category of font, such as Monospaced. The face name specifies a specific font, such as Courier Italic.
An instance of the Font class represents a specific font to the system.

The Font class defines these variables:-


 Variable                     Meaning


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


There are four styles for displaying fonts in Java: plain, bold, italic, and bold italic. Three class constants are used to represent font styles:

public static final int BOLD:The BOLD constant represents a boldface font.

public static final int ITALIC:The ITALIC constant represents an italic font.

public static final int PLAIN:The PLAIN constant represents a plain or normal font.

The combination BOLD/ITALIC represents a bold italic font. PLAIN combined with either BOLD or ITALIC represents bold or italic, respectively.




public Font (String name, int style, int size):There is a single constructor for Font. It requires a name, style, and size. name argument represents the name of the font to create.

Example :


setFont (new Font ("TimesRoman", Font.BOLD | Font.ITALIC, 20));

public String getName ():The getName() method returns the font's logical name. This is the name that is passed to the constructor when the instance of the Font is created.

public String getFamily ():The getFamily() method returns the name of the font family to which the invoking font belongs.

public int getStyle ():The getStyle() method returns the current style of the invoking font as an integer. It is convenient to use the isPlain(), isBold(), and isItalic() methods to find out the current style.

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.

public int getSize ():The getSize() method returns the point size of the invoking font, as set by the size parameter in the constructor. The actual displayed size may be different.

public static Font getFont(String property):Returns the font associated with the system property specified by property. Null is returned if property does not exist.

public static Font getFont(String property, Font defaultFont):- Returns the font associated with the system property specified by property. The font specified by defaultFont is returned if property does not exist.

public int hashCode ():The hashCode() method returns a hash code for the font. This hash code is used whenever a Font object is used as the key in a Hashtable.

public boolean equals (Object o):The equals() method overrides the equals() method of Object to define equality for two Font objects. Two Font objects are equal if their size, style, and name are equal.

public String toString ():The toString() method of Font returns a string showing the current family, name, style, and size settings. 

For example:


java.awt.Font[family=TimesRoman,name=TimesRoman,style=bolditalic ,size=20]

DETERMINING THE AVAILABLE FONTS

When working with fonts, we need to know which fonts are available on our machine. To know this, we can use the getAvailableFontFamilyNames( ) method defined by the GraphicsEnvironment class. 

String[ ] getAvailableFontFamilyNames( ):This method returns an array of strings that contains the names of the available font families.

In addition, the getAllFonts( ) method is defined by the GraphicsEnvironment class. 

Font[ ] getAllFonts( ):This method returns an array of Font objects for all of the available fonts.

We need a Graphics Environment reference to call them as these are the members of Graphics Environment. We can obtain this reference by using the getLocalGraphicsEnvironment( ) static method, which is defined by GraphicsEnvironment.


static GraphicsEnvironment getLocalGraphicsEnvironment( )









No comments :

Post a Comment