import java.applet.*;
import java.lang.*;
import java.awt.*;
import java.net.*;

//Box class definition

public class BoxX extends Applet {

     final int ON  =  1;					//ON -> grid square is PINK
     final int OFF = -1;					//OFF -> grid square is BLACK
     final int WIDTH = 220;					//board width
     protected int box10[][];					//board array definition
     int end = 0;						//game end variable

/** Initialize applet parameters and define grid */

     public void init() {					//initialize applet parameters
          end = 0;
          box10 = new int[10][10];				//define game grid of 10 x 10
          for (int a = 0; a < 10; a++) {			//initialize all grid spaces to 'OFF' = PINK
               for (int b = 0; b < 10; b++) {
                    box10[a][b] = OFF;
               }
          }
     }

/** Define background and call init function */

     public void start() {					//applet start
          setBackground(Color.gray);				//define background color
          init();						//call initialization
     }		

/** Construct RESET button call to drawBoard Function */

     public void paint(Graphics g) {				//input = graphics object
          g.setColor(Color.black);				//RESET button background is black
          g.fill3DRect(75, 230, 60, 20, true);			//fill rectangle
          g.setColor(Color.white);				//text is white
          g.drawString("RESET", 85, 245);			//text defined
          drawBoard(g);						//call to function to draw gameboard
          for (int a = 0; a < 10; a++) {			// and fill with PINK
               for (int b = 0; b < 10; b++) {
                    if (box10[a][b]  != 0) {
                         drawBox(a, b, g);
                    }
               }
          }
     }

/** Construct 10 x 10 game board grid */

     public void drawBoard(Graphics g) {			//input = graphics object
          g.setColor(Color.black);				//board lines to be black
          g.drawLine(0,0, 0,WIDTH);				//outside perimeter of box
          g.drawLine(WIDTH,0, WIDTH,WIDTH);
          g.drawLine(0,0, WIDTH,0);
          g.drawLine(0,WIDTH, WIDTH,WIDTH);
          for (int a = 1; a < 10; a++) {			//divide into 10 x 10
               g.drawLine(WIDTH*a/10,0, WIDTH*a/10,WIDTH);
               g.drawLine(0,WIDTH*a/10, WIDTH,WIDTH*a/10);
          }
     }

/** Change grid squares appropriately based on mouseclick */

     public void drawBox(int column, int row, Graphics g) {	//definition for color change on mouseclick	
          if (box10[column][row] == ON) {			//for grid square clicked on and diagonals
               g.setColor(Color.black);				//if PINK change to BLACK, if BLACK change to PINK
          } else if (box10[column][row] == OFF) {
               g.setColor(Color.pink);
          }
          g.fillRect(column * WIDTH / 10 + 2, row * WIDTH / 10 + 2,	// fill squares
                     WIDTH / 10 - 3, WIDTH / 10 - 3);
     }

/** End of game definition, reset board */

     public void endGame(Graphics g) {				//end of game, reset board
          update(getGraphics());
          for (int a = 0; a < 10; a++) {
               for (int b = 0; b < 10; b++) {
                    box10[a][b] = OFF;
                    update(getGraphics());
               }
          }
          for (int a = 0; a <10; a++) {
               for (int b = 0; b < 10; b++) {
                    box10[a][b] = ON;
                    update(getGraphics());
               }
          }
         end = 1;
     }

/** Mouse events handled, definition of mouseclick pattern */

     public boolean mouseUp(Event event, int x, int y) {		//function based on mouseclick
          if (end == 1) {						//if end=1, new game-reset graphics
               init();
               update(getGraphics());
          } else {
               if (x > 75 && x < 270 && y > 230 && y < 280) {		//define field for RESET button
                    init();
                    update(getGraphics());
               }
               int column = (int)(x / (WIDTH / 10));
               int row    = (int)(y / (WIDTH / 10));
               box10[column][row] = -box10[column][row];
               try {
                    box10[column+1][row+1] = -box10[column+1][row+1];	//grid square at lower right diagonal of clicked grid square
               } catch(Exception e) {
               }
               try {
                    box10[column-1][row-1] = -box10[column-1][row-1];	//grid square at upper left diagonal of clicked grid square
               } catch(Exception e) {
               }
               try {
                    box10[column+1][row-1] = -box10[column+1][row-1];	//grid square at upper right diagonal of clicked grid square
               } catch(Exception e) {
               }
               try {
                    box10[column-1][row+1] = -box10[column-1][row+1];	//grid square at lower left diagonal of clicked grid square
               } catch(Exception e) {
               }
               if (end == 0) {						//not done, repaint screen
                    update(getGraphics());
               }
          }
          return true;
     }
}
