Microsoft SDK for Java

Setting the Font on a Graphics Object

The Graphics object supports the setFont method, which associates a font with the object. After you associate a Font object with the Graphics object, all text drawn within the Graphics object’s bounding rectangle is drawn using that font.

The following example illustrates the process of setting the background color of the Graphics object to white, the text color to black, and the font to 26-point Times New Roman. The text is drawn to the display at a specified position.

Example

Graphics g = this.createGraphics();

g.setFont(new Font("Times New Roman", 26));
g.setBackColor(new Color(255, 255, 255));
g.setTextColor(new Color(0, 0, 0));
g.drawString("Hello, World", 0, 0);

© 1999 Microsoft Corporation. All rights reserved. Terms of use.