what is the role of fonts in java explain with


What is the role of fonts in java explain with example?

You've already seen one instance of drawing text in the HelloWorldApplet program of the last chapter. You call the drawString() method of the Graphics object. This method is passed the String you need to draw as well as an x and y coordinate. If g is a Graphics object, then the syntax is

g.drawString(String s, int x, int y)

The String is easily the text you want to draw. The two integers are the x and y coordinates of the lower left-hand corner of the String. The String will be drawn above and to the right of this point. Therefore letters along with descenders such as y and p may have their descenders drawn below the line.

Until now all the applets have used the default font, probably a few variation of Helvetica though this is platform dependent. Therefore unlike HTML Java does permit you to choose your fonts. Java implementations are guaranteed to have a serif font like Times which can be accessed along with the name "Serif", a monospaced font like courier which can be accessed along with the name "Mono", and a sans serif font like Helvetica which can be accessed along with the name "SansSerif".

The subsequent applet lists the fonts available on the system it's running on. It does this through using the getFontList() method from java.awt.Toolkit. This technique returns an array of strings containing the names of the available fonts. These might or may not be the similar as the fonts installed on your system. It's implementation dependent while or not all the fonts a system has are available to the applet.

import java.awt.*;
import java.applet.*;

public class FontList extends Applet {

private String[] availableFonts;

public void init () {

Toolkit t = Toolkit.getDefaultToolkit();
availableFonts = t.getFontList();

}

public void paint(Graphics g) {

for (int i = 0; i < availableFonts.length; i++) {
g.drawString(availableFonts[i], 5, 15*(i+1));
}
}

}

Request for Solution File

Ask an Expert for Answer!!
JAVA Programming: what is the role of fonts in java explain with
Reference No:- TGS0285271

Expected delivery within 24 Hours