Oracle
Code sample
This simple applet was started way back in '97.
Here's the complete source code.
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)
{
talking = !talking;
if (talking)
{
currentSuggestion = ((int)(Math.random()*1000))%numSugs;
label.setText(suggestions[currentSuggestion]);
label.setVisible(true);
}
else
{
label.setVisible(false);
}
}
}
public void init()
{
setBackground(Color.white);
setForeground(Color.blue);
setCursor(new Cursor(Cursor.HAND_CURSOR));
oracle = getImage(getDocumentBase(), getParameter("image"));
addMouseListener(new OracleMouseAdapter());
loadWisdom();
initLabel();
}
public void paint(Graphics g)
{
g.drawImage(oracle, 0, 0, this);
showStatus("Click on the Oracle for advice");
}
private void loadWisdom()
{
URL url = null;
try
{
try
{
url = new URL(getDocumentBase(),"oracle.dat");
}
catch (MalformedURLException mue) {}
URLConnection uc = url.openConnection();
InputStreamReader isr = new InputStreamReader(uc.getInputStream());
BufferedReader br = new BufferedReader(isr);
br.mark(1000000);
String line;
while ((line = br.readLine()) != null)
if (!line.equals("")) // trap blank last line (if any)
numSugs++;
br.reset();
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()
{
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);
setLayout(null);
add(label);
label.setBounds(20, 340, 248, 20);
label.setVisible(false);
}
}