/*
    A basic extension of the java.applet.Applet class
 */

import java.awt.*;
import java.applet.*;
import java.net.*;
import java.io.*;

public class CounterClient extends Applet implements Runnable {

    Thread m_CounterClient=null;

    public void start() {
        if(m_CounterClient==null) {
            m_CounterClient=new Thread(this);
            m_CounterClient.start();
        }
    }

    public void run() {
        while(true) {
            repaint();
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                stop();
            }
        }
    }

    public void stop() {
		if (m_CounterClient != null) {
			m_CounterClient.stop();
			m_CounterClient = null;
		}
	}


	public void init() {
		super.init();

		//{{INIT_CONTROLS
		//setLayout(null);
		addNotify();
		resize(100,40);
		CounterLabel = new java.awt.Label("",Label.CENTER);
		CounterLabel.reshape(0,0,100,40);
		CounterLabel.setFont(new Font("Helvetica", Font.BOLD, 24));
		CounterLabel.setForeground(new Color(16777215));
		CounterLabel.setBackground(new Color(255));
		add(CounterLabel);
		setBackground(new Color(255));
		//}}

		// Get the label
        GetCounter();
	}

    public boolean handleEvent(Event event) {
		return super.handleEvent(event);
	}

	//{{DECLARE_CONTROLS
	java.awt.Label CounterLabel;
	//}}

    private void GetCounter() {
      try {
          // Open a new socket connection
          URL data=new URL("http://www.suba.com/~spullara/counter");

          // Get the input stream from the socket and convert
          // it into a data input stream
          DataInputStream Input=
        	new DataInputStream(data.openStream());

          // Read the results from the server until the socket closes
          String line=Input.readLine();

          // Set the Label
          CounterLabel.setText(line);

        } catch (IOException e) {
	        System.out.println(e);
        }
    }
}

