Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

antichess
Class XChessBoard  view XChessBoard download XChessBoard.java

java.lang.Object
  extended byjava.awt.Component
      extended byjava.awt.Container
          extended byjavax.swing.JComponent
              extended byjavax.swing.JLayeredPane
                  extended byantichess.XChessBoard
All Implemented Interfaces:
javax.accessibility.Accessible, java.util.EventListener, java.awt.image.ImageObserver, java.awt.MenuContainer, java.awt.event.MouseListener, java.awt.event.MouseMotionListener, java.io.Serializable

public class XChessBoard
extends javax.swing.JLayeredPane
implements java.awt.event.MouseListener, java.awt.event.MouseMotionListener

XChessBoard creates an antichess board image on the board, places pieces on it, and animates pieces for drag&drop.

Here is the outline of how XChessBoard handles an antichess board:
Everytime setGameController is called, a new backgroud image is created by first calling initBoard() and then, setBoardImage().

placePieces() creates labels with piece images for every non-empty square. It places this accordingly on the board.

updateBoard() goes through all the squares of the board. It updates the positions of already created pieces. If there are not enough labels, it creates new labels. If there are any excess labels it gets rid of them.

Drag&drop is implemented through JLabel.setLocation() method. This provides a flawless animation compared to other methods.


Nested Class Summary
 
Nested classes inherited from class javax.swing.JLayeredPane
javax.swing.JLayeredPane.AccessibleJLayeredPane
 
Nested classes inherited from class javax.swing.JComponent
javax.swing.JComponent.AccessibleJComponent
 
Nested classes inherited from class java.awt.Container
java.awt.Container.AccessibleAWTContainer
 
Nested classes inherited from class java.awt.Component
java.awt.Component.AccessibleAWTComponent, java.awt.Component.BltBufferStrategy, java.awt.Component.FlipBufferStrategy
 
Field Summary
private  int arbX
           
private  int arbY
           
private  javax.swing.JLabel background
           
private  java.awt.Color BLACKCOLOR
           
private  Board board
           
private  int boardMargin
           
private  javax.swing.JLabel[][] circArray
           
private  javax.swing.JLabel[][] circArray2
           
private static boolean DEBUG
           
private  int endCol
           
private  int endRow
           
private  int fourtwoMargin
           
private static GameController gc
           
private  HumanPlayer human
           
private  java.util.ArrayList[] labelList
           
private  int letterMargin
           
private  boolean mousePress
           
private  javax.swing.JLabel movingLabel
           
private  java.awt.Image[] pieceImageList
           
private  int piecePaddingX
           
private  int piecePaddingY
           
private  int pieceSizeX
           
private  int pieceSizeY
           
private  int SIZEX
           
private  int SIZEY
           
private  int squareSizeX
           
private  int squareSizeY
           
private  int startCol
           
private  char startLetter
           
private  char startNumber
           
private  int startRow
           
private  java.awt.Color WHITECOLOR
           
 
Fields inherited from class javax.swing.JLayeredPane
DEFAULT_LAYER, DRAG_LAYER, FRAME_CONTENT_LAYER, LAYER_PROPERTY, MODAL_LAYER, PALETTE_LAYER, POPUP_LAYER
 
Fields inherited from class javax.swing.JComponent
accessibleContext, listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
 
Fields inherited from class java.awt.Container
 
Fields inherited from class java.awt.Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Constructor Summary
XChessBoard()
          Creates new form XChessBoard
 
Method Summary
private  void getCircles()
          Loads the red circle images from the directory that indicate valid pieces to be moved
private  void getCircles2()
          Loads the blue circle images from the directory that indicate valid moves that can be made by piece being moved
private  void getImages()
          Loads the images from the directory
private  void initBoard()
          Initalizes the board variables
private  void initComponents()
          This method is called from within the constructor to initialize the form.
private  void initValues()
          Initializes global variables
 void mouseClicked(java.awt.event.MouseEvent e)
          Invoked when the mouse button has been clicked (pressed and released) on a component.
 void mouseDragged(java.awt.event.MouseEvent e)
          Invoked when a mouse button is pressed on a component and then dragged.
 void mouseEntered(java.awt.event.MouseEvent e)
          Invoked when the mouse enters a component.
 void mouseExited(java.awt.event.MouseEvent e)
          Invoked when the mouse exits a component.
 void mouseMoved(java.awt.event.MouseEvent e)
          Invoked when the mouse button has been moved on a component (with no buttons down).
 void mousePressed(java.awt.event.MouseEvent e)
          Invoked when a mouse button has been pressed on a component.
 void mouseReleased(java.awt.event.MouseEvent e)
          Invoked when a mouse button has been released on a component.
private  void placeCircles()
          Method to place red circles on the board that indicate the pieces that can be moved
private  void placeCircles2()
           
private  void placePieces()
          Method to place pieces on the board
private  void setBoardImage()
          Sets the board image for background.
 void setGameController(GameController gc)
          Sets the GameController handler to the specified gamecontroller
 void updateBoard()
          Method to update pieces on the board Goes through board, and if there is a piece at (i,j) takes a label from labelList, puts the necessary image on it places it on the right place.
 
Methods inherited from class javax.swing.JLayeredPane
addImpl, getAccessibleContext, getComponentCountInLayer, getComponentsInLayer, getComponentToLayer, getIndexOf, getLayer, getLayer, getLayeredPaneAbove, getObjectForLayer, getPosition, highestLayer, insertIndexForLayer, isOptimizedDrawingEnabled, lowestLayer, moveToBack, moveToFront, paint, putLayer, remove, removeAll, setLayer, setLayer, setPosition
 
Methods inherited from class javax.swing.JComponent
addAncestorListener, addNotify, addPropertyChangeListener, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBorder, getBounds, getClientProperty, getComponentGraphics, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getGraphics, getHeight, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getUIClassID, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isPaintingTile, isPreferredSizeSet, isRequestFocusEnabled, isValidateRoot, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, paramString, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFont, setForeground, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update, updateUI
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalKeys, setFocusTraversalPolicy, setLayout, transferFocusDownCycle, validate, validateTree
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

DEBUG

private static final boolean DEBUG
See Also:
Constant Field Values

gc

private static GameController gc

board

private Board board

SIZEX

private int SIZEX

SIZEY

private int SIZEY

letterMargin

private int letterMargin

squareSizeX

private int squareSizeX

squareSizeY

private int squareSizeY

pieceSizeX

private int pieceSizeX

pieceSizeY

private int pieceSizeY

startLetter

private char startLetter

startNumber

private char startNumber

WHITECOLOR

private java.awt.Color WHITECOLOR

BLACKCOLOR

private java.awt.Color BLACKCOLOR

boardMargin

private int boardMargin

fourtwoMargin

private int fourtwoMargin

pieceImageList

private java.awt.Image[] pieceImageList

piecePaddingX

private int piecePaddingX

piecePaddingY

private int piecePaddingY

startCol

private int startCol

startRow

private int startRow

endCol

private int endCol

endRow

private int endRow

background

private javax.swing.JLabel background

labelList

private java.util.ArrayList[] labelList

movingLabel

private javax.swing.JLabel movingLabel

arbX

private int arbX

arbY

private int arbY

mousePress

private boolean mousePress

circArray

private javax.swing.JLabel[][] circArray

circArray2

private javax.swing.JLabel[][] circArray2

human

private HumanPlayer human
Constructor Detail

XChessBoard

public XChessBoard()
Creates new form XChessBoard

Method Detail

initComponents

private void initComponents()
This method is called from within the constructor to initialize the form.


initValues

private void initValues()
Initializes global variables


getImages

private void getImages()
Loads the images from the directory


getCircles

private void getCircles()
Loads the red circle images from the directory that indicate valid pieces to be moved


getCircles2

private void getCircles2()
Loads the blue circle images from the directory that indicate valid moves that can be made by piece being moved


placeCircles

private void placeCircles()
Method to place red circles on the board that indicate the pieces that can be moved


placeCircles2

private void placeCircles2()

mouseClicked

public void mouseClicked(java.awt.event.MouseEvent e)
Invoked when the mouse button has been clicked (pressed and released) on a component.

Specified by:
mouseClicked in interface java.awt.event.MouseListener

mouseEntered

public void mouseEntered(java.awt.event.MouseEvent e)
Invoked when the mouse enters a component.

Specified by:
mouseEntered in interface java.awt.event.MouseListener

mouseExited

public void mouseExited(java.awt.event.MouseEvent e)
Invoked when the mouse exits a component.

Specified by:
mouseExited in interface java.awt.event.MouseListener

mouseMoved

public void mouseMoved(java.awt.event.MouseEvent e)
Invoked when the mouse button has been moved on a component (with no buttons down).

Specified by:
mouseMoved in interface java.awt.event.MouseMotionListener

mouseDragged

public void mouseDragged(java.awt.event.MouseEvent e)
Invoked when a mouse button is pressed on a component and then dragged. MOUSE_DRAGGED events will continue to be delivered to the component where the drag originated until the mouse button is released (regardless of whether the mouse position is within the bounds of the component).

Due to platform-dependent Drag&Drop implementations, MOUSE_DRAGGED events may not be delivered during a native Drag&Drop operation.

Specified by:
mouseDragged in interface java.awt.event.MouseMotionListener

mousePressed

public void mousePressed(java.awt.event.MouseEvent e)
Invoked when a mouse button has been pressed on a component.

Specified by:
mousePressed in interface java.awt.event.MouseListener

mouseReleased

public void mouseReleased(java.awt.event.MouseEvent e)
Invoked when a mouse button has been released on a component.

Specified by:
mouseReleased in interface java.awt.event.MouseListener

setGameController

public void setGameController(GameController gc)
Sets the GameController handler to the specified gamecontroller


initBoard

private void initBoard()
Initalizes the board variables


setBoardImage

private void setBoardImage()
Sets the board image for background. Paints squares in given BLACKCOLOR and WHITECOLOR. Adds position letters/numbers at the bottom and on the right


placePieces

private void placePieces()
Method to place pieces on the board


updateBoard

public void updateBoard()
Method to update pieces on the board Goes through board, and if there is a piece at (i,j) takes a label from labelList, puts the necessary image on it places it on the right place. This method discards labels if there are less pieces on the board.