1 import java.awt.*; 2 3 public class Centered extends java.applet.Applet 4 { 5 public void paint (Graphics g) 6 { 7 Font f = new Font("TimesRoman", Font.PLAIN, 24); 8 FontMetrics fm = getFontMetrics(f); 9 String s = "Today is your lucky day."; 10 11 setBackground(Color.cyan); 12 g.setFont(f); 13 int xstart = (size().width - fm.stringWidth(s))/2; 14 int ystart = ((size().height - fm.getHeight())/2) 15 + fm.getAscent() + fm.getLeading(); 16 g.drawString (s, xstart, ystart); 17 } 18 }