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

Variable Index

 o boardheight
 o boardwidth
 o BORDER
 o clickable
 o dragging
 o drawall
 o E_DRAG
 o GAP
 o image
 o listener
 o MAX_DIMENSION
Maximum width and height of the board in squares.
 o new_height
 o new_width
 o NO_DRAG
 o oldcontent
 o pressed
 o resizable
 o S_DRAG
 o SE_DRAG
 o squaresize
 o squarex
 o squarey

Constructor Index

 o GraphicalBoard(int, int, Image)
Constructs a new GraphicalBoard of the specified dimensions.

Method Index

 o getMinimumSize()
 o getPreferredSize()
 o inBorder(int, int)
Checks if the specified coordinates are in the borders of the board.
 o mouseClicked(MouseEvent)
 o mouseDragged(MouseEvent)
If a drag has started, draws a resizing grid as the user moves the mouse.
 o mouseEntered(MouseEvent)
 o mouseExited(MouseEvent)
 o 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.
 o mousePressed(MouseEvent)
Notices when a user presses the mouse button on a square inside the board, assuming that click events are enabled.
 o mouseReleased(MouseEvent)
 o paint(Graphics)
Re-draws the board completely.
 o removeBoardListener()
Removes the current listener.
 o setBoardListener(BoardListener)
Sets the listener that will receive boardResized() and boardClicked() events (if enabled).
 o setClickable(boolean)
Enables or disables choosing a square by pointing and clicking.
 o setResizable(boolean)
Enables or disables resizing by mouse-dragging the borders.
 o update(Graphics)
Updates the board on screen.

Variables

 o MAX_DIMENSION
 public static final int MAX_DIMENSION
Maximum width and height of the board in squares.

 o GAP
 private static final int GAP
 o BORDER
 private static final int BORDER
 o NO_DRAG
 private static final int NO_DRAG
 o E_DRAG
 private static final int E_DRAG
 o S_DRAG
 private static final int S_DRAG
 o SE_DRAG
 private static final int SE_DRAG
 o resizable
 private boolean resizable
 o clickable
 private boolean clickable
 o listener
 private BoardListener listener
 o image
 private Image image
 o dragging
 private int dragging
 o pressed
 private boolean pressed
 o squaresize
 private int squaresize
 o boardwidth
 private int boardwidth
 o boardheight
 private int boardheight
 o new_width
 private int new_width
 o new_height
 private int new_height
 o squarex
 private int squarex
 o squarey
 private int squarey
 o oldcontent
 private int oldcontent[][]
 o drawall
 private boolean drawall

Constructors

 o 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

Methods

 o 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
 o removeBoardListener
 public void removeBoardListener()
Removes the current listener. BoardListener events will be lost after this.

See Also:
setBoardListener
 o setResizable
 public void setResizable(boolean b)
Enables or disables resizing by mouse-dragging the borders.

Parameters:
b - true if the board can be resized.
 o 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.
 o 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
 o 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
 o 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
 o 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.
 o 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.
 o 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.
 o mouseReleased
 public void mouseReleased(MouseEvent e)
 o getMinimumSize
 public Dimension getMinimumSize()
Overrides:
getMinimumSize in class Component
 o getPreferredSize
 public Dimension getPreferredSize()
Overrides:
getPreferredSize in class Component
 o mouseClicked
 public void mouseClicked(MouseEvent e)
 o mouseEntered
 public void mouseEntered(MouseEvent e)
 o mouseExited
 public void mouseExited(MouseEvent e)