Java Reference
In-Depth Information
45.
46.
47.
48.
public void hideIt(){
49.
this .setVisible( false );
50.
}
51.
52.
53.
}
14.4
The control part
The user-program interaction is specified as follows. The user selects a direction
by clicking a radio button in the direction panel. This requires no listeners because
the mechanism is supplied by RadioButton and ButtonGroup . Once a direction
is selected a move can be made by clicking on a block in the BlockPuzzlePanel .
This block is then moved in the specified direction if the move is legal, i.e. if the
empty place is adjacent to the block in that direction.
A listener is used to detect when a click in the BlockPuzzlePanel occurs and
to make the move. As we are reacting to a mouse click we extend a MouseAdapter
in class BlockPuzzleListener .Inorder to be able to perform the desired task, the
listener has to know the direction and the block of the desired move. The move is
then made in the block puzzle panel. Therefore BlockPuzzleListener receives
references to the DirectionPanel and the BlockPuzzlePanel in the constructor.
The listener is assigned to the BlockPuzzlePanel .
If a mouse click occurs in the BlockPuzzlePanel , the listener's mouseClicked
method is invoked. The listener extracts the x - and y -coordinates of the location
of the click from the mouse event object. These are in pixels. The listener asks the
panel which block is at that location. This is done by using the block panel's get-
BlockNoAtPixels(x,y) method. The listener determines the direction by asking
the direction panel using getDirection() .
Now that the block number and direction are known, the listener constructs an
instance MoveModel for the desired move. The move is passed to the block puzzle
panel using method makeMove(move) .
File: its/BlockPuzzle/BlockPuzzleListener.java
package its.BlockPuzzle;
1.
import java.awt.event.MouseAdapter;
2.
import java.awt.event.MouseEvent;
3.
4.
5.
6.
public class BlockPuzzleListener extends MouseAdapter{
private DirectionPanel dirPanel;
7.
Search WWH ::




Custom Search