Home | | Web Programming | Working with Fonts - AWT Java

Chapter: Java The Complete Reference : The Java Library : Introducing the AWT: Working with Windows, Graphics, and Text

Working with Fonts - AWT Java

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

Working with Fonts

The AWT supports multiple type fonts. Years ago, fonts 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. The logical name specifies a name, such as Monospaced, that is linked to an actual font at runtime. The face name specifies a specific font, such as Courier Italic.

Fonts are encapsulated by the Font class. Several of the methods defined by Font are listed in Table 25-2.

The Font class defines these protected 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


Several static fields are also defined.

 

Method : Description

 

static Font decode(String str) : Returns a font given its name.

boolean equals(Object FontObj) : Returns true if the invoking object contains the same font as that specified by FontObj. Otherwise, it returns false.

String getFamily( ) : Returns the name of the font family to which the invoking font belongs.

static Font getFont(String property) : 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) : 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.

int hashCode( ) : Returns the hash code associated with the invoking object.

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.


Table 25-2   A Sampling of Methods Defined by Font

 

Determining the Available Fonts

When working with fonts, often you need to know which fonts are available on your machine. To obtain this information, you can use the getAvailableFontFamilyNames( ) method defined by the GraphicsEnvironment class. It is shown here:

 

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. It is shown here:

 

Font[ ] getAllFonts( )

 

This method returns an array of Font objects for all of the available fonts. Since these methods are members of GraphicsEnvironment, you need a

GraphicsEnvironment reference to call them. You can obtain this reference by using the getLocalGraphicsEnvironment( ) static method, which is defined by

GraphicsEnvironment. It is shown here:

 

static GraphicsEnvironment getLocalGraphicsEnvironment( )

 

Here is an applet that shows how to obtain the names of the available font families:

 

// Display Fonts /*

 

<applet code="ShowFonts" width=550 height=60> </applet>

 

*/

 

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

 

public class ShowFonts extends Applet {

public void paint(Graphics g) {

 

String msg = ""; String FontList[];

 

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();

 

FontList = ge.getAvailableFontFamilyNames();

for(int i = 0; i < FontList.length; i++)

 

msg += FontList[i] + " ";

 

g.drawString(msg, 4, 16);

 

}

 

}

Sample output from this program is shown next. However, when you run this program, you may see a different list of fonts than the one shown in this illustration.


Creating and Selecting a Font

 

To create a new font, construct a Font object that describes that font. One Font constructor has this general form:

 

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, SansSerif, Serif, and Monospaced. Dialog is the font used by your system’s dialog boxes. Dialog is also the default if you don’t explicitly set a font. You can also use any other fonts supported by your 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.

 

To use a font that you have created, you must select it using setFont( ), which is defined by Component. It has this general form:

 

void setFont(Font fontObj)

 

Here, fontObj is the object that contains the desired font.

 

The following program outputs a sample of each standard font. Each time you click the mouse within its window, a new font is selected and its name is displayed.

 

// Show fonts.

 

import java.applet.*; import java.awt.*; import java.awt.event.*; /*

 

<applet code="SampleFonts" width=200 height=100> </applet>

 

*/

 

public class SampleFonts extends Applet {

int next = 0;

 

Font f; String msg;

 

public void init() {

 

f = new Font("Dialog", Font.PLAIN, 12); msg = "Dialog";

 

setFont(f);

 

addMouseListener(new MyMouseAdapter(this));

 

}

 

public void paint(Graphics g) { g.drawString(msg, 4, 20);

 

}

 

}

 

class MyMouseAdapter extends MouseAdapter { SampleFonts sampleFonts;

 

public MyMouseAdapter(SampleFonts sampleFonts) { this.sampleFonts = sampleFonts;

 

}

 

public void mousePressed(MouseEvent me) {

// Switch fonts with each mouse click.

sampleFonts.next++; switch(sampleFonts.next) {

 

case 0:

 

sampleFonts.f = new Font("Dialog", Font.PLAIN, 12); sampleFonts.msg = "Dialog";

 

break; case 1:

 

sampleFonts.f = new Font("DialogInput", Font.PLAIN, 12); sampleFonts.msg = "DialogInput";

 

break; case 2:

 

sampleFonts.f = new Font("SansSerif", Font.PLAIN, 12); sampleFonts.msg = "SansSerif";

 

break; case 3:

 

sampleFonts.f = new Font("Serif", Font.PLAIN, 12); sampleFonts.msg = "Serif";

 

break; case 4:

 

sampleFonts.f = new Font("Monospaced", Font.PLAIN, 12); sampleFonts.msg = "Monospaced";

 

break;

 

}

 

if(sampleFonts.next == 4) sampleFonts.next = -1;

 

sampleFonts.setFont(sampleFonts.f);

 

sampleFonts.repaint();

Sample output from this program is shown here:



Obtaining Font Information

 

Suppose you want to obtain information about the currently selected font. To do this, you must first get the current font by calling getFont( ). This method is defined by the Graphics class, as shown here:

 

Font getFont( )

 

Once you have obtained the currently selected font, you can retrieve information about it using various methods defined by Font. For example, this applet displays the name, family, size, and style of the currently selected font:

 

// Display font info.

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

/*

 

<applet code="FontInfo" width=350 height=60> </applet>

 

*/

 

public class FontInfo extends Applet { public void paint(Graphics g) {

 

Font f = g.getFont();

 

String fontName = f.getName(); String fontFamily = f.getFamily(); int fontSize = f.getSize();

 

int fontStyle = f.getStyle();

 

String msg = "Family: " + fontName; msg += ", Font: " + fontFamily;

 

msg += ", Size: " + fontSize + ", Style: "; if((fontStyle & Font.BOLD) == Font.BOLD)

 

msg += "Bold ";

 

if((fontStyle & Font.ITALIC) == Font.ITALIC) msg += "Italic ";

 

if((fontStyle & Font.PLAIN) == Font.PLAIN) msg += "Plain ";

 

g.drawString(msg, 4, 16);

 

}

 

}


Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Java The Complete Reference : The Java Library : Introducing the AWT: Working with Windows, Graphics, and Text : Working with Fonts - AWT Java |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.