Program to demonstrate some of font class methods

This program demonstrates some important methods of Java's Font class. The Font class represents fonts, which are used to render text in a visible way. It has following constructors:
Font (Font font)

Font (String name, int style, int size)
The first constructor creates a new Font from the specified font. And the second constructor creates a new Font from the specified name, style and point size. Some of the methods of Font class are given below:
  1. Font getFont( ) : It returns the reference of current font.
  2. String getFamily( ) : It returns the family name of the Font.
  3. String getFontName( ) : It returns the font face name of the Font.
  4. String getName( ) : It returns the logical name of the Font.
  5. int getSize( ) : It returns the point size of the Font, rounded to an integer.
  6. int getStyle( ) : It returns the style of the Font.
  7. boolean isBold( ) : It indicates whether or not the Font object's style is BOLD.
  8. boolean isItalic( ) : It indicates whether or not the Font object's style is ITALIC.
  9. boolean isPlain( ) : It indicates whether or not the Font object's style is PLAIN.

import java.applet.Applet;
import java.awt.Font;
import java.awt.Graphics;

public class FontDemoApplet extends Applet {

 public void paint(Graphics g) {
  Font f = g.getFont();
  String fontName = f.getFontName(); // Getting Font name
  String fontFamily = f.getFamily(); // Getting Font family
  int fontStyle = f.getStyle();      // Getting Font style
  int fontSize = f.getSize();        // Getting Font size
  String fontStl = "Font Style = ", fontS;
  fontName = "Font Name = " + fontName;
  fontFamily = "Font Family = " + fontFamily;
  fontS = "Font Size = " + fontSize;
  if( fontStyle == Font.PLAIN ) 
   fontStl += "Plain";
  if( fontStyle == Font.BOLD )
   fontStl += "Bold";
  if( fontStyle == Font.ITALIC )
   fontStl += "Italic";
  g.drawString(fontName, 50, 50);
  g.drawString(fontFamily, 50, 80);
  g.drawString(fontS, 50, 110);
  g.drawString(fontStl, 50, 140);

/* <applet code="FontDemoApplet.class" width=200 height=200>



Popular posts from this blog

Program to define a class 'employee' with data members as empid, name and salary. Accept data for 5 objects using Array of objects and print it.

Define a class Student with four data members such as name, roll no.,sub1, and sub2. Define appropriate methods to initialize and display the values of data members. Also calculate total marks and percentage scored by student.

Program to input age from user and throw user-defined exception if entered age is negative