You are on page 1of 6

ST.

XAVIER’S COLLEGE
MAITIGHAR, KATHMANDU

Computer Graphics (CG)

Lab Report 1
Text and Fonts

Submitted by
Jeshika Baniya
019BIM017

Submitted to
Mr. Ganesh Yogi
Lecturer
Department of Computer Science
St. Xavier’s College
Theory:
In Java, Font is a class that belongs to java. awt package. It implements the Serializable interface.
FontUIResource is the direct known subclass of the Java Font class.

An instance of the Font class represents a specific font to the system. Within AWT, a font is
specified by its name, style, and point size. Each platform that This method returns a String array
of the fonts available. Under Java 1.0, on any platform, the available fonts were: TimesRoman,
Helvetica, Courier, Dialog, DialogInput, and ZapfDingbats. For copyright reasons, the list is
substantially different in Java 1.1: the available font names are TimesRoman images, Serif,
Helvetica images, SansSerif, Courier images, Monospaced, Dialog, and DialogInput. The actual
fonts available aren't changing; the deprecated font names are being replaced by non-copyrighted
equivalents. Thus, TimesRoman is now Serif, Helvetica is now SansSerif, and Courier is
Monospaced.

The Font Class


Constants
There are four styles for displaying fonts in Java: plain, bold, italic, and bold italic.
Three class constants are used to represent font styles:
public static final int BOLD
 The BOLD constant represents a boldface font.
public static final int ITALIC
 The ITALIC constant represents an italic font.
public static final int PLAIN
 The PLAIN constant represents a plain or normal font.
The combination BOLD | ITALIC represents a bold italic font. PLAIN combined with either
BOLD or ITALIC represents bold or italic, respectively.
There is no style for underlined text. If you want underlining, you have to do it manually, with
the help of FontMetrics.
Font Faces and Names
A font may have many faces such as heavy, regular, medium, oblique, gothic, etc. All font faces
have a similar typography design.
A Font object has three different names that are:
 Logical font name: It is the name that is used to construct the font.
 Font face name: It is the name of particular font face. For example, Helvetica Bold.
 Family name: It is the name of the font family. It determines the typography design
among several faces.
The Java Font class represents an instance of a font face from a collection of font faces that are
present in the system resources of the host system. Example of font faces are Arial Bold, Courier
Bold Italic, etc. A font face (each differing in size, style, transform and font feature) may
associate with several Font objects.

Font Class Constructors

S.N. Constructor & Description

1
protected Font() ()
Creates a new Font from the specified font.

2
Font(Map<? extends AttributedCharacterIterator.Attribute,?> attributes)
Creates a new Font from the specified font.

3
Font(String name, int style, int size)
Creates a new Font from the specified font.
Algorithm
Step 1:
Import libraries
import java.awt.*
import javax.swing.*
import java.awt.Font

Step 2:
Using the constructor to create a font class and set font.
Font f1= new Font("Helvetica", Font.ITALIC, 22);
setFont(f1);

Step 3:
Write a string and print in an applet window or AWT/Swing window.
drawString("The required text", Font.ITALIC, 22);
Program
package jeshika;
import java.awt.*;
import java.swing.*;
import javax.swing.*;
public class fontexample extends JFrame
{
fontexample()
{
setSize(500,700);
setLayout(new FlowLayout(FlowLayout.CENTER));
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public void paint(Graphics g)


{
Graphics2D g2d=(Graphics2D)g;
Font f1=new Font("Helvetica",Font.ITALIC,22);
g2d.setPaint(Color.blue);
g2d.setFont(f1);
g2d.drawString("St. Xaviers College",200,200);
}
public static void main(String[] args)
{
new fontexample();
}
}
Output

Conclusion
After completing this lab report, I have known how to set specific text with different font colors
and different styles.

You might also like