// reads random quotes from a file and does a nice fade in/out
//loads a new quote each time you click on the quote.

import java.awt.*;
import java.net.*;
import java.io.*;
import java.util.Random;
import java.util.StringTokenizer;

public class Quoter extends java.applet.Applet implements Runnable{

    private static boolean newQuote = false;
    private Thread quoter;
    private String curText[] = new String[5]; 
    private String newText[] = new String[5];
    private URL url;
    private Object urlContent;
    private InputStream inputStream;
    private DataInputStream dataInput;
    private int rndQuote;
    private Font textFont;
    private StringTokenizer st;
    private int charWidth;
    private int width;
    private String text = "";
    private boolean init = false;
    private boolean lock = false;
    
    public void init() {
        setBackground(Color.black);
        textFont = new Font("Helevetica", Font.PLAIN, 16);
        //get the document and read a quote
        getQuote();
        //stuff for managing the quote length... damn you Jim!  :-)
        Dimension d = this.size();
        width = d.width; //get the width of the applet so we can put line
breaks in there
        for (int x = 0; x < 5; x++) {
            curText[x] = " ";
            newText[x] =" ";
        }
    }


    public void getQuote() {
        new Random();

        try {

            url = new URL(getCodeBase(),"quotes.txt");

            inputStream = url.openStream();
            dataInput = new DataInputStream(inputStream);
            
            rndQuote = (int)(Math.random()*707); 
            
            int line = 0;
            
            while((text = dataInput.readLine()) != null)
            {
                        if (line == rndQuote) {
                        break;
                    }
                    line++;
            }
        }
        catch (MalformedURLException e) {
            System.out.println(e.toString());
        }

        catch (IOException ioe) {
            System.out.println(ioe.toString());
        }
    }
    
    
    //
    // FORMATQUOTE starts here **************************
    //
    public void formatQuote(Graphics g) {
        String temp[] = new String[5]; // a temp string to manipulate for
adding carrige returns
        for (int q = 0; q < 5; q++) { temp[q] = "";}   

        String token;
        StringTokenizer st = new StringTokenizer(text);
         
        char chars[];
        int qLength = 0;    
        
        int numLines = 1;
        while (st.hasMoreTokens()) {
            token = st.nextToken();
            
            //get the length of this token
            chars = token.toCharArray();
            for (int t = 0; t<chars.length; t++) 
                qLength += g.getFontMetrics().charWidth(chars[t]);
            
                
            //check to see if the line is to long
            //if it is then this token goes to the next string and we start over
            //counting pixels again
            if (qLength + g.getFontMetrics().charWidth(' ') > width-15) {
                numLines++;
                temp[numLines-1] = token;
                qLength = 0;
            }
            
            //the current token is being added to the current string
            //add space to the qLength in pixels with another call to
getFontMetrics
            else  {
                qLength += g.getFontMetrics().charWidth(' ');
                temp[numLines-1] = temp[numLines-1].concat(" ");
                temp[numLines-1] = temp[numLines-1].concat(token);
            }            
        }
        
        curText = temp;
    }
    //END FORMATQUOTE


    public boolean mouseDown(Event evt, int x, int y) {
        lock = true;
        newQuote = true;
        newText = curText;
        getQuote();
        repaint();
        return true;
    }

    public void getNewQuote() {
        getQuote();
        repaint();
    }

    public void paint(Graphics g) {
        if (!init) {
            charWidth = g.getFontMetrics().getMaxAdvance();
            init = true;
        }
        
        g.setFont(textFont);        
        formatQuote(g);
        
        int spacey = 20;
        if (newQuote) {
            //fade out
            for (int x = 255; x >=0; x-=16) {
                g.setColor(new Color(x, x, x));
               for (int y = 0; y < 5; y++) {
                    if (newText[y] != null) g.drawString(newText[y], 10,
spacey + y*charWidth /*g.getFontMetrics().getHeight()*/);
                    //System.out.println(newText[y]);
                }

                try {
                    quoter.sleep(100);
                }
                catch (InterruptedException e) {}
            }
            //fade in new
            for (int x = 0; x <=255; x+= 16) {
                g.setColor(new Color(x, x, x));
                for (int y = 0; y < 5; y++) {
                     if (curText[y] != null) g.drawString(curText[y], 10,
spacey + y*charWidth/* g.getFontMetrics().getHeight()*/);
                    System.out.println(curText[y]);
                }

                try {
                    quoter.sleep(100);
                }
                catch (InterruptedException e) {}
            }
            newQuote = false;
        }
        //fade in first quote
        else {
                for (int x = 0; x <=255; x+= 16) {
                    g.setColor(new Color(x, x, x));
                    
                    for (int y = 0; y < 5; y++) {
                         if (curText[y] != null) g.drawString(curText[y],
10, spacey + y*charWidth /*10 + g.getFontMetrics().getHeight()*/);
                    }
    
                    try { quoter.sleep(100); } catch (InterruptedException
e) { }
                } 
        }
        lock = false;
    }

    public void start() {
        if (quoter == null) {
        quoter = new Thread(this);
        quoter.start();
        }
    }

    public void stop() {
        if (quoter != null) {
        quoter.stop();
        quoter = null;
        }
    }


    public void run() {
       while (true) {
            try { quoter.sleep(25000); } catch (InterruptedException e) { }
            if (!lock )  getNewQuote();
        }    
    }

}



