/*
   < Applet code = "ShowFonts"
           width = 400 height = 200>
   < /applet>
 */

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

public class ShowFonts extends Applet implements ActionListener {
   TextField GetString;
   TextField GetSize;
   String[] fontlist;
   String FontShow = "ABC";
   int FontSize = 10;
   int delta_y = 15;

   public void init() {
      Panel p = new Panel();
      setLayout(new BorderLayout());
      p.setLayout(new GridLayout(1,4));
      add("South", p);
      p.add(new Label("String:",Label.RIGHT));
      p.add(GetString = new TextField());
      p.add(new Label("Size:",Label.RIGHT));
      p.add(GetSize = new TextField());
      GetSize.setText(""+FontSize);
      GetString.setText(FontShow);
      GetString.addActionListener(this);
      GetSize.addActionListener(this);
      get_fonts();
      setBackground(Color.cyan);
      repaint(1);
   }

   private void get_fonts() {
      Toolkit tk;
      tk = Toolkit.getDefaultToolkit();
      fontlist = tk.getFontList();
   }

   private int min_increment(Font f, int old_inc) {
      FontMetrics fm;
      int inc;
      fm = getFontMetrics(f);
      inc = fm.getMaxAscent() + fm.getMaxDescent() + 2;
      if (inc < old_inc) return old_inc;
      return inc;
   }

   public void paint(Graphics g) {
      int lm = 2;
      int delta_x;
      int new_inc;
      Graphics g1;
      Font f;

      delta_x = getBounds().width/5;

      f = g.getFont();
      new_inc = min_increment(f,0);
      
      g1 = g.create();
      g.drawString("Name",lm,delta_y);
      g.drawString("Plain",lm+delta_x,delta_y);
      g.drawString("Bold",lm+2*delta_x,delta_y);
      g.drawString("Italics",lm+3*delta_x,delta_y);
      g.drawString("Bold Italics",lm+4*delta_x,delta_y);
      for (int i=0;i < fontlist.length;i++) {
           g.drawString(fontlist[i],lm,(i+2)*delta_y);

           f = new Font(fontlist[i],Font.PLAIN,FontSize);
           new_inc = min_increment(f,new_inc);
           g1.setFont(f);
           g1.drawString(FontShow,lm+delta_x,(i+2)*delta_y);

           f = new Font(fontlist[i],Font.BOLD,FontSize);
           new_inc = min_increment(f,new_inc);
           g1.setFont(f);
           g1.drawString(FontShow,lm+2*delta_x,(i+2)*delta_y);

           f = new Font(fontlist[i],Font.ITALIC,FontSize);
           new_inc = min_increment(f,new_inc);
           g1.setFont(f);
           g1.drawString(FontShow,lm+3*delta_x,(i+2)*delta_y);

           f = new Font(fontlist[i],Font.BOLD|Font.ITALIC,FontSize);
           new_inc = min_increment(f,new_inc);
           g1.setFont(f);
           g1.drawString(FontShow,lm+4*delta_x,(i+2)*delta_y);
      }
      if (delta_y != new_inc) {
         delta_y = new_inc;
         repaint(100);
      }
   }

   private void reset_it() {
      FontSize = Integer.parseInt(GetSize.getText());
      FontShow = GetString.getText();
      repaint();
   }

   public void actionPerformed (ActionEvent e) {
      if (e.getSource() == GetSize) {
         reset_it();
      }  
      if (e.getSource() == GetString) {
         reset_it();
      }  
   }

}