Class Input
- All Implemented Interfaces:
MouseListener
,MouseMotionListener
,MouseWheelListener
,EventListener
This class allows players to interact with the chess board by selecting (pressing), moving (dragging), and placing (releasing) pieces. It updates the board state based on valid moves or resets the piece if the move is invalid.
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
Handles mouse drag events from the player.void
Handles mouse press events from the player.void
Handles mouse release events from the player.Methods inherited from class java.awt.event.MouseAdapter
mouseClicked, mouseEntered, mouseExited, mouseMoved, mouseWheelMoved
-
Constructor Details
-
Input
-
-
Method Details
-
mousePressed
Handles mouse press events from the player.Determines the column and row based on the
MouseEvent
coordinates, then attempts to retrieve a piece from the corresponding location on theBoard
. If a non-null piece exists at that square, it is selected as the current piece.- Specified by:
mousePressed
in interfaceMouseListener
- Overrides:
mousePressed
in classMouseAdapter
- Parameters:
e
- the mouse event triggered by the user click action
-
mouseReleased
Handles mouse release events from the player.Determines the column and row based on the
MouseEvent
coordinates. IfBoard.selPiece
is non-null, the method constructs * aMove
and checks whether it is valid. If the move is valid, * the board is updated accordingly. Otherwise, the selected piece * returns to its original position.- Specified by:
mouseReleased
in interfaceMouseListener
- Overrides:
mouseReleased
in classMouseAdapter
- Parameters:
e
- the mouse event triggered by the user release action
-
mouseDragged
Handles mouse drag events from the player.If
Board.selPiece
is non-null, updates its screen position based on the currentMouseEvent
coordinates to allow the player to visually drag the piece. Triggers arepaint()
of the board to reflect the piece's new position.- Specified by:
mouseDragged
in interfaceMouseMotionListener
- Overrides:
mouseDragged
in classMouseAdapter
- Parameters:
e
- the mouse event triggered by the user drag action
-