All About Class Font in AWT in Java

0
12
Spread the love
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share

Class Font in AWT in Java
Class Font in AWT in Java

All About Class Font in Java:

Font class represents the various styles for the textual representation along with size.If we use same character in different font,they will appear differently. java supports five basic fonts.

Basic fonts in java:

  1. TimesRoman
  2. Helvetica
  3. Courier
  4. Dialog
  5. DialogInput
The default font is TimesRoman for applications and applets.

Three characters of font:

  1. font name
  2. style
  3. size

The styles of a font:

  1. serif
  2. Sanssarif
  3. Monospaced

The style of a font :

It is specified by named constants defined in Font class. They are as follows:
  1. Font.PLAIN
  2. Font.ITALIC
  3. Font.BOLD
  4. Font.BOLD+Font.Italic
The size of the font is an integer ranging from 10 to 36 but we can use lower number or higher number based on our need.The size of a font is equal to the height of the largest character in the font in terms of pixel. The default font size is 12.

The class is defined as:


public class java.awt.Font extends java.lang.Object{
//member elements
protected String name;//the logical name of the font
protected int size;//the point size of the font
protected int style;//the style of the font.This is the sum of 
//constants PLAIN,BOLD,ITALIC such as:
public final static int BOLD;
public final static int ITALIC;
public final static int PLAIN;

//constructors:
public Font(String name,int style,int size);
//creates a new Font with the specified name style and point size.
//methods
public boolean equals(Object obj);
public boolean isBold();
public boolean isItalic();
public boolean isPlain();
public String toString();
public String getName();
public String getFamily();
public int hashCode();
public int getStyle();
public int getSize();
public static Font getFont(String mn);
public static Font getFont(String mn,Font font);
public void setFont(Font f);
//Font f is represented as new Font("Helvetica",Font.Plain,12);
//Other styles are:
//Font.Plain,Font.BOLD,Font.Italic or Font.Italic+Font.Bold
}
 

How to create a font:

we can construct a new font by the following code:


Font myFont=new Font("Serif",Font.Plain,12);
 

Every graphics context has a current font that helps us to write text.However,we can change it by using setFont() method.so if g is the graphics context then we can use


 g.setFont(myFont);
 

Every component has an associated font.It can also be changed with setFont() method defined in component class.When graphics context is created for drawing on component,the graphics context’s current font is set to the font of the component.

Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

Spread the love
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share

LEAVE A REPLY

Please enter your comment!
Please enter your name here