import java.applet.Applet;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;

import java.awt.Image;
import java.awt.Graphics;
import java.awt.MediaTracker;
import java.awt.Toolkit;


import java.lang.Thread;

import java.util.Random;

import netscape.javascript.JSObject;

public class crash extends Applet implements Runnable
{
	private Thread thr = null;
	private int pause = 50;
	private boolean alive = true;
	private Random rand = new Random();

	private String[] crash_words = {"stack overflow",
	"rebuilding vertices...",
	"3d model absorbed",
	"building...",
	"system error",
	"requested information unavailable",
	"running out of space",
	"the pain the pain the pain",
	"privileges denied",
	"hardware error detected",
	"software transform",
	"running low on system resources",
	"mother..."};

	private String crash_string = "";


	private Image off_screen, players, background, help, cursor;
	private int width, height;

	Font font = new Font ("Courier", Font.BOLD, 20);
	FontMetrics fontmetrics =  Toolkit.getDefaultToolkit().getFontMetrics(font);

	public void init()
	{
		players = getImage(getCodeBase(), "players.gif");
		background = getImage(getCodeBase(), "background.gif");
		help = getImage(getCodeBase(), "help.gif");
		cursor = getImage(getCodeBase(), "cursor.gif");
		MediaTracker mt = new MediaTracker (this);
		mt.addImage(players, 0);
		mt.addImage(background, 1);
		mt.addImage(help, 2);
		mt.addImage(cursor, 3);

		Dimension dim;
		try {dim = getSize();} catch (Throwable t) {dim=size();}
		width = dim.width;
		height = dim.height;

		off_screen = createImage (width, height);

		clearBackground();

		try {mt.waitForAll();} catch (Throwable t){}


		int hw = width * height / fontmetrics.getHeight();
		while (fontmetrics.stringWidth(crash_string) < hw)
		{
			crash_string = crash_string + rand.nextInt();
//			System.out.println(rand.nextInt());
		}



		thr = new Thread (this);
		thr.start();

	}

	private void clearBackground()
	{
		Graphics g = off_screen.getGraphics();
		g.setColor(new Color (0xff000000));
		g.fillRect (0, 0, width, height);
		g.dispose();
	}

	private void drawClip(Image i, int x1, int y1, int x2, int y2, int w, int h)
	{
		Graphics g = off_screen.getGraphics();
		g.clipRect (x1, y1, w, h);
		g.drawImage(i, x2, y2, this);
		g.dispose();
	}

	private int floorRand (int x)
	{
		return Math.abs(rand.nextInt()) % x;
	}

	public void run()
	{
//		alive = false;
//		while (alive)

		for (int i = 0; i < 40; i++)
		{
			drawClip(background, floorRand(width / 2), floorRand(height / 2), floorRand(width / 2), floorRand(height / 2), floorRand(width / 2), floorRand(height / 2));
			drawClip(players, floorRand(width / 2), floorRand(height / 2), floorRand(width / 2), floorRand(height / 2), floorRand(width / 2), floorRand(height / 2));

			try {thr.sleep(pause);} catch (Throwable t){}
			paint(getGraphics());
		}

		Graphics G = getGraphics();
		G.setColor (Color.blue);
		G.setFont (font);

		String s = "" + rand.nextInt();
		int sw = width - fontmetrics.stringWidth(s);
		int sh = height - fontmetrics.getHeight();
		int del_h = fontmetrics.getHeight();

		for (int i = 0; i < 150; i++)
		{
			try {thr.sleep(5);} catch (Throwable t){}
			s = "" + rand.nextInt();
			G.drawString(s, floorRand (sw), del_h + floorRand (sh));
		}

		clearBackground();
		paint(getGraphics());
		try {thr.sleep(5000);} catch (Throwable t){}

		Graphics gg = off_screen.getGraphics();

		for (int i = 0; i < 10; i++)
		{
			for (int j = 0; j < 10; j++)
			gg.drawImage(help, floorRand(width - 200), floorRand(height - 120), this);
			for (int in = 0; in < 200; in++)
			gg.drawImage(cursor, floorRand(width - 200), floorRand(height - 120), this);

			try {thr.sleep(pause);} catch (Throwable t){}
			paint(getGraphics());
		}


//		try {thr.sleep(2000);} catch (Throwable t){}
		try
		{
			JSObject thiswin = JSObject.getWindow(this);
			thiswin.eval("alert('help')");
			thiswin.eval("self.top.close()");
		}
		catch (Throwable t){}
	}

	public void paint (Graphics g)
	{
		g.drawImage(off_screen, 0, 0, this);
	}

	public void update (Graphics g)
	{
		paint (g);
	}

	private void kill()
	{
		alive = false;
	}
}