/** 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.   **/                                                               

/** USE EXISTING JAVA CLASSES **/ 
  
import java.awt.*; 
import java.applet.Applet; 
  
/** EXTEND THE JAVA APPLET CLASS **/ 
  
public class MyApplet6 extends Applet 
   {Font font1 = new Font("TimesRoman",Font.PLAIN,18);
    Font font2 = new Font("TimesRoman",Font.BOLD,18);
    Font font3 = new Font("TimesRoman",Font.ITALIC,18);
    Font font4 = new Font("TimesRoman",Font.BOLD+Font.ITALIC,18); 
  
    FontMetrics font1m = getFontMetrics(font1);
    FontMetrics font2m = getFontMetrics(font2); 
    FontMetrics font3m = getFontMetrics(font3); 
    FontMetrics font4m = getFontMetrics(font4); 
  
    String text = "Java is densely populated."; 
  
    public void init() 
       {setBackground(Color.green);} 
  
   /** CREATE THE CURRENT FRAME **/ 
  
    public void paint(Graphics g) 
       {int xstart1 = (size().width - font1m.stringWidth(text))/2; // center horizontally. 
         int xstart2 = (size().width - font2m.stringWidth(text))/2; 
         int xstart3 = (size().width - font3m.stringWidth(text))/2; 
         int xstart4 = (size().width - font4m.stringWidth(text))/2; 
  
         int font1A = font1m.getAscent(); 
         int font1D = font1m.getDescent();
         int font1L = font1m.getLeading();
         int font1H = font1m.getHeight();
  
         int font1Awhite = font1D - font1L; 
         int font1Atext = font1A - font1Awhite;
         int font1text = font1Atext + font1D; 
  
         int font2A = font2m.getAscent(); 
         int font2D = font2m.getDescent(); 
         int font2L = font2m.getLeading(); 
         int font2H = font2m.getHeight(); 
  
         int font2Awhite = font2D - font2L; 
         int font2Atext = font2A - font2Awhite;
         int font2text = font2Atext + font2D;
  
         int font3A = font3m.getAscent(); 
         int font3D = font3m.getDescent(); 
         int font3L = font3m.getLeading(); 
         int font3H = font3m.getHeight(); 
  
          int font3Awhite = font3D - font3L; 
          int font3Atext = font3A - font3Awhite; 
          int font3text = font3Atext + font3D; 
  
          int font4A = font4m.getAscent(); 
          int font4D = font4m.getDescent(); 
          int font4L = font4m.getLeading(); 
          int font4H = font4m.getHeight(); 
  
          int font4Awhite = font4D - font4L; 
          int font4Atext = font4A - font4Awhite; 
          int font4text = font4Atext + font4D;
          int yspacing = 
               (size().height - (font1text + font2text + font3text + font4text))/5; 
  
                // align vertically. 
          int ystart1 = yspacing + font1Atext;
          int ystart2 = ystart1 + font1D + yspacing + font2Atext; 
          int ystart3 = ystart2 + font2D + yspacing + font3Atext; 
          int ystart4 = ystart3 + font3D + yspacing + font4Atext; 
  
         g.setColor(Color.blue); 
         g.setFont(font1); 
         g.drawString(text,xstart1,ystart1); 
         g.setColor(Color.red);
	 g.setFont(font2); 
         g.drawString(text,xstart2,ystart2); 
         g.setColor(Color.black);
	 g.setFont(font3); 
         g.drawString(text,xstart3,ystart3); 
         g.setColor(Color.white);
	 g.setFont(font4); 
         g.drawString(text,xstart4,ystart4); 
         } 
   } 