You are here:   HomeJava ProgrammingAWT and AppletsFont Class in Java Example
by Dinesh Thakur Category: AWT and Applets

The Font class provides a method of specifying and using fonts. The Font class constructor constructs font objects using the font's name, style (PLAIN, BOLD, ITALIC, or BOLD + ITALIC), and point size. Java's fonts are named in a platform independent manner and then mapped to local fonts that are supported by the operating system on which it executes. The getName() method returns the logical Java font name of a particular font and the getFamily() method returns the operating system-specific name of the font. The standard Java font names are Courier, Helvetica, TimesRoman etc.

The font can be set for a graphics context and for a component.

Font getFont() It is a method of Graphics class used to get the font property

setFont(Font f) is used to set a font in the graphics context

There are following logical font names which are standard on all platforms and are mapped to actual fonts on a particular platform:

"Serif" variable pitch font with serifs

"SansSerif" variable pitch font without serifs

"Monospaced" fixed pitch font

"Dialog" font for dialogs

"DialogInput" font for dialog input

"Symbol" mapped to the Symbol font

Font style is specified using constants from the Font class:

Font.BOLD

Font.ITALIC

import java.applet.Applet;

import java.awt.*;
import java.awt.event.*;
/* <APPLET CODE ="FontClass.class" WIDTH=300 HEIGHT=200> </APPLET> */
public class FontClass extends java.applet.Applet
    {
        Font f;
        String m;
          public void init()
             {
                 f=new Font("Arial",Font.ITALIC,20);
                 m="Welcome to Java";
                 setFont(f);
             }
                 public void paint(Graphics g)
                     {
                          Color c=new Color(0,255,0);
                          g.setColor(c);
                          g.drawString(m,4,20);
                     }
  }                                                                                             
          

Font Class in Java Example





Subscribe To Free Daily Newsletter!

Get Free News Updates Delivered Directly To Your Inbox
About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to contact us.



What's New and Popular





Popular Article