import java.net.*;
import java.io.*;
import java.awt.*;
import java.lang.*;
import java.util.*;
import java.text.*;

public class WormWavesApplet extends java.applet.Applet {
	public int W = 500;
	public int H = 200;

	public final Color bg = Color.white;
	boolean threadSuspended = false;
	Image im;
	Graphics offscreen;
	WormWaves wormwaves;
	
	public void init() {
		resize(W, H);
		try {
			im = createImage(W, H);
			offscreen = im.getGraphics();
		} catch (Exception e) {
			offscreen = null;
		}
	}

	public void start() {
		if (wormwaves == null) {
			wormwaves = new WormWaves(this);
			wormwaves.start();
		}
		repaint();
	}

	public void stop() {
		wormwaves.stop();
	}
	
	public void update(Graphics g) {
		paint(g);
	}

	public void paint(Graphics g) {
		if (offscreen != null) {
			paintApplet(offscreen);
			g.drawImage(im,0,0,this);
		} else {
			paintApplet(g);
		}
	}

	public void paintApplet(Graphics g) {
		boolean changed = false;
		g.setColor(bg);
		g.fillRect(0, 0, W, H);
		try {
			wormwaves.paint(g);
		} catch(java.lang.NullPointerException e) {
			System.out.println("waves.paint barfs: " + e.getMessage());
		}
	}
}

class WormWaves implements Runnable {
	public Thread wavethread = null;
	public boolean changed = true;
	public int wavex = 0;
	public int wavey = 100;
	public int xlen = 500;
	public int ycurrent[];
	int yr=0, mo=0, da=0, hr=0, mn=0, sec=0;
	double dt=0.;
	Date start;

	URL u;
	InputStream is;
	DataInputStream dis;

	URL time;
	InputStream time_is;
	StreamTokenizer st;

	WormWavesApplet applet;

	public WormWaves(WormWavesApplet applet) {
		this.applet = applet;

	/*********** get wave data ************/
		try {
			u = new URL("http://www.seismo.unr.edu/ftp/pub/ichinose/JavaWorm/sac.html");
		} catch (MalformedURLException e) { }
		try {
			is = u.openStream();
		}  catch (java.io.IOException e) {
			System.out.println("No wave data in file");
		}
		dis = new DataInputStream(is);
		
	/********* get time stamp ***********/
		try {
			time = new URL("http://www.seismo.unr.edu/ftp/pub/ichinose/JavaWorm/time.html");
		} catch (MalformedURLException e) { }
		try {
			time_is = time.openStream();
		} catch (java.io.IOException e) {
			System.out.println("No time data in file");
		}

		ycurrent = new int[xlen + 1];
		for(int i = 0; i < xlen + 1; i++) {
			ycurrent[i] = 0;
		}
	}
	
	public void paint(Graphics g) {
		SimpleDateFormat utcformatter = new SimpleDateFormat("yy MM dd HH:mm:ss ");
		SimpleTimeZone utc = new SimpleTimeZone(0 * 60 * 60 * 1000, "UTC");
		utcformatter.setTimeZone(utc);

		g.setColor(Color.black);
		g.setFont( new Font("Dialog", Font.PLAIN,10));
		g.drawString( utcformatter.format(this.start),1,150 );

		for(int x = 0; x < xlen; x++) {
			g.drawLine(
				x + wavex,
				ycurrent[x] + wavey,
				x + wavex + 1,
				ycurrent[x+1] + wavey
			);
				ycurrent[x] = ycurrent[x+1];
		}
	}

	public void start() {
		if(wavethread == null)
		{
			wavethread = new Thread(this,"Waves");
			wavethread.start();
		}
	}

	public void stop() {
		wavethread = null;
	}

	public void run() {
		while (wavethread != null) {
			changed = true;
			
			st = new StreamTokenizer(time_is);
			st.eolIsSignificant(true);
			try {
                		st.nextToken();
                		yr = (int) st.nval;
               			st.nextToken();
                		mo = (int) st.nval;
                		st.nextToken();
                		da = (int) st.nval;
                		st.nextToken();
                		hr = (int) st.nval;
                		st.nextToken();
                		mn = (int) st.nval;
                		st.nextToken();
                		sec = (int)st.nval;
                		st.nextToken();
                		dt = (double) st.nval; /* samples per second */
                	} catch ( IOException e ) { }
                	Date start = new Date(yr-1990, mo, da, hr, mn, sec);
			
			try {
				ycurrent[xlen] = (int) dis.readInt();
				// scale
				ycurrent[xlen] /= -10;
				// debug
				// System.out.println(Integer.toString(ycurrent[xlen]));
			} catch (java.io.IOException e) {
				System.out.println("End of file");
				stop();
			}
			applet.repaint();

			try {
				Thread.sleep(100);
			} catch (InterruptedException e){
				System.out.println("InterruptedException");
			}
		}
		wavethread = null;
	}

	public void update(Graphics g) {
		paint(g);
	}
}
