/* game02.java   last mod 28 JAN 2012 by Ed James */

import java.awt.*;
import java.applet.*;         // to run this in Applet mode
import java.awt.event.*;      // for the window adapter
import javax.swing.JFrame;    // for JFrame, main GUI component
import javax.swing.JButton;   // for the GUI buttons.
import java.util.Random;      // for the random number generation

// Helper/internal class for this system - highest level GUI component
class game02Frame extends JFrame implements MouseListener, ActionListener {
   public static final long serialVersionUID = 496L; // Not currently used.

   Image myImage;
   Graphics myGraphics;

   BorderLayout myBorderLayout;

   JButton upButton, blueButton, greenButton, yellowButton;

   game02Canvas myCanvas;

   public game02Frame () {
      super ();

      setTitle ("game02");
      setSize (500,300);   // first approximation for size

      setVisible (true);
      }

// These overrides are needed for the MouseListener implementation:
   public void mouseExited   (MouseEvent ME) { }
   public void mouseEntered  (MouseEvent ME) { }
   public void mousePressed  (MouseEvent ME) { }
   public void mouseReleased (MouseEvent ME) { }
   public void mouseClicked  (MouseEvent ME) {
      myCanvas.mousePressed (ME);
      }

// This override is needed for the ActionListener implementation:
   public void actionPerformed(ActionEvent AE) {
   // NOTE: This is for demo purposes only, doesn't do anything useful yet.
      String AC = AE.getActionCommand ();
      if      (AC == "Up"   ) handleUpButton ();
      else if (AC == "Down" ) handleDownButton ();
      else if (AC == "Right") handleRightButton ();
      else if (AC == "Left" ) handleLeftButton ();
      else
         System.out.println ("ERROR: unknown action command - " + AC);
      }

   void handleUpButton    () { myCanvas.moveUp();   }
   void handleDownButton  () { myCanvas.moveDown(); }
   void handleRightButton () { myCanvas.moveRight();}
   void handleLeftButton  () { myCanvas.moveLeft(); }

   void buildGUI () {
      myBorderLayout = new BorderLayout ();
      setLayout (myBorderLayout);

      upButton = new JButton ("Up");
      upButton.setBackground (Color.yellow);
      upButton.addActionListener (this);
      add ("North", upButton);
 
      blueButton = new JButton ("Down");
      blueButton.setBackground (Color.yellow);
      blueButton.addActionListener (this);
      add ("South", blueButton);

      greenButton = new JButton ("Right");
      greenButton.setBackground (Color.yellow);
      greenButton.addActionListener (this);
      add ("East" , greenButton);

      yellowButton = new JButton ("Left");
      yellowButton.setBackground (Color.yellow);
      yellowButton.addActionListener (this);
      add ("West" , yellowButton);

      myCanvas = new game02Canvas();
      myCanvas.addMouseListener (this);
      add ("Center", myCanvas);
      }

   void addWindowListener () {
      try {
         addWindowListener (new WindowAdapter() {
            public void windowClosing (WindowEvent myEvent) {
               System.exit (0);
               }
            });
         }
      catch (java.security.AccessControlException ACE2) {
         System.out.println("Error 2: " + ACE2.getMessage());
         }
      }
   }

// Helper class for this system - graphics holder component
class game02Canvas extends java.awt.Canvas {
   public static final long serialVersionUID = 438L; // Not currently used.
   final int ARRSIZE = 10;
   int xCoor[] = new int[ARRSIZE+1];
   int yCoor[] = new int[ARRSIZE+1];
   int xInc, yInc;
   int xPiece, yPiece;
   Image offscreenImg;
   Graphics offscreen;

   Color colors[] = new Color[ARRSIZE*ARRSIZE];

   game02Canvas () {
      System.out.println ("game02Canvas() constructor");

      Random myRandom = new Random();
      int iRandom = 0;

      for (int j = 0 ; j < ARRSIZE ; j++)
         for (int i = 0 ; i < ARRSIZE ; i++) {
            iRandom = (int) (myRandom.nextDouble() * 3);
            if      (iRandom == 0) colors[j*ARRSIZE + i] = Color.red;
            else if (iRandom == 1) colors[j*ARRSIZE + i] = Color.blue;
            else if (iRandom == 2) colors[j*ARRSIZE + i] = Color.green;
            }
      xPiece = 0;
      yPiece = 0;
      }

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

   public void paint(Graphics g) {
      offscreenImg = createImage (getSize().width, getSize().height);
      offscreen = offscreenImg.getGraphics();

      xInc = getSize().width/ARRSIZE;
      yInc = getSize().height/ARRSIZE;
      if (xInc < yInc) yInc = xInc;
      else             xInc = yInc;

      for (int i = 0 ;  i <= ARRSIZE ; i++) {
         xCoor[i] = i * xInc;
         yCoor[i] = i * yInc;
         }
      setBackground (Color.black);
      offscreen.setColor (Color.white);
      for (int i = 0 ; i <= ARRSIZE ; i++) {
         offscreen.drawLine(xCoor[0], yCoor[i], xCoor[ARRSIZE], yCoor[i]);
         offscreen.drawLine(xCoor[i], yCoor[0], xCoor[i], yCoor[ARRSIZE]);
         }

      offscreen.setColor (Color.green);
      for (int j = 0 ; j < ARRSIZE ; j++)
         for (int i = 0 ; i < ARRSIZE ; i++) {
            drawSquare (g, i, j, j*ARRSIZE+i);
            }
      drawPiece (g);

      g.drawImage (offscreenImg, 0, 0, this);
      offscreen.dispose();
      }

   void drawSquare (Graphics g, int i, int j, int iColor) {
      offscreen.setColor (colors[iColor]);
      offscreen.fillRect (xCoor[i]+1, yCoor[j]+1, xInc-2, yInc-2);
      }

   void drawPiece (Graphics g) {
      offscreen.setColor (Color.black);
      offscreen.fillOval (xCoor[xPiece]+1, yCoor[yPiece]+1,  xInc-2, yInc-2);
      }

   void moveUp () {
      if (yPiece > 0)
         yPiece--;

      repaint ();
      }

   void moveDown () {
      if (yPiece < ARRSIZE-1)
         yPiece++;

      repaint ();
      }

   void moveLeft () {
      if (xPiece > 0)
         xPiece--;

      repaint ();
      }

   void moveRight () {
      if (xPiece < ARRSIZE-1)
         xPiece++;

      repaint ();
      }

   public void mousePressed(MouseEvent ME) {
      Point myPoint = ME.getPoint(); // gives a Point object.
      xPiece = ARRSIZE;
      yPiece = ARRSIZE;
      while (xCoor[xPiece] > myPoint.x) xPiece--;
      while (yCoor[yPiece] > myPoint.y) yPiece--;

      repaint();
      }
   }

// Main class for this system:
public class game02 extends Applet {
   public static final long serialVersionUID = 497L; // Not currently used.
   public static game02Frame myFrame;

   public void init () {
      System.out.println ("game02.init () started");
      myFrame = new game02Frame ();

      myFrame.buildGUI ();
      }

   public static void main (String[] args) {
      System.out.println ("game02.main() started");
      myFrame = new game02Frame ();

      myFrame.buildGUI ();
      myFrame.addWindowListener ();
      }


   public void paint (Graphics g) {
      }
   }