The Java Program: MuApplet01.java

  1 // MuApplet01.java -- Mu puzzle applet, version 1
  2 
  3 /*
  4    <applet code=MuApplet01.java width=300 height=300></applet>
  5 */
  6 
  7 import java.applet.Applet;
  8 import java.awt.*;
  9 
 10 class Derivation extends Canvas {
 11 
 12    String derivation [] = new String [10];
 13    int number = 0;
 14  
 15    public void paint(Graphics g) {
 16       g.setFont (new Font ("TimesRoman", Font.BOLD, 18));
 17       for (int i=0; i<number; i++) {
 18          g.drawString (i+" "+derivation[i], 40, 20+23*i);
 19       }
 20    }
 21 
 22 }
 23 
 24 public class MuApplet01 extends Applet {
 25 
 26    TextField text;
 27    Derivation d;
 28 
 29    public void init() {
 30       setLayout (new BorderLayout());
 31       text = new TextField (10);
 32       d = new Derivation ();
 33       add ("North", text);
 34       add ("Center", d);
 35       setBackground (Color.white); // Make applet window stand out
 36       d.derivation [d.number++] = "MI";
 37    }
 38 
 39    public boolean action (Event evt, Object arg) {
 40       if (evt.target == text) {
 41          String input = (String) arg;
 42          d.derivation [d.number++] = input;
 43          d.repaint();
 44          return true;
 45       } else {
 46          return false;
 47       }
 48    }
 49 }