To display text(write the text), you must first select a font. A font is specified by its names, such as “Arial”, the style(plain, bold, italic, or bold italics), and the point size. A font is an object in Java, so we need to create it via a call to new before we can use it. The setColor methods call selects a Colour that is used for all subsequent drawing operations.
Java Program to set the Font And Color of some Text
import java.applet.*;
import java.awt.*;
public class Fonts extends Applet
{
public void paint(Graphics g)
{
Font f1=new Font(“TimesRoman”,Font.PLAIN,20);//f1 is used for writing(TimesRoman) plain text
Font f2=new Font(“Courier”,Font.BOLD,22);//f2 is used for writing(Courier) Bold text
Font f3=new Font(“Arial”,Font.ITALIC,18);//f3 is used for writing (Arial)Italics text
Font f4=new Font(“Times”,Font.BOLD+Font.ITALIC,24);
g.setColor(Color.blue);
g.setFont(f1);
g.drawString(“Times Roman “,20,40);
g.setColor(Color.pink);
g.setFont(f2);
g.drawString(“Courier bold “,20,60);
Color c=new Color(0,255,255);
g.setColor(c);
g.setFont (f3);
g.drawString(“Arial “,20,80);
g.setColor(Color.gray);
g.setFont(f4);
g.drawString(“Times bold “,20,120);
}}
//<applet code=”Fonts.class” height=500 width=500></applet>
Explanation….
The setColor method takes a parameter of type Color.
You can create an object of type Color by using the class variables available with the Color class or by specifying the red, green, and blue components.
g.setColor(Color.gray);
g.setColor(new Color(0,127,122));
By using these methods we can set the Color in the Applet.