/** Assignment:  Modify this file (previously ManyFonts
 ** on p. 202 of the text) by changing it to center each 
 ** of the different fonts horizontally, and to give each        
 ** a different color. Make sure to check out SimpleApplet3 
 ** first, which shows how to do these things.  Also, edit 
 ** MyApplet6.html in the  project directory, to run the
 ** MyApplet6.class file you create, and to look nice.   **/                                                               

import java.awt.*;
import java.applet.Applet;

public class MyApplet6 extends java.applet.Applet 
  
      {Font f = new Font("TimesRoman", Font.PLAIN, 18);
       Font fb = new Font("TimesRoman", Font.BOLD, 18);
       Font fi = new Font("TimesRoman", Font.ITALIC, 18);
       Font fbi = new Font("TimesRoman", Font.BOLD + Font.ITALIC, 18);
       
       FontMetrics font1m = getFontMetrics(f);         // The different font metrics.
       FontMetrics font2m = getFontMetrics(fb);
       FontMetrics font3m = getFontMetrics(fi);
       FontMetrics font4m = getFontMetrics(fbi);
       
       String text1 = "This is a plain font.";
       String text2 = "This is a bold font.";
       String text3 = "This is an italic font.";
       String text4 = "This is a bold italic font.";
       
    public void init()	
       {setBackground(Color.black);}          // Change the gray background to black.
						      						     			         				     
    /** CREATE THE CURRENT FRAME **/

    public void paint(Graphics g)
       {int xstart1 = (size().width - font1m.stringWidth(text1))/2; // Center text horizontally.
	int xstart2 = (size().width - font2m.stringWidth(text2))/2;
	int xstart3 = (size().width - font3m.stringWidth(text3))/2; 
	int xstart4 = (size().width - font4m.stringWidth(text4))/2; 
	
	int font1ht = font1m.getHeight();                          // Get font heights.
	int font2ht = font2m.getHeight();
	int font3ht = font3m.getHeight();
	int font4ht = font4m.getHeight();           
	           
							    // Compute spacing between lines.
	int yspacing = (size().height - (font1ht + font2ht +font3ht + font4ht))/5; 
							       
	
	int ystart1 = yspacing + font1m.getAscent();               // Align text vertically.
	int ystart2 = ystart1 + font1m.getDescent() + yspacing + font2m.getAscent();
	int ystart3 = ystart2 + font2m.getDescent() + yspacing + font3m.getAscent();
	int ystart4 = ystart3 + font3m.getDescent() + yspacing + font4m.getAscent();
	        
	g.setColor(Color.blue);
	g.setFont(f);
	g.drawString(text1,xstart1,ystart1);
	g.setColor(Color.yellow);
	g.setFont(fb);
	g.drawString(text2,xstart2,ystart2);
	g.setColor(Color.red);
	g.setFont(fi);
	g.drawString(text3,xstart3,ystart3);
	g.setColor(Color.green);
	g.setFont(fbi);
	g.drawString(text4,xstart4,ystart4);
	}
    }