Class Grid

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Container
          extended by javax.swing.JComponent
              extended by javax.swing.JPanel
                  extended by Grid
All Implemented Interfaces:
java.awt.image.ImageObserver, java.awt.MenuContainer, java.io.Serializable, javax.accessibility.Accessible

public class Grid
extends javax.swing.JPanel

This is the Grid panel for the CLIP application. It is responsible for drawing the the gridlines and tiles. It implements interface functions used by the UserInteraction and Playback classes to manipulate tiles in the grid.

See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class javax.swing.JPanel
javax.swing.JPanel.AccessibleJPanel
 
Nested classes/interfaces inherited from class javax.swing.JComponent
javax.swing.JComponent.AccessibleJComponent
 
Nested classes/interfaces inherited from class java.awt.Container
java.awt.Container.AccessibleAWTContainer
 
Nested classes/interfaces inherited from class java.awt.Component
java.awt.Component.AccessibleAWTComponent, java.awt.Component.BltBufferStrategy, java.awt.Component.FlipBufferStrategy
 
Field Summary
(package private)  boolean drawGridBool
          If true then draw gridlines on the grid.
(package private)  int[][] grid
          An array that stores tile information for the grid.
(package private)  java.awt.Dimension GridDefaultSizeInPixels
          The default size of the grid before a tileset is loaded.
(package private)  int gridHeight
          The height of the grid in gridSquares.
(package private)  java.lang.String GridName
          The name of this panel.
(package private)  java.awt.Dimension gridSizeInPixels
          The size of the grid in pixels.
(package private)  int gridSquareSize
          The size in pixels of each gridsquare on the grid.
(package private)  int gridWidth
          The width of the grid in gridSqaures.
(package private)  UserInteraction mouseInput
          The UserInteraction object.
private static long serialVersionUID
           
(package private)  MiniMapPanel theMiniMap
          the MiniMapPanel object.
(package private)  TileSet theTileSet
          The TileSet that is currently in use.
 
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.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
Grid(UserInteraction mInput)
          The constructor for the grid class.
 
Method Summary
 void changeGridSize(java.awt.Dimension newGridSize)
          This changes the size of the grid and updates the appropriate variables.
 void drawGrid(java.awt.Graphics g)
          Draws gridlines defined by the gridSquareSize.
 void drawTiles(java.awt.Graphics g)
          Draws the tiles on the grid.
 void drawTiles(java.awt.Graphics2D g, int xOffset, int yOffset)
          Draws tiles on a 2D graphics object at a certain offset.
 void eraseTilePosition(int tileToErase)
          This erases the passed in tile from the grid array.
 java.awt.Point findLocForTile(int xGridCenter, int yGridCenter, int curTile)
          Finds a valid location for a tile to be placed in the grid.
 int findTileAt(int x, int y)
          Finds the tileID of the tile stored at the grid square defined by the passed in coordinates (0 if no tile found).
 java.awt.Point getGridSquareAt(int x, int y)
          Returns the coordinates of the grid square defined by the passed in mouse coordinates.
 int getGridSquareSize()
          Returns the grid square size.
 java.awt.Point getMouseOffset(int x, int y, int tileID)
          Accepts mouse coordinates and a tileID and then returns the offset from the mouse location to the tile's upper left location.
 boolean isValidLoc(int x, int y, int tID)
          Determines if the grid coordinates defined by the passed in x and y values is a valid grid location for the tile with the passed in TileID
 void paintComponent(java.awt.Graphics g)
          Paints the grid with tiles and or gridlines if needed.
 void placeTileAt(int x, int y, int tileToPlace)
          Places the tile with the passed in tileID to the location defined by the passed in x and y values (in gridsquares).
 void printGrid()
          Debugging function that prints the contents of the grid array to the console.
 void setMiniMap(MiniMapPanel mm)
          Sets the mini map to the passed in MiniMapPanel.
 void setTileSet(TileSet curSet)
          Sets the current tile set to the passed in tile set.
 void updateTileID(int newID, int x, int y, int w, int h)
          Updates a grid location defined by the parameters with a new tileID that has been passed in.
 
Methods inherited from class javax.swing.JPanel
getAccessibleContext, getUI, getUIClassID, paramString, setUI, updateUI
 
Methods inherited from class javax.swing.JComponent
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFocusTraversalKeys, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, addImpl, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusBackward, 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, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, 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, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, 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, 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, transferFocusUpCycle
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

serialVersionUID

private static final long serialVersionUID
See Also:
Constant Field Values

GridDefaultSizeInPixels

final java.awt.Dimension GridDefaultSizeInPixels
The default size of the grid before a tileset is loaded.


gridSquareSize

final int gridSquareSize
The size in pixels of each gridsquare on the grid.

See Also:
Constant Field Values

GridName

final java.lang.String GridName
The name of this panel.

See Also:
Constant Field Values

mouseInput

UserInteraction mouseInput
The UserInteraction object.


theMiniMap

MiniMapPanel theMiniMap
the MiniMapPanel object.


theTileSet

TileSet theTileSet
The TileSet that is currently in use.


gridSizeInPixels

java.awt.Dimension gridSizeInPixels
The size of the grid in pixels.


gridWidth

int gridWidth
The width of the grid in gridSqaures.


gridHeight

int gridHeight
The height of the grid in gridSquares.


drawGridBool

boolean drawGridBool
If true then draw gridlines on the grid.


grid

int[][] grid
An array that stores tile information for the grid. It tracks everything by each tile's unique tileID.

Constructor Detail

Grid

public Grid(UserInteraction mInput)
The constructor for the grid class. This constructor sets the grid size to its default, creates a new grid arrray, sets the current tile set to null, and registers mouse listeners.

Parameters:
mInput - CLIP's mouse adapter class
Method Detail

paintComponent

public void paintComponent(java.awt.Graphics g)
Paints the grid with tiles and or gridlines if needed. It also causes the minimap to repaint.

Overrides:
paintComponent in class javax.swing.JComponent
Parameters:
g - Graphics object to paint
See Also:
drawGrid(Graphics), drawTiles(Graphics), MiniMapPanel

setTileSet

public void setTileSet(TileSet curSet)
Sets the current tile set to the passed in tile set.

Parameters:
curSet - TileSet to use as the current TileSet
See Also:
TileSet

setMiniMap

public void setMiniMap(MiniMapPanel mm)
Sets the mini map to the passed in MiniMapPanel.

Parameters:
mm - The MiniMapPanel to use as the mini map

getGridSquareSize

public int getGridSquareSize()
Returns the grid square size. Grid square size is the size of each square in the grid in pixels. (should be 20x20)

Returns:
The grid square size.
See Also:
gridSquareSize

printGrid

public void printGrid()
Debugging function that prints the contents of the grid array to the console.

See Also:
grid

drawGrid

public void drawGrid(java.awt.Graphics g)
Draws gridlines defined by the gridSquareSize.

Parameters:
g - The graphics object to draw the gridlines on
See Also:
paintComponent(Graphics)

drawTiles

public void drawTiles(java.awt.Graphics g)
Draws the tiles on the grid. Tiles will only be drawn if they are in the grid and they are not the current tile (being dragged). param g The graphics object to draw the cards on

See Also:
Tile, paintComponent(Graphics)

drawTiles

public void drawTiles(java.awt.Graphics2D g,
                      int xOffset,
                      int yOffset)
Draws tiles on a 2D graphics object at a certain offset. The offsets are defined in grid squares. The offsets are subtracted from a tile's location and then it is drawn. The purpose of this function is to create functionality to generate a cropped image containing all the tiles in the grid. This is used by the print functionality in the UserInterface class.

Parameters:
g - The graphics object to draw on
xOffset - Number to subtract from tiles' X location
yOffset - Number to subtract from tiles' Y location
See Also:
Tile, UserInterface.getPrintArrangementImage()

changeGridSize

public void changeGridSize(java.awt.Dimension newGridSize)
This changes the size of the grid and updates the appropriate variables. Since it creates a new grid array, doing this while tiles are still in the grid will cause that information to be lost.

Parameters:
newGridSize - New size of the grid

findTileAt

public int findTileAt(int x,
                      int y)
Finds the tileID of the tile stored at the grid square defined by the passed in coordinates (0 if no tile found).

Parameters:
x - X coordinate of location to check (in gridsquares)
y - Y coordinate of location to check (in gridsquares)
Returns:
The tileID of the tile found at the passed in coordinates. This returns zero if no tile was found or the TileSet is null

getGridSquareAt

public java.awt.Point getGridSquareAt(int x,
                                      int y)
Returns the coordinates of the grid square defined by the passed in mouse coordinates. The mouse coordinates are relative to the grid panel.

Parameters:
x - X location of mouse
y - Y location of mouse
Returns:
The coordinates of the grid square

getMouseOffset

public java.awt.Point getMouseOffset(int x,
                                     int y,
                                     int tileID)
Accepts mouse coordinates and a tileID and then returns the offset from the mouse location to the tile's upper left location.

Parameters:
x - Mouse X coordinate
y - Mouse Y coordinate
tileID - TileID to get the mouse offset of
Returns:
Offset from mouse position to tile's upper left location

isValidLoc

public boolean isValidLoc(int x,
                          int y,
                          int tID)
Determines if the grid coordinates defined by the passed in x and y values is a valid grid location for the tile with the passed in TileID

Parameters:
x - Grid X location to check
y - Grid Y location to check
tID - ID of the tile to check
Returns:
true if the location is valid; false otherwise.

eraseTilePosition

public void eraseTilePosition(int tileToErase)
This erases the passed in tile from the grid array. This funtion won't work if the tile's position has been modifed since it was last placed in its current grid position.

Parameters:
tileToErase - The TileID of the tile to erase from the grid

placeTileAt

public void placeTileAt(int x,
                        int y,
                        int tileToPlace)
Places the tile with the passed in tileID to the location defined by the passed in x and y values (in gridsquares).

Parameters:
x - Grid's X location to place the tile at
y - Grid's Y location to place the tile at
tileToPlace - ID of the tile to place

updateTileID

public void updateTileID(int newID,
                         int x,
                         int y,
                         int w,
                         int h)
Updates a grid location defined by the parameters with a new tileID that has been passed in.

Parameters:
newID - New ID number to use
x - X location to start the change
y - Y location to start the change
w - Width of tile
h - Height of tile

findLocForTile

public java.awt.Point findLocForTile(int xGridCenter,
                                     int yGridCenter,
                                     int curTile)
Finds a valid location for a tile to be placed in the grid. It takes a grid location to start the search which should be the aproximate center gridsquare of the tile. It also takes a tileID. It uses a breadth first search from the center of the tile position outward and up to half the tile's width and height. It returns a point representing a valid grid square to place the tile at or null if no valid location is found.

Parameters:
xGridCenter - Grid X location to start the search at
yGridCenter - Grid Y location to start the search at
curTile - Tile ID to check for placement
Returns:
Point representing a valid grid coordinate to place the tile at. null if no valid location can be found.