R4R Java SourceCode Set Font Java


Set Font Java

In this example we going to explain how we can set font of string on applet. We need to import two packages java.applet.Applet and java.awt.*. The java.applet.Applet is imported to make applet window and used its two methods eg. paint(Graphics g) and init(). In init() method we define the initial property which we required in application. The paint()  method is used to paint the window. We are using Font class to create an objects of font. We are using Font(String fontName, int fontType,int size) constructor. In this example we are creating three objects of font .In first object we are passing the Aligerian type font and it is pain type and the size is 25.Similay we are creating second object the name of Font is TimesRoman and it is Bold and the size of the font is 45.In last we are creating font as Courier type and it is ITALIC type and the size is 25.
To set the font we are using setFont(Font object).

SOURCE CODE


import java.applet.*;
import java.awt.*;
//<applet code="Appf1.class" width=200 height=200></applet>
public class SetFont extends Applet
{
	 Font a,b,c;
	 public void init()
	 {
		setBackground(Color.red);
		a=new Font("Algerian",Font.PLAIN,25);
		b=new Font("TimesRoman",Font.BOLD + Font.ITALIC,45);
		c=new Font("Courier",Font.ITALIC,25);
	 }
	 public void paint(Graphics g)
	 {
		g.setFont(a);
		g.setColor(Color.black);
		g.drawString("R4$",10,20);
		g.setFont(b);
		g.setColor(Color.blue);
		g.drawString("Rajesh",10,50);
		g.setFont(c);
		g.setColor(Color.white);
		g.drawString("Java",10,80);
	}
}

Download Source File
 

Output of This Example