Java Reference
In-Depth Information
An application can create a font object as follows:
Font f = new Font(“SansSerif”, Font.BOLD, 14);
The font is then installed in the device context using the setFont()
method of the Component class, as follows:
g.setFont(f);
The following program, named Text.java, demonstrates the fundamen-
tal manipulations required for a Java graphics application that displays
text to the screen.
//**********************************************************
// Project: Java for Engineers
// Program: Text.java
// Reference: Chapter 26
// Topics:
// 1. Instantiating a Graphics object
// 2. Redefining the paint() method
// 3. Changing fonts
// 4. Displaying a text message
//**********************************************************
// Requires:
// 1. ActiveFrame class in the current directory
//**********************************************************
//**********************************************************
import java.awt.*;
//*********************************************
//*********************************************
// driving class
//*********************************************
//*********************************************
public class Text extends ActiveFrame
{
//*********************************
// redefinition of paint()
//*********************************
// The paint() method of the Java AWT Canvas class
// fills the Window's client area with the default
// color. An application can redefine paint() in
// order to output graphics to the display.
// The following code redefines paint(), which receives
// a Graphics object as a parameter, and then uses
// the drawString method of the Graphics class to
// display a message on the screen.
public void paint(Graphics g)
{
// Define a monospaced, bold, 16-point font
Font f = new Font(“Monospaced”, Font.BOLD, 16);
// Set the font in the device context
g.setFont(f);
// Display a text string
Search WWH ::




Custom Search