/* 
	Game.java

	[ more java @ mlab - 10-17.1.2000 - juhuu@katastro.fi ]

*/

import java.applet.Applet;
import java.awt.*;
import java.net.*;
import java.util.*;

public class Game extends Applet implements Runnable
{
	Image		imgDoubleBuffer;
	Graphics	gDoubleBuffer;
	Thread		engine = null;
  
	int		nMousex, nMousey;
	boolean 	bMouse_down;

	int		nScr_width;
	int		nScr_height;

	int		nGrid_width;
	int		nGrid_height;

	public void init()
	{
		// initialize double buffering
		imgDoubleBuffer = createImage(size().width, size().height);
		gDoubleBuffer = imgDoubleBuffer.getGraphics();
		gDoubleBuffer.setColor(Color.white);
		gDoubleBuffer.fillRect(0, 0, size().width, size().height);

		nScr_width		= size().width;
		nScr_height		= size().height;

		nMousex 		= 0;
		nMousey 		= 0;
		bMouse_down		= false;

		nGrid_width		= 10;
		nGrid_height		= 10;
		
	}

//----------------------------------------------------

	public void paint(Graphics g) {

		// clear the screen

		gDoubleBuffer.setColor(Color.white);
		gDoubleBuffer.fillRect(0, 0, size().width, size().height);

		gDoubleBuffer.setColor(Color.black);

		int	nX;
		int	nY;
		int	nWidth;
		int	nHeight;

		for(int i = 0 ; i < nGrid_width; i++)
		{
			for(int j = 0 ; j < nGrid_height; j++)
			{
				nX = 20 * i + 100;
				nY = 20 * j + 100;

				nWidth = 10 + Math.abs((nMousex - nX) / 10);
				nHeight = 10 + Math.abs((nMousey - nY) / 10);

				gDoubleBuffer.drawOval(nX, nY, nWidth, nHeight);
			}
		}

		// draw all the stuff from the double buffer to the screen
		g.drawImage(imgDoubleBuffer, 0, 0, null);				
	}


//----------------------------------------------------


	// start the applet
	public void start() {
		if (engine == null)
		{
			engine = new Thread(this);
			engine.start();
		}
		showStatus(getAppletInfo());
	} 

	// stop the applet
	public void stop() {
		if (engine != null && engine.isAlive())
		{
			engine.stop();
		}

		engine = null; 
	}

	public void run()
	{                  
		while (true)
		{
			try
			{
				repaint();
				Thread.sleep(10);
			} catch (InterruptedException e) {
				stop();
			}
		}
	}

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

	// print to the console & browserstatus
	private void print(String s) {
		System.out.println(s); 		// console
		showStatus(s);         		// browser status  
	}

//---

	public boolean mouseDown(Event e, int x, int y) {
		bMouse_down = true;
		return true;  
	}

	public boolean mouseUp(Event e, int x, int y) {
		bMouse_down = false;
		return true;  
	}

	public boolean mouseMove(Event e, int x, int y)
	{
		nMousex = x;
		nMousey = y;
		return true;  
	}

	public boolean mouseDrag(Event e, int x, int y)
	{
		nMousex = x;
		nMousey = y;
		return true;  
	}
//---
}
