Class GraphicalBoard
java.lang.Object
|
+----java.awt.Component
|
+----java.awt.Canvas
|
+----KnightBoard
|
+----GraphicalBoard
- public class GraphicalBoard
- extends KnightBoard
- implements MouseListener, MouseMotionListener
A graphical (AWT-based) front-end for KnightBoard. Provides completely
automatic and adjusting display as well as intuitive drag functionality
for resizing the board.
Dragging is implemented completely within the class. The class provides
event notification in a fashion similar to other AWT classes. To use these
events, the caller must specify an object implementing the BoardListener
interface using setBoardListener(); however, this is optional.
By default, both mouse clicks on squares and resizing by dragging borders
are disabled. After specifying a listener, they may be enabled with
setClickable() and setResizable(), respectively.
Note: GraphicalBoard subclasses java.awt.Canvas via KnightBoard.
- See Also:
- KnightBoard, Canvas, BoardListener
-
boardheight
-
-
boardwidth
-
-
BORDER
-
-
clickable
-
-
dragging
-
-
drawall
-
-
E_DRAG
-
-
GAP
-
-
image
-
-
listener
-
-
MAX_DIMENSION
- Maximum width and height of the board in squares.
-
new_height
-
-
new_width
-
-
NO_DRAG
-
-
oldcontent
-
-
pressed
-
-
resizable
-
-
S_DRAG
-
-
SE_DRAG
-
-
squaresize
-
-
squarex
-
-
squarey
-
-
GraphicalBoard(int, int, Image)
- Constructs a new GraphicalBoard of the specified dimensions.
-
getMinimumSize()
-
-
getPreferredSize()
-
-
inBorder(int, int)
- Checks if the specified coordinates are in the borders of the board.
-
mouseClicked(MouseEvent)
-
-
mouseDragged(MouseEvent)
- If a drag has started, draws a resizing grid as the user moves the mouse.
-
mouseEntered(MouseEvent)
-
-
mouseExited(MouseEvent)
-
-
mouseMoved(MouseEvent)
- Catches mouseMoved events and changes the mouse cursor to an appropriate
shape when moved over the right or bottom borders of the board.
-
mousePressed(MouseEvent)
- Notices when a user presses the mouse button on a square inside the board,
assuming that click events are enabled.
-
mouseReleased(MouseEvent)
-
-
paint(Graphics)
- Re-draws the board completely.
-
removeBoardListener()
- Removes the current listener.
-
setBoardListener(BoardListener)
- Sets the listener that will receive boardResized() and boardClicked()
events (if enabled).
-
setClickable(boolean)
- Enables or disables choosing a square by pointing and clicking.
-
setResizable(boolean)
- Enables or disables resizing by mouse-dragging the borders.
-
update(Graphics)
- Updates the board on screen.
MAX_DIMENSION
public static final int MAX_DIMENSION
- Maximum width and height of the board in squares.
GAP
private static final int GAP
BORDER
private static final int BORDER
NO_DRAG
private static final int NO_DRAG
E_DRAG
private static final int E_DRAG
S_DRAG
private static final int S_DRAG
SE_DRAG
private static final int SE_DRAG
resizable
private boolean resizable
clickable
private boolean clickable
listener
private BoardListener listener
image
private Image image
dragging
private int dragging
pressed
private boolean pressed
squaresize
private int squaresize
boardwidth
private int boardwidth
boardheight
private int boardheight
new_width
private int new_width
new_height
private int new_height
squarex
private int squarex
squarey
private int squarey
oldcontent
private int oldcontent[][]
drawall
private boolean drawall
GraphicalBoard
public GraphicalBoard(int width,
int height,
Image knightimage)
- Constructs a new GraphicalBoard of the specified dimensions.
The knight image is drawn on the current location of the knight,
being scaled to fit within the square.
- Parameters:
- width - width of the board in squares.
- height - height of the board in squares.
- knightimage - image of a knight; if null, a filled red
circle is used instead.
- See Also:
- update, Image
setBoardListener
public void setBoardListener(BoardListener l)
- Sets the listener that will receive boardResized() and boardClicked()
events (if enabled). Only one listener can be used at a time.
- Parameters:
- l - a class implementing the BoardListener interface.
- See Also:
- removeBoardListener, BoardListener
removeBoardListener
public void removeBoardListener()
- Removes the current listener. BoardListener events will be lost after this.
- See Also:
- setBoardListener
setResizable
public void setResizable(boolean b)
- Enables or disables resizing by mouse-dragging the borders.
- Parameters:
- b - true if the board can be resized.
setClickable
public void setClickable(boolean b)
- Enables or disables choosing a square by pointing and clicking.
- Parameters:
- b - true if a square can be selected with the mouse.
update
public void update(Graphics g)
- Updates the board on screen. Automatically adjusts to the available
space. Only squares whose content has changed are re-drawn, unlike
in paint(). Normally, the Java AWT takes care of calling these
methods, and the programmer just uses repaint().
In unvisited squares, the value of the square is displayed.
Visited squares are marked with an orange circle and the number
of the visiting move, except for the square the knight is currently
in, in which a knight image is drawn. A filled blue circle is drawn
in the start square.
- Parameters:
- g - the Graphics object of this canvas.
- Overrides:
- update in class Component
- See Also:
- paint, repaint
paint
public void paint(Graphics g)
- Re-draws the board completely. Normally there is no reason to call this
method directly.
- Parameters:
- g - the Graphics object of this canvas.
- Overrides:
- paint in class Canvas
- See Also:
- update, repaint
mouseMoved
public void mouseMoved(MouseEvent e)
- Catches mouseMoved events and changes the mouse cursor to an appropriate
shape when moved over the right or bottom borders of the board.
Also notifies the BoardListener if a drag was ended.
- Parameters:
- e - mouse move event received from AWT.
- See Also:
- BoardListener, mouseDragged
inBorder
private int inBorder(int x,
int y)
- Checks if the specified coordinates are in the borders of the board.
- Parameters:
- x - x-coordinate.
- y - y-coordinate
- Returns:
- NO_DRAG, S_DRAG, E_DRAG or SE_DRAG.
mouseDragged
public void mouseDragged(MouseEvent e)
- If a drag has started, draws a resizing grid as the user moves the mouse.
Ending the drag with the same board dimensions that it was started with
will cancel the drag.
- Parameters:
- e - mouse drag event received from AWT.
mousePressed
public void mousePressed(MouseEvent e)
- Notices when a user presses the mouse button on a square inside the board,
assuming that click events are enabled. The click can be canceled by moving
the mouse out of the current square before releasing.
- Parameters:
- e - mouse press event received from AWT.
mouseReleased
public void mouseReleased(MouseEvent e)
getMinimumSize
public Dimension getMinimumSize()
- Overrides:
- getMinimumSize in class Component
getPreferredSize
public Dimension getPreferredSize()
- Overrides:
- getPreferredSize in class Component
mouseClicked
public void mouseClicked(MouseEvent e)
mouseEntered
public void mouseEntered(MouseEvent e)
mouseExited
public void mouseExited(MouseEvent e)