/**
 *
 *  ReadsFromURL.java -- a trivial applet demonstrating
 *    how to read server-side data via a URL stream
 *
 *  The applet also uses a bit of graphics with the
 *    heavyweights.  The results don't redraw well,
 *    but look ok.
 *
 */

import java.awt.*;
import java.applet.*;
import java.net.*;
import java.io.*;

public class ReadsFromURL extends Applet
{
    /*=======================================================*/
    /*               I N S T A N C E   V A R S               */
    /*=======================================================*/

    protected URL fileURL;
    protected String result;
    protected TextArea ta;
    protected int off = 15;

    /*=======================================================*/
    /*               C O N S T R U C T O R S                 */
    /*=======================================================*/
    
    /**
     *  Default constructor
     *     
     */
    public ReadsFromURL()
    {
	// default constructor for some fussy VMs
    }


    /**
     *  initialize the applet
     *
     *  @see#readInURL() -- called to initialize data
     */

    public void init()
    {    
        setBackground(Color.lightGray);
	ta = new TextArea();
        ta.setFont(new Font("Courier", Font.BOLD, 12));

        /*
         *  Set server to read applet source code
         */
	try    
	    {
		fileURL = new URL 
                    (getCodeBase() + "/ReadsFromURL.java");
	    }    
	catch(MalformedURLException e)
	    {
	    }

        /*
         *  Layout the container
         */
        this.setLayout(null); // ack!
	add(ta);
        ta.setBounds(getSize().width/8, getSize().height/8,
                     getSize().width*3/4, getSize().height*3/4);

	readInURL();
    }// init
    
    /*=======================================================*/
    /*                       P A I N T                       */
    /*=======================================================*/

    /**
     *   paint the applet, including borders, bevels, and
     *      screws
     *   
     */

    public void paint(Graphics g)
    {
        // bevels        
        for (int w=0; w<getSize().width; w+=12)
            {
                for (int h=0; h<getSize().height; h+=12)
                    {
                        g.setColor(Color.white);
                        g.drawLine(w,h,w+1,h);
                        g.drawLine(w+1,h,w+5,h+4);
                        g.drawLine(w+8,h+10,w+12,h+6);
                        g.drawLine(w+12,h+6,w+13,h+6);
                        g.setColor(Color.darkGray);
                        g.drawLine(w,h+2,w+5,h+7);
                        g.drawLine(w+5,h+7,w+6,h+7);
                        g.drawLine(w+9,h+12,w+13,h+8);
                    }
            }
        
        /* borders */
        g.setColor(Color.gray);
        g.fillRect(0, 0, getSize().width, off);
        g.fillRect(0, 0, off, getSize().height);
        g.fillRect(getSize().width-off, 0, off, getSize().height);
        g.fillRect(0,getSize().height-off, getSize().width, off);
        
        g.fillRect(0,0,2*off, 2*off);
        g.fillRect(getSize().width-2*off,0, 2*off, 2*off);
        g.fillRect(0, getSize().height-2*off, 2*off, 2*off);
        g.fillRect(getSize().width-2*off, getSize().height-2*off, 2*off, 2*off);

        int dX, dY;
        int x1=off; int x2=2*off; int x3=getSize().width-(1+2*off);
        int x4=getSize().width-(1+off);
        
        int y1=off; int y2=2*off; int y3=getSize().height-(1+2*off);
        int y4=getSize().height-(1+off);

        g.setColor(Color.black);
        g.drawLine(x4,y2,x3,y2); g.drawLine(x3,y1,x2,y1);
        g.drawLine(x2,y1,x2,y2); g.drawLine(x2,y2,x1,y2);
        g.drawLine(x1,y2,x1,y3); g.drawLine(x2,y3,x2,y4);
        
        g.setColor(Color.white);
        g.drawLine(x3,y1,x3,y2); g.drawLine(x4,y2,x4,y3);
        g.drawLine(x4,y3,x3,y3); g.drawLine(x3,y3,x3,y4);
        g.drawLine(x3,y4,x2,y4); g.drawLine(x2,y3,x1,y3);
        
        //raised outer edge
        g.setColor(Color.black);
        g.drawRect(0,0, getSize().width-1, getSize().height-1);
        g.setColor(Color.white);
        g.drawLine(0,0, getSize().width-1,0);
        g.drawLine(0,0,0, getSize().height-1);
        
        //screws
        for (int x=2;x-->0;)
            {
                for (int y=2;y-->0;)
                    {
                        dX=(x==1)?0:x3;
                        dY=(y==1)?0:y3;
                        g.setColor(Color.white);
                        g.drawArc(dX+off/2+1,dY+off/2+1,
                                    (int)1.5*off,(int)1.5*off,90,100);
                        g.drawLine(dX+(int) (1.25*off)+2,
                                     dY+(int) (.75*off),
                                     dX+(int) (.75*off)-1,
                                     dY+(int) (1.5*off)-1);
                        g.setColor(Color.black);
                        g.drawOval(dX+off/2,dY+off/2,
                                     (int)1.5*off,(int)1.5*off);
                        g.drawLine(dX+(int) (1.25*off)+1,
                                     dY+(int) (.75*off)-1,
                                     dX+(int) (.75*off)-2,
                                     dY+(int) (1.5*off)-2);                        
                    }
            }
    }// paint

    
    /*=======================================================*/
    /*                  N E T W O R K I N G                  */
    /*=======================================================*/

    /**
     *
     *  Read the URL -- this method reads in the URL's stream,
     *    wrapping it in a BufferdReader
     *
     */

   public void readInURL()
    {
        try
        {
            String strTemp;
            java.io.InputStream input = fileURL.openStream();
            
            BufferedReader buff = new BufferedReader
                (new InputStreamReader(input));
            while((strTemp = buff.readLine()) != null)
                ta.append(strTemp+"\n");

            // be a good net neighbor and close the stream
            buff.close();
        }

        catch(IOException darn)
        {
            showStatus("Exception: " + darn.toString());
        }
    }// readInURL

}// class ReadsFromURL