// Copyright Andy Deck 2002, GNU Public License 2
// With the notable provision that any commercial use
// must be approved by the copyright holder.
import java.awt.*;
import java.awt.*;
import java.io.*;
import java.net.URL;
import java.net.URLEncoder;

public class load extends Module
    implements Runnable
{

    public load()
    {
        x = 5;
        y = 12;
        fm = null;
        f = null;
        url = null;
    }

    public void init(lexicon lexicon1, String s)
    {
        super.lex = lexicon1;
        url = s;
        Graphics g = lexicon1.img.getGraphics();
        int i = y;
        f = new Font("Helvetica", 0, i);
        g.setFont(f);
        fm = g.getFontMetrics();
        g.dispose();
        start();
    }

    public boolean drawString(Graphics g, String s)
    {
        g.setFont(f);
        for(int j = 0; j < s.length(); j++)
        {
            String s1 = s.substring(j, j + 1);
            if(x > Module.WIDTH - 10)
            {
                x = 5;
                if(y < Module.HEIGHT - fm.getHeight())
                    y += fm.getHeight() + fm.getLeading() + 2;
                else
                    return false;
            }
            g.setColor(Color.black);
            g.drawString(s1, x, y + 1);
            g.drawString(s1, x, y);
            g.drawString(s1, 1 + x, y);
            g.drawString(s1, 2 + x, y);
            g.drawString(s1, 2 + x, y + 1);
            g.drawString(s1, 2 + x, y + 2);
            g.drawString(s1, 1 + x, y + 2);
            g.drawString(s1, x, y + 2);
            g.setColor(Color.white);
            g.drawString(s1, 1 + x, y + 1);
            int i = fm.stringWidth(s1);
            x += i;
        }

        g.dispose();
        return true;
    }

    public void stop()
    {
        t = null;
    }

    public void start()
    {
        t = new Thread(this, "load");
        t.start();
    }

    public void run()
    {
        try
        {
            URL url1 = new URL(Module.documentBase + "load.php?url=" + URLEncoder.encode(url));
            Graphics g = super.lex.img.getGraphics();
            setRenderMode(g);
            g.setColor(Color.white);
            for(int i = 0; i < Module.HEIGHT; i += fm.getHeight() + fm.getLeading() + 2)
                g.fillRect(0, i, Module.WIDTH, 4);

            g.setColor(Color.black);
            DataInputStream datainputstream = new DataInputStream(new BufferedInputStream(url1.openStream()));
            String s;
            while((s = datainputstream.readLine()) != null) 
            {
                if(super.abort || !drawString(g, s))
                    break;
                super.repaint();
            }
            g.dispose();
            datainputstream.close();
        }
        catch(Exception exception)
        {
            System.out.println(exception);
        }
    }

    private volatile Thread t;
    int x;
    int y;
    FontMetrics fm;
    Font f;
    String url;
}
