// beginner's guide to applets:
//
// K.Becker

// you will the following libraries

import java.awt.*;// awt = Abstract Windows Toolkit

import java.applet.Applet; // to run applets

import java.net.*; // to get at stuff over the net
import java.io.*;  // for reading and stuff

import java.lang.Character;
import java.lang.String;

// the name of your applet (Readit) must be the same as the
// file that contains it (Readit.java) and must also be the same 
// as the name used to refer to it in the HTML file
// <APPLET CODE="Readit.class"
//     CODEBASE="http://www.cpsc.ucalgary.ca/~becker/461/Asst/SearchEngine">
//
// the CODEBASE is the URL of the directory containing
//      1. the HTML page 
//      2. the '.class' byte-code
//      3. the data-file you have generated

public class Readit extends Applet {

    // the name of the data file to be read
    private static final String urlName =
       	"http://www.cpsc.ucalgary.ca/~becker/461/Asst/SearchEngine/Resources/text.bin";

    String s;

    int i,j,k;

    // init is like the constructor; it gets called automatically when the
    // applet is first loaded and an instance of the READIT object is
    // created. It gets called once and only once
    // do your initializations here
    public void init() {
	s = new String( "" );
	j = 25;
	k = 25;

	// loading the file
       	try {
	    doit();
	} catch (Exception e) {}

    } // end init
  
    // this method gets called automatically right after INIT
    // this is essentially your main program
    public void start() {
    } // end start
    
    // called when the browser leaves the page
    public void stop() {	
    } // end stop
    
    // called automatically to draw the picture
    // there is also a REDRAW that clears the screen first 
    //     and then calls PAINT
    public void paint (Graphics g) {

	// draws the given string starting at X(column), Y(row)
	// the reference point for the thing you are drawing is
	// the lower-left corner
	g.drawString( "OK...",1,10);
	g.drawString( s, 10, 30 );
    
    } // end paint

    // read the data
    public void doit () throws Exception {

        try {
	    // for opening a network file
	    URL binfile = new URL( urlName );
	    // allows for binary input in blocks or by byte
	    BufferedInputStream in =
		new BufferedInputStream( binfile.openStream() );

	    int b = 0;
	    s = "START::";
	    // read will return -1 at end-of-file
	    while( ( b = in.read() ) >= 0 ) {
		// reads in a single signed byte at a time
		// this example converts it to character to display

		s = s + (char)b;
		// question: what's happenning to the white space?
	    }
	    in.close();
	    s = s + "::END";
	}
	catch( IOException e ) { System.err.println( e.toString() ); }
    } // end doit   

} // end applet
