Oracle

Code sample

This simple applet was started way back in '97.
Here's the complete source code.
/** Oracle.java
 *  copyright Software Cottage
 *  March 1st 1997
 *
 *  Changes:
 *      15/8/98    upgraded to the 1.1 event model
 *      16/8/98    data is now loaded from server
 */
import java.applet.Applet;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseAdapter;
import java.net.*;
import java.io.*;

public class Oracle extends Applet
{
  Image oracle;
  Label label;
  boolean talking = false;
  String[] suggestions;
  int numSugs;
  int currentSuggestion;

  class OracleMouseAdapter extends MouseAdapter
  {
    public void mousePressed(MouseEvent e)
    {
      // suggest an answer/clear the label
      talking = !talking;

      if (talking)
      {
        // throw the dice to get a random suggestion index
        currentSuggestion = ((int)(Math.random()*1000))%numSugs;

        // update the suggestion label
        label.setText(suggestions[currentSuggestion]);

        // display the new suggestion
        label.setVisible(true);
      }
      else
      {
        // hide the suggestion label
        label.setVisible(false);
      }
    }
  }

  public void init()
  {
    // set the applet's underlying colours
    setBackground(Color.white);
    setForeground(Color.blue);

    // set up a hand cursor
    setCursor(new Cursor(Cursor.HAND_CURSOR));

    // load the oracle image
    oracle = getImage(getDocumentBase(), getParameter("image"));

    // attach a mouse adapter to listen for mouse clicks
    addMouseListener(new OracleMouseAdapter());

    // load the oracle's wisdom
    loadWisdom();

    // and initialise the suggestion label
    initLabel();
  }

  public void paint(Graphics g)
  {
    // draw the oracle
    g.drawImage(oracle, 0, 0, this);

    // tell the user what to do
    showStatus("Click on the Oracle for advice");
  }

  private void loadWisdom()
  {
    // load the suggestion file into an array
    URL url = null;

    try
    {
      // form the URL of the file
      try
      {
        url = new URL(getDocumentBase(),"oracle.dat");
      }
      catch (MalformedURLException mue) {}

      // attach a buffered reader to the file
      URLConnection uc = url.openConnection();
      InputStreamReader isr = new InputStreamReader(uc.getInputStream());
      BufferedReader br = new BufferedReader(isr);

      // mark the beginning of the file
      br.mark(1000000);

      // count the number of suggestions in the file
      String line;
      while ((line = br.readLine()) != null)
        if (!line.equals("")) // trap blank last line (if any)
          numSugs++;

      // return to the beginning
      br.reset();

      // load the file into the suggestions array
      suggestions = new String[numSugs];
      for (int i = 0; i < numSugs; i++)
        suggestions[i] = br.readLine();

      br.close(); isr.close();
    }
    catch(IOException e)
    {
      System.out.println("Error reading oracle.dat: " + e);
      System.exit(1);
    }
  }
  private void initLabel()
  {
    // initialise the suggestion label
    label = new Label();
    label.setAlignment(Label.CENTER);
    label.setFont(new Font("Arial", Font.PLAIN, 14));
    label.setBackground(new Color(0x33, 0x33, 0x33)); // dark grey
    label.setForeground(Color.white);

    // set the applet's layout to null
    // so we can manually position the label control
    setLayout(null);

    // add the label to the container
    add(label);

    // position it on the page
    label.setBounds(20, 340, 248, 20);

    // hide it
    label.setVisible(false);
  }
}