© Corel Interactions with the mouse

In this part our programm will learn to react on mouse events. That means that the user may move the pieces with the mouse. We still don't look for valid moves, the user may do what he want.

First we have to register the event handler method. Therefore we add the followin lines to the constructor of the class Board:

//event handlers
addMouseListener (this);
addMouseMotionListener (this);

So the corresponding methos of the MouseListener and MouseMotionListener Interfaces will be called it the event occurs. The interaction will have Drag&Drop from. The user clicks with the mouse on a piece, drags the mouse on an other field and then drop the piece. For that we need some variables. We declare them in the global namespace of the class Board:

//Drag & Drop
int code = 0,   //access for movelist
    start = 21, //index of the start field
    alt = 21,   //new field?
    end = 21,   //index of the end field
    x = 0,      //x coordinate
    y = 0;      //y coordinate

We also have to definite some new methods. First there is the method execute (int start, int end):

//execute a move
public void execute (int start, int end) {
   board [end] = board [start];
   board [start] = 0;

   paintField (end);
   paintField (start);
}

This method will be called when the computer has choosen his move or when the human makes a valid move. To different between valid and invalid moves we add the method isvalid (int move):

//This method checks if a move is valid
public boolean isvalid (int move) {
    return true;
}

Because we still have not implemented the chess rules returns the method alway true. We come back to this method in the first AI part so that it will different between valid and invalid moves.

Now we comes to the methods of the event handlers. We use the methods mouseDragged (), mousePressed ()and mouseReleased (). Wie start with the beginning of the mouse interaction, the method mousePressed ():

public void mousePressed( java.awt.event.MouseEvent e) {
   x = e.getX() / 40;
   if (x < 0)
      x = 0;
   if (x > 7)
      x = 7;

   y = e.getY() / 40;
   if (y < 0)
      y = 0;
   if (y > 7)
      y = 7;

   start = 21 + y*10 + x;
   alt = start;
   end = start;

   //mark start field
   Graphics g = getGraphics ();
   g.setColor (blue);
   g.fillRect (x * 40, y * 40, 40, 40);
   try {
      g.drawImage (pieces [board [start] % 100 - 10], x * 40, y * 40, 40, 40, parent);
   } catch (ArrayIndexOutOfBoundsException ex) {}
}

First of all we calculate the row and the column of the chessboard over which the mouse is. We correct errors that could happen when the mouse leaves the chess board (we look that the index is always valid). Then we set the Drag&Drop variables. At the end we repaint the start field with a blue ground.

The next method is mouseDragged (). It is called when the mouse is moved while a button is pressed:

public void mouseDragged (java.awt.event.MouseEvent e) {
   x = e.getX() / 40;
   if (x < 0 )
      x = 0;
   if (x > 7 )
      x = 7;

   y = e.getY() / 40;
   if (y < 0)
      y = 0;
   if (y > 7 )
      y = 7;

   end = 21 + y * 10 + x;

   if ( end != alt) {
      //rebuild old field
      if (alt != start)
         paintField (alt);
      //mark new field
      if ( end != start) {
         Graphics g = getGraphics ();
         if ( (code != 1) && (isvalid (start * 100 + end) ))
            g.setColor (green);
         else
            g.setColor (red);
         g.fillRect (x * 40, y * 40, 40, 40);
         try {
            g.drawImage (pieces [board [end] % 100 - 10], x * 40, y * 40, 40, 40, parent);
         } catch (ArrayIndexOutOfBoundsException ex) {}
      }
      alt = end; }
   }
}

This method checks if the mouse is on a new field of the chess board. If not the following commands will not be executed. If the mouse was moved on a new field, the old field will be repaired. With one exception: the start field should be marked until the end of the mouse interaction. Then the new field will be marked. Also this time we first test if we're back on the start field.

Now we come to the end of the mouse interaction: the method mouseReleased (). The method is called when the user releases a mouse button.

public void mouseReleased (java.awt.event.MouseEvent e) {
   //erase marks
   paintField (start);
   paintField (end);

   //execute move if valid
   if ((code != 1) && (isvalid (start * 100 + end ) ))
      execute (start, end);
}

Those methods are really easy. If the mouse has been moved to the end field, the coordinates have been set by the mousePressed () or mouseDragged () method. We only have to erase the marks and execute the move if it is valid. The valiable code looks that we do not access the movelist while the AI is generating the new list.

show applet step 4
source code of step 4


to the overview | back | continue