import java.awt.*;
import java.util.*;
import java.util.zip.*;
import java.io.*;
import java.net.URL;
public class Fortune extends java.applet.Applet implements Runnable {
private int flags;
private final int RANDOM = 1;
private final int ORDERED = 2;
private final int ROTATED = 4;
private Random rand;
private Vector data = new Vector(5);
private Thread fortuneThread;
public void run() {
int version;
int numstr;
int longlen;
int shortlen;
byte[] delim = new byte[4];
try {
data.removeAllElements();
data.addElement("");
data.addElement("Lade Zitat vom Server ...");
repaint(10);
String fortune = "";
Vector fortunes = new Vector(5);
StringTokenizer st = new StringTokenizer(getParameter("FORTUNES"));
while (st.hasMoreTokens()) {
fortunes.addElement( st.nextToken());
}
Double n = new Double(Math.floor(fortunes.size() * rand.nextDouble()));
fortune = (String)fortunes.elementAt(n.intValue());
URL url = new URL( getCodeBase(), fortune + ".zip");
ZipInputStream zip = new ZipInputStream( url.openStream());
ZipEntry datfile = zip.getNextEntry();
if (! datfile.getName().equals( fortune + ".dat")) {
System.err.println( "datfile not found in zipfile");
return;
}
DataInputStream in = new DataInputStream( zip);
version = in.readInt();
if (version != 1) {
System.err.println( "Wrong version of dat file");
return;
}
numstr = in.readInt();
longlen= in.readInt();
shortlen = in.readInt();
flags = in.readInt();
in.readFully( delim);
Double datOffset = new Double(Math.floor(numstr * rand.nextDouble()));
for (int i = 0; i < datOffset.intValue(); ++i) in.readInt();
int fileOffset = in.readInt();
ZipEntry textfile = zip.getNextEntry();
if (! textfile.getName().equals( fortune)) {
System.err.println( "textfile not found in zipfile");
return;
}
data.removeAllElements();
DataInputStream tin = new DataInputStream( zip);
for (int i = 0; i < fileOffset; ++i) tin.readByte();
String line = "";
String end = new String( delim, 0, 1);
while (! line.equals(end)) {
data.addElement( line);
line = tin.readLine();
line = line.replace( '\t', ' ');
}
repaint();
}
catch (Exception e) {
System.err.println( e);
e.printStackTrace();
return;
}
}
public void init() {
rand = new Random();
int size = 14;
try {
size = Integer.parseInt(getParameter("SIZE"), 10);
}
catch (NumberFormatException e) {
}
Font font = new java.awt.Font("Arial", Font.BOLD, size);
setFont(font);
setBackground( new Color(0x5cacee));
fortuneThread = new Thread(this);
fortuneThread.start();
}
public void paint( Graphics g) {
if (data.size() > 1) {
int x = 5;
int h = g.getFont().getSize();
g.setColor(Color.white);
for (int i = 1, y = h+5; i < data.size(); ++i, y+=h) {
g.drawString( (String)data.elementAt(i), x, y);
}
}
}
public boolean mouseDown( Event e, int x, int y) {
if (! fortuneThread.isAlive()) {
fortuneThread = new Thread(this);
fortuneThread.start();
}
return true;
}
}