import java.applet.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;

public class puzzle extends Applet {

  Image tiles[] = new Image[6];

  public void init() {

    setLayout(new BorderLayout());

    for (int i = 0; i < 6; i++) {
      tiles[i] = getImage(getDocumentBase(), "tile" + i + ".jpg");
    }

    Label l = new Label("This is where the project will be.");
    l.setBackground(Color.white);
    add(l, "South");

    Panel backGround = new Panel();
    backGround.setBackground(Color.white);
    add(backGround, "Center");

    backGround.setLayout(new GridLayout(1,2));

    myCanvas c = new myCanvas(tiles[0], tiles[1], tiles[2], tiles[3], tiles[4], tiles[5]);
    c.setBackground(Color.white);

    backGround.add(c);
    }// init
  } // class puzzle

  //////////////////////////////////////

  class myCanvas extends Component implements MouseListener {

    Image s1,s2,s3,s4,s5,s6;
    boolean isNew = true;
    board myBoard = new board();
    int slotVar;
    int tempVar;
    int newVar;
    Image pics[] = new Image[7];

    myCanvas(Image in0, Image in1, Image in2, Image in3, Image in4, Image in5) {
      s1= in1;
      s2= in2;
      s3= in3;
      s4= in4;
      s5= in5;
      s6= in0;
      addMouseListener(this);

      pics[0] = in0;
      pics[1] = in1;
      pics[2] = in2;
      pics[3] = in3;
      pics[4] = in4;
      pics[5] = in5;
    }

   public void paint(Graphics g) {
//     theWork();
     update(g);
   }

   public void update(Graphics g) {

     int h = s1.getHeight(this);
     int w = s1.getWidth(this);

     int a = myBoard.whichInSlot(1);
     int b = myBoard.whichInSlot(2);
     int c = myBoard.whichInSlot(3);
     int d = myBoard.whichInSlot(4);
     int e = myBoard.whichInSlot(5);
     int f = myBoard.whichInSlot(6);

     s1 = pics[a]; //myBoard.whichInSlot(1)];
     s2 = pics[b]; //myBoard.whichInSlot(2)];
     s3 = pics[c]; //myBoard.whichInSlot(3)];
     s4 = pics[d]; //myBoard.whichInSlot(4)];
     s5 = pics[e]; //myBoard.whichInSlot(5)];
     s6 = pics[f]; //myBoard.whichInSlot(6)];

     g.drawImage(s1, 0, 0, w, h, this); 
     g.drawImage(s2, 101, 0, w, h, this); 
     g.drawImage(s3, 0, 101, w, h, this); 
     g.drawImage(s4, 101, 101, w, h, this); 
     g.drawImage(s5, 0, 202, w, h, this); 
     g.drawImage(s6, 101, 202, w, h, this);
   } // update

   public void mouseClicked(MouseEvent evt) {
     int Xextent = evt.getX();
     int Yextent = evt.getY();
     int theClicked = 0;

     if (Xextent < myBoard.getXCol2() && Yextent < myBoard.getYRow2()) { theClicked = 1; }
     else if (Xextent > myBoard.getXCol2() && Yextent < myBoard.getYRow2()) { theClicked = 2; }
     else if (Xextent < myBoard.getXCol2() && Yextent > myBoard.getYRow2() && Yextent < myBoard.getYRow3()) { theClicked = 3; }
     else if (Xextent > myBoard.getXCol2() && Yextent > myBoard.getYRow2() && Yextent < myBoard.getYRow3()) { theClicked = 4; }
     else if (Xextent < myBoard.getXCol2() && Yextent > myBoard.getYRow3() && Yextent < myBoard.getYEnd()) { theClicked = 5; }
     else if (Xextent > myBoard.getXCol2() && Yextent > myBoard.getYRow3() && Yextent < myBoard.getYEnd()) { theClicked = 6; }

     refresh();

     if (myBoard.isAdjacent(theClicked,myBoard.whichEmpty())) {
       myBoard.putInSlot(myBoard.whichEmpty(),myBoard.whichInSlot(theClicked));
       myBoard.openSlot(theClicked);

     }

   }
   public void mouseEntered(MouseEvent evt) {};
   public void mouseExited(MouseEvent evt) {};
   public void mousePressed(MouseEvent evt) {};
   public void mouseReleased(MouseEvent evt) {};

   public void refresh() {
	repaint();
	return;
   }

   public void theWork() {
     randNum testVar = new randNum();

     for (int i = 1; i < 6; i++) {
      slotVar = testVar.newNum();
      if (slotVar > 6 || myBoard.checkSlot(i) || myBoard.isPicUsed(slotVar)) {
        while (slotVar > 6 || myBoard.checkSlot(i) || myBoard.isPicUsed(slotVar)) {
          slotVar = testVar.newNum();
        }
      }
      myBoard.usePic(slotVar);
      myBoard.putInSlot(i,slotVar);
     }
   }//end theWork

}// class myCanvas


