|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjava.awt.Component
java.awt.Canvas
ch.aplu.jgamegrid.GameGrid
public class GameGrid
Class to create a container where the actors live in. It is a two dimensional
grid of cells. The size of cells can be specified (in pixel units = distance
between two adjacent pixels) at GrameGrid's creation time,
and is constant after creation. Simple scenarios may use large cells that
entirely contain the representations of objects in a single cell.
More elaborate scenarios may use smaller cells (down to a single pixel unit)
to achieve fine-grained placement and smoother animation.
The background of the game grid can be decorated with drawings or a background
image.
The default constructor (parameterless) is used to create a canvas that can
be embedded in your own frame window or applet. You may use a GUI builder
because GameGrid is a Java bean. All other constructors embed the canvas
in a simple frame window with or without a navigation bar containing 3 buttons
(Run, Step, Reset) and a slider to select the simulation period.
GameGrid is derived from Canvas (a heavy weight component based on AWT)
in order to use hardware based rendering enhancement (page flipping).
In principle do not mix Swing and AWT components. For example you should
not add a GameGrid in a JScrollPane (use the AWT container ScrollPane instead).
(A GameGrid may be added to the content pane of a JFrame without harm.)
The cellSize is given in pixel units, e.g. the distance between adjacent pixels.
The size in pixel units of the usable playground is width = nbHorzCells * cellSize horizontally and
height = nbVertCells * cellSize vertically. Thus the playground contains nbHorzPixels = width + 1 pixels horizontally and
nbVertPixels = height + 1 pixels vertically, with pixel indexes 0 <= i <= width (inclusive),
0 <= k <= height (inclusive) respectively.
For pixel accurate positioning be aware that an image that has the size m x n in pixel units
contains m+1 pixels horizontally and n+1 pixels vertically.
So the background image must have nbVertPixels horizontally and
nbVerticalPixels vertically to fit exactly into the playground.
(E.g. GameGrid(60, 50, 10) will need a background image with 601 x 501 pixels.)
To increase performance the automatic repainting of the graphics
canvas is disabled. Repainting is done in every simulation cycle, when
the window is moved or by calling refresh(). setWindowListener()
registers a window listener that calls refresh() when the window is activated.
The x- and y-coordinates for positioning actors are actually the cell index ranging from
0 <= x < nbHorzCells (exclusive) horizontally and 0 <= y < nbVertCells (exclusive) vertically.
There image is automatically centered in the cell as accurate as possible.
The class design is inspired by the great Greenfoot programming environment
(see www.greenfoot.org) and by an article about game programming in Java
at www.cokeandcode.com/tutorials with thanks to the authors. The code
is entirely rewritten and in my responsability.
A extended sound library is included that supports MP3.
Using a sound converter integrated in the native DLL soundtouch.dll
(source from www.surina.net/soundtouch)
you may change pitch and tempo independently.
For the compilation you need the package ch.aplu.jaw and the
native DLL soundtouch.dll must be found in the system path. It only works
on Windows machines.
If a non-default constructor of GameGrid is used, all uncaught exceptions
are handled by a GGExceptionHandler that shows the
stack trace in a modal dialog box and terminates the application. This is
useful for GUI-based applications where no console window is visible.
To change this behavior, register a null GGExceptionHandler,
override getStackTrace() or register your own GGExceptionHandler.
For applets the Java standard exception handler is not modified because
this would cause a security exception.
Key strokes may be reported by a GGKeyListener or by polling the keyboard using
isKeyPressed() or kbhit(). Be aware that the game grid window must have the
focus so that the keys are active. You may deny to give the focus to other
components (e.g. to a text area or a second game grid in a GUI application)
by calling setFocusable(false).
Methods that access/modify the internal data structure
are made thread-safe. This does not mean that ALL methods are thread-safe.
Some library defaults of the GameGrid window can be modified by using a
Java properties file gamegrid.properties. For more details
consult gamegrid.properties file found in the distribution.
When the close button of the GameGrid window title bar is hit, System.exit(0)
is executed that terminates the JVM, but you can modifiy this behavior
by registering your own implementation of the ExitListener interface or by using
the key ClosingMode in gamegrid.properties.
All Swing methods are executed by the Event Dispatch Thread (EDT), so
all methods may be executed directly from any thread.
Nested Class Summary | |
---|---|
static class |
GameGrid.ClosingMode
Modes to determine what happens when the title bar close button is hit. |
Nested classes/interfaces inherited from class java.awt.Component |
---|
java.awt.Component.BaselineResizeBehavior |
Field Summary | |
---|---|
static GameGrid.ClosingMode |
AskOnClose
ClosingMode AskOnClose. |
java.awt.Color |
bgColor
The color of the background. |
java.lang.String |
bgImagePath
Path of the background image. |
int |
bgImagePosX
ULX of the background image with respect to playground. |
int |
bgImagePosY
ULY of the background image with respect to playground. |
static java.awt.Color |
BLACK
Short for Color.BLACK. |
static java.awt.Color |
BLUE
Short for Color.BLUE. |
int |
cellSize
Size of cells in pixels (square). |
static java.awt.Color |
CYAN
Short for Color.CYAN. |
static java.awt.Color |
DARKGRAY
Short for Color.DKGRAY. |
static GameGrid.ClosingMode |
DisposeOnClose
ClosingMode DisposeOnClose. |
static java.awt.Color |
GRAY
Short for Color.GRAY. |
static java.awt.Color |
GREEN
Short for Color.GREEN. |
java.awt.Color |
gridColor
Color of the grid. |
static java.awt.Color |
LIGHTGRAY
Short for Color.LTGRAY. |
static java.awt.Color |
MAGENTA
Short for Color.MAGENTY. |
static java.lang.Object |
monitor
Object used for synchronizing access of methods accessing the sceen list. |
int |
nbHorzCells
Number of horizonal cells. |
static int |
nbRotSprites
Number of rotated sprite images. |
int |
nbVertCells
Number of vertical cells. |
static GameGrid.ClosingMode |
NothingOnClose
ClosingMode NothingOnClose. |
static java.awt.Color |
PINK
Short for Color.RED. |
static java.awt.Color |
RED
Short for Color.RED. |
int |
simulationPeriod
Simulation period (in ms). |
static GameGrid.ClosingMode |
TerminateOnClose
ClosingMode TerminateOnClose. |
static java.awt.Color |
TRANSPARENT
Short for transparent color Color(0, 0, 0, 0) (black with alpha = 0). |
static java.awt.Color |
WHITE
Short for Color.WHITE. |
static java.awt.Color |
YELLOW
Short for Color.YELLOW. |
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 | |
---|---|
GameGrid()
Constructs the game playground with 10 by 10 cells (20 pixels wide). |
|
GameGrid(boolean isNavigation)
Constructs a game window including a playground of 10 by 10 cells (60 pixels wide) with possibly a navigation bar and a visible red grid but no background image. |
|
GameGrid(int nbHorzCells,
int nbVertCells)
Constructs a game window with one pixel cell size including a playground with no navigation bar, no visible grid and no background image. |
|
GameGrid(int nbHorzCells,
int nbVertCells,
int cellSize)
Constructs a game window including a playground with a navigation bar and no visible grid and no background image. |
|
GameGrid(int nbHorzCells,
int nbVertCells,
int cellSize,
boolean isNavigation)
Constructs a game window including a playground with no visible grid and no background image, but possibly a navigation bar. |
|
GameGrid(int nbHorzCells,
int nbVertCells,
int cellSize,
java.awt.Color gridColor)
Constructs a game window including a playground with a navigation bar and a possibly a visible grid, but no background image. |
|
GameGrid(int nbHorzCells,
int nbVertCells,
int cellSize,
java.awt.Color gridColor,
boolean isNavigation)
Constructs a game window including playground with possibly a navigation bar, possibly a visible grid and no background image. |
|
GameGrid(int nbHorzCells,
int nbVertCells,
int cellSize,
java.awt.Color gridColor,
java.lang.String bgImagePath)
Constructs a game window including a playground with a navigation bar, possibly a visible grid and possibly a background image. |
|
GameGrid(int nbHorzCells,
int nbVertCells,
int cellSize,
java.awt.Color gridColor,
java.lang.String bgImagePath,
boolean isNavigation)
Constructs a game window including a playground with possibly a navigation bar, possibly a visible grid and possibly a background image. |
|
GameGrid(int nbHorzCells,
int nbVertCells,
int cellSize,
java.awt.Color gridColor,
java.lang.String bgImagePath,
boolean isNavigation,
boolean undecorated)
Constructs a game window including a playground with possibly a navigation bar, possibly a visible grid, possibly a background image and possibly no decoration. |
|
GameGrid(int nbHorzCells,
int nbVertCells,
int cellSize,
java.awt.Color gridColor,
java.lang.String bgImagePath,
boolean isNavigation,
int nbRotSprites)
Constructs a game window including a playground with possibly a navigation bar, possibly a visible grid, and possibly a background image. |
|
GameGrid(int nbHorzCells,
int nbVertCells,
int cellSize,
java.awt.Color gridColor,
java.lang.String bgImagePath,
boolean isNavigation,
int nbRotSprites,
boolean undecorated)
Constructs a game window including a playground with possibly a navigation bar, possibly a visible grid, possibly a background image and and possibly no decoration. |
|
GameGrid(int nbHorzCells,
int nbVertCells,
int cellSize,
java.lang.String bgImagePath)
Constructs a game window including a playground with a navigation bar and a possibly a background image, but no visible grid. |
Method Summary | |
---|---|
void |
act()
Empty method called in every simulation cycle after all actor act() calls. |
void |
actAll()
Invokes all actor's act() methods in the order of the scene and draws the new game situation. |
void |
activate()
Brings the game grid window to the front and request the focus. |
void |
addActListener(GGActListener listener)
Registers the given GGActListener to get act() events. |
void |
addActor(Actor actor,
Location location)
Same as addActor(Actor actor, Location location, double direction) with direction = 0 (to east). |
void |
addActor(Actor actor,
Location location,
double direction)
Adds a new actor at given starting position with given moving direction to the scene. |
void |
addActor(Actor actor,
Location location,
Location.CompassDirection compassDir)
Same as addActor(Actor actor, Location location, double direction) with compass direction. |
void |
addActorNoRefresh(Actor actor,
Location location)
Same as addActor(Actor actor, Location location), but the game grid window is not refreshed automatically. |
void |
addActorNoRefresh(Actor actor,
Location location,
double direction)
Same as addActor(Actor actor, Location location, double direction), but the game grid window is not refreshed automatically. |
void |
addExitListener(GGExitListener listener)
Registers the given GGExitListener to get an event when the title bar close button is hit. |
void |
addKeyListener(GGKeyListener listener)
Adds a GGKeyListener to get events when a key is pressed. |
void |
addKeyRepeatListener(GGKeyRepeatListener listener)
Adds a GGKeyRepeatListener to get repeating events with default period (20 ms) when a key is pressed until the key is released. |
void |
addMouseListener(GGMouseListener listener,
int mouseEventMask)
Adds a GGMouseListener to get notifications from mouse events. |
void |
addNavigationListener(GGNavigationListener listener)
Register a navigation listener to get notifications when the navigation panel is used. |
void |
addResetListener(GGResetListener listener)
Registers the given GGResetListener to get events when the reset button is hit. |
void |
addStatusBar(int height)
Adds a status window attached at the bottom of the game grid window (below the navigation bar, if available). |
void |
addWindowStateListener(GGWindowStateListener listener)
Register a GGWindowStateListener that will report change of window location and iconification. |
GGTileMap |
createTileMap(int nbHorzTiles,
int nbVertTiles,
int tileWidth,
int tileHeight)
Creates/Replaces a TileMap with the specified number of horizontal and vertical tiles. |
static void |
delay(long time)
Delays execution for the given amount of time. |
void |
dispose()
Stops the game thread, hides the game window, disposes the frame and calls Monitor.wakeUp(). |
static void |
disposeAll()
Disposes the GameGrid instance. |
void |
doPause()
Pauses the simulation cycling. |
void |
doReset()
If still running, calls doPause() and restores actors to their initializing state. |
void |
doRun()
Starts the simulation cycling. |
void |
doStep()
Runs the the simulation loop once. |
java.util.ArrayList<Actor> |
getActors()
Returns all actors that are part of the game grid. |
java.util.ArrayList<Actor> |
getActors(java.lang.Class clazz)
Returns all actors of the specified class that are part of the game grid. |
java.util.ArrayList<Actor> |
getActorsAt(Location location)
Returns all actors at the given location that are part of the game grid. |
java.util.ArrayList<Actor> |
getActorsAt(Location location,
java.lang.Class clazz)
Returns all actors of given type at given location that are part of the game grid. |
java.awt.Dimension |
getAreaSize()
Returns the size of the occupied area (the frame window including navigation and status bar, if available). |
GGBackground |
getBg()
Returns a reference to the background of the game grid. |
java.awt.Color |
getBgColor()
Returns the color of the background. |
java.lang.String |
getBgImagePath()
Returns to path to the background image file. |
java.awt.Point |
getBgImagePos()
Returns the x-y-coordinates of the upper left vertex of the background image with respect to the playground pixel coordinates. |
int |
getBgImagePosX()
Returns the x-coordinate of the upper left vertex of the background image with respect to the playground pixel coordinates. |
int |
getBgImagePosY()
Returns the y-coordinate of the upper left vertex of the background image with respect to the playground pixel coordinates. |
int |
getCellSize()
Returns the size of a cell (in pixels). |
static GameGrid.ClosingMode |
getClosingMode()
Returns the closing mode read from the gamegrid.properties file. |
java.util.ArrayList<Location> |
getDiagonalLocations(Location location,
boolean up)
Returns a list of all grid locations that are on the diagonal line through the given location. |
int |
getDoubleClickDelay()
Returns the current double-click delay. |
java.util.ArrayList<Location> |
getEmptyLocations()
Returns a list of all locations not occupied by actors. |
javax.swing.JFrame |
getFrame()
Returns the JFrame reference of the surrounding window. |
java.awt.Color |
getGridColor()
Returns the color of the grid. |
java.awt.image.BufferedImage |
getImage()
Returns the BufferedImage of the current GameGrid window including the background and all actors. |
char |
getKeyChar()
Returns the character of the last key pressed and removes it from the one-key buffer. |
int |
getKeyCharWait()
Waits until a key is pressed and then returns the character of the last key pressed and removes it from the one-key buffer. |
int |
getKeyCode()
Returns the key code of the last key pressed and removes it from the one-key buffer. |
int |
getKeyCodeWait()
Waits until a key is pressed and then eeturns the key code of the last key pressed and removes it from the one-key buffer. |
int |
getKeyModifiers()
Returns the key modifier of the last key pressed. |
java.lang.String |
getKeyModifiersText()
Returns the key modifier as text of the last key pressed. |
java.util.ArrayList<Location> |
getLineLocations(Location loc1,
Location loc2,
boolean interjacent)
Returns a list of cell grid locations whose cell centers are exactly on the the line through the centers of loc1 and loc1. |
Location |
getMouseLocation()
Returns the cell location of the cell under the mouse pointer or null, if the game grid is not visible or obscured by another window. |
int |
getNbCycles()
Returns the number of simulation cycles since last reset. |
int |
getNbHorzCells()
Returns the horizontal number of cells. |
int |
getNbHorzPix()
Returns the horizontal number of pixels of the playground. |
int |
getNbVertCells()
Returns the vertical number of cells. |
int |
getNbVertPix()
Returns the vertical number of pixels of the playground. |
int |
getNumberOfActors()
Returns total number of actors in the scene. |
int |
getNumberOfActors(java.lang.Class clazz)
Returns number of actors of specified class. |
int |
getNumberOfActorsAt(Location location)
Returns number of actors at specified location. |
int |
getNumberOfActorsAt(Location location,
java.lang.Class clazz)
Returns number of actors of specified class at specified location. |
java.util.ArrayList<Location> |
getOccupiedLocations()
Returns a list of all locations occupied by actors. |
Actor |
getOneActor(java.lang.Class clazz)
Returns the actor of the specified class that is part of the game grid and is on top in the paint order. |
Actor |
getOneActorAt(Location location)
Returns the actor at the specified location that is part of the game grid and is on top in the paint order. |
Actor |
getOneActorAt(Location location,
java.lang.Class clazz)
Returns the actor of the specified class at the specified location that is part of the game grid and is on top in the paint order. |
java.util.ArrayList<Actor> |
getPaintOrderList()
Returns a list with actor references in the order they are painted. |
java.util.ArrayList<Actor> |
getPaintOrderList(java.lang.Class clazz)
Returns a list with actor references of the given class in the order they are painted. |
GGPanel |
getPanel()
Returns a reference to the GGPanel with current settings. |
GGPanel |
getPanel(double xmin,
double xmax,
double ymin,
double ymax)
Sets the coordinate system in GGPanel to given range and returns a reference to the GGPanel of the game grid. |
int |
getPgHeight()
Returns the height (vertical size) of the playground in pixel units. |
int |
getPgWidth()
Returns the width (horizontal size) of the playground in pixel units. |
java.awt.Point |
getPosition()
Returns the the location in screen coordiantes of game grid window. |
java.util.ArrayList<java.lang.Integer> |
getPressedKeyCodes()
Returns a list of the key codes of all keys currently pressed |
double |
getRandomDirection()
Returns a random direction 0..360 degrees. |
Location |
getRandomEmptyLocation()
Returns an empty random location within the game grid. |
Location |
getRandomLocation()
Returns a random location within the game grid. |
int |
getSimulationPeriod()
Returns the current simulation period. |
void |
getStackTrace(java.lang.String s)
Implementation of the GGExceptionHandler's event method called when an uncaught exception is thrown. |
GGTileMap |
getTileMap()
Returns the reference to the GGTileMap. |
java.util.ArrayList<Actor> |
getTouchedActors(java.lang.Class clazz)
Returns a list with actor references of all actors of the given class whose touched area (of type IMAGE, RECTANGLE or CIRCLE) intersects with the current mouse cursor location. |
static java.lang.String |
getVersion()
Returns current version information. |
void |
hide()
Hides the game grid, but does not destroy it. |
boolean |
isActorColliding(Actor a1,
Actor a2)
Checks if the two given actors are colliding. |
boolean |
isAtBorder(Location location)
Returns true, if the given cell location is at the grid border. |
static boolean |
isDisposed()
Returns true if closing mode DisposeOnClose is selected and the close button was hit. |
boolean |
isEmpty(Location location)
Returns true, if there is no actor at specified location. |
boolean |
isInGrid(Location location)
Returns true, if the given cell location is within the grid. |
boolean |
isKeyPressed(int keyCode)
Returns true if the key with the given key code is currently pressed. |
boolean |
isPaused()
Returns true, if the game is paused |
boolean |
isRunning()
Returns true, if the game is running. |
boolean |
isShown()
Returns the state if visibilty of the game grid window. |
boolean |
isTileColliding(Actor a,
Location location)
Checks if the given actor and the tile at the given tile location are colliding. |
boolean |
isUndecorated()
Returns true, if the window is undecorated (no title bar and no borders). |
boolean |
kbhit()
Returns true, if a key was press since the last call to getKeyChar() or getKeyCode(). |
void |
paint(java.awt.Graphics g)
For internal use only (overrides Canvas.paint() to get paint notifications). |
SoundPlayer |
playLoop(GGSound sound)
Play continously a distributed sound sample with maximum volume using the default sound device. |
SoundPlayer |
playLoop(java.lang.Object obj,
GGSound sound)
Play continously a distributed sound sample with maximum volume using the default sound device. |
SoundPlayer |
playLoop(java.lang.Object obj,
java.lang.String audioPathname)
Play continuously a sound sample from a JAR resource of the specified object with maximum volume using the default sound device. |
SoundPlayer |
playLoop(java.lang.String audioPathname)
Play continuously a sound sample with maximum volume using the default sound device using the current class loader to load the resource. |
SoundPlayerExt |
playLoopExt(java.lang.Object obj,
java.lang.String audioPathname)
Same as playLoop(obj, audioPathname) but supports MP3. |
SoundPlayerExt |
playLoopExt(java.lang.String audioPathname)
Same as playLoop(audioPathname) but supports MP3. |
SoundPlayer |
playSound(GGSound sound)
Plays a distributed sound sample with maximum volume using the default sound device. |
SoundPlayer |
playSound(java.lang.Object obj,
GGSound sound)
Play a distributed sound sample with maximum volume using the default sound device. |
SoundPlayer |
playSound(java.lang.Object obj,
java.lang.String audioPathname)
Play a sound sample from a JAR resource of the specified object with maximum volume using the default sound device. |
SoundPlayer |
playSound(java.lang.String audioPathname)
Play a sound sample from given audio file with maximum volume using the default sound device using the current class loader to load the resource. |
SoundPlayerExt |
playSoundExt(java.lang.Object obj,
java.lang.String audioPathname)
Same as playSound(obj, audioPathname) but supports MP3. |
SoundPlayerExt |
playSoundExt(java.lang.String audioPathname)
Same as playSound(audioPathname) but supports MP3. |
void |
refresh()
Refreshs the current game situation (repaint background, tiles, actors). |
boolean |
removeActor(Actor actor)
Removes the given actor from the scene, so that act() is not called any more. |
int |
removeActors(java.lang.Class clazz)
Removes all actors from the specified class, so that act() is not called any more. |
int |
removeActorsAt(Location location)
Removes all actors at the specified location, so that act() is not called any more. |
int |
removeActorsAt(Location location,
java.lang.Class clazz)
Removes all actors from the specified class at the specified location, so that act() is not called any more. |
int |
removeAllActors()
Removes all actors from the scene, so that act() is not called any more. |
void |
removeKeyListener(GGKeyListener listener)
Removes a previously registered GGKeyListener. |
void |
removeKeyRepeatListener(GGKeyRepeatListener listener)
Removes a previously registered GGKeyRepeatListener. |
boolean |
removeMouseListener(GGMouseListener listener)
Removes the given mouse listener from the list of registered mouse listeners. |
void |
reset()
Empty method called when the reset button is hit or doReset() is called. |
Actor |
reverseSceneOrder(java.util.ArrayList<Actor> actors)
Reverses the order of the given actor list in the corresponding scene list. |
void |
setActEnabled(boolean enable)
Enables/disables the simulation cycle momentarily. |
void |
setActOrder(java.lang.Class... classes)
Set the act order of objects in the scene. |
void |
setActorOnBottom(Actor actor)
Puts the given actor at first place in the corresponding scene list, in order to act last and to be drawn on the bottom of other actors of the same class. |
void |
setActorOnTop(Actor actor)
Puts the given actor at last place in the corresponding scene list, in order to act first and to be drawn on top of other actors of the same class. |
void |
setBgColor(java.awt.Color color)
Sets the color of the background. |
void |
setBgColor(int r,
int g,
int b)
Sets the color of the background. |
void |
setBgImagePath(java.lang.String bgImagePath)
Sets the path to the background image file. |
void |
setBgImagePos(java.awt.Point point)
Sets the x-y-coordinate of the upper left vertex of the background image with respect to the playground pixel coordinates. |
void |
setBgImagePosX(int x)
Sets the x-coordinate of the upper left vertex of the background image with respect to the playground pixel coordinates. |
void |
setBgImagePosY(int y)
Sets the y-coordinate of the upper left vertex of the background image with respect to the playground pixel coordinates. |
void |
setCellSize(int cellSize)
Sets the size of a cell. |
static void |
setClosingMode(GameGrid.ClosingMode closingMode)
Sets the closing mode that determines what happens when the title bar close button is hit. |
void |
setDoubleClickDelay(int delay)
Sets the time delay the system uses to distinct click and double-click mouse events. |
void |
setGridColor(java.awt.Color color)
Sets the color of the grid and reconstructs the background. |
int |
setKeyRepeatPeriod(int keyRepeatPeriod)
Sets the time between two successive key repeat events. |
void |
setMouseEnabled(boolean enabled)
Enable/disable all mouse event callbacks. |
void |
setNbHorzCells(int nbHorzCells)
Sets the horizontal number of cells. |
void |
setNbVertCells(int nbVertCells)
Sets the vertical number of cells. |
void |
setPaintOrder(java.lang.Class... classes)
Sets the paint order of objects in the game grid. |
void |
setPosition(int ulx,
int uly)
Move the location of the game grid window to the given screen position. |
void |
setSceneOrder(java.util.ArrayList<Actor> actors)
Sets the act and paint order of the actors in the given list by rearraging their scene list to the order in the given list. |
void |
setSimulationPeriod(int millisec)
Sets the period of the simulation loop. |
void |
setStatusText(java.lang.String text)
Replaces the text in the status bar by the given text using the default font and text color of JOptionPane. |
void |
setStatusText(java.lang.String text,
java.awt.Font font,
java.awt.Color color)
Replaces the text in the status bar by the given text using the given font and text color. |
void |
setTitle(java.lang.String text)
Set the title in the window's title bar. |
Actor |
shiftSceneOrder(java.util.ArrayList<Actor> actors,
boolean forward)
Shifts (rolls) the actors of the given actor list in the corresponding scene list forward or backward. |
void |
show()
Shows the game grid after initialisation or when hided. |
void |
showStatusBar(boolean show)
Shows or hides the status bar. |
void |
stopGameThread()
Stops the game thread to avoid any CPU consumption. |
Location |
toLocation(int x,
int y)
Returns the location (cell indices) of the cell where the point with given coordinates resides. |
Location |
toLocation(java.awt.Point pt)
Returns the location (cell indices) of the cell where the given point resides. |
Location |
toLocationInGrid(int x,
int y)
Returns the location (cell indices) of the cell where the point with given coordinates resides. |
Location |
toLocationInGrid(java.awt.Point pt)
Returns the location (cell indices) of the cell where the given point resides. |
java.awt.Point |
toPoint(Location location)
Returns the x-y-coordinates of the center of the cell with given location (cell indices). |
Methods inherited from class java.awt.Canvas |
---|
addNotify, createBufferStrategy, createBufferStrategy, getAccessibleContext, getBufferStrategy, update |
Methods inherited from class java.awt.Component |
---|
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, bounds, checkImage, checkImage, contains, contains, createImage, createImage, createVolatileImage, createVolatileImage, deliverEvent, disable, dispatchEvent, doLayout, enable, enable, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getAlignmentX, getAlignmentY, getBackground, getBaseline, getBaselineResizeBehavior, getBounds, getBounds, getColorModel, getComponentAt, getComponentAt, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeys, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphics, getGraphicsConfiguration, getHeight, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getListeners, getLocale, getLocation, getLocation, getLocationOnScreen, getMaximumSize, getMinimumSize, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPreferredSize, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getSize, getToolkit, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, imageUpdate, inside, invalidate, isBackgroundSet, isCursorSet, isDisplayable, isDoubleBuffered, isEnabled, isFocusable, isFocusCycleRoot, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, layout, list, list, list, list, list, locate, location, lostFocus, minimumSize, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, preferredSize, prepareImage, prepareImage, print, printAll, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removeNotify, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, repaint, requestFocus, requestFocusInWindow, reshape, resize, resize, setBackground, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setFocusable, setFocusTraversalKeys, setFocusTraversalKeysEnabled, setFont, setForeground, setIgnoreRepaint, setLocale, setLocation, setLocation, setMaximumSize, setMinimumSize, setName, setPreferredSize, setSize, setSize, setVisible, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle, validate |
Methods inherited from class java.lang.Object |
---|
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
public static final GameGrid.ClosingMode TerminateOnClose
public static final GameGrid.ClosingMode AskOnClose
public static final GameGrid.ClosingMode DisposeOnClose
public static final GameGrid.ClosingMode NothingOnClose
public static final java.awt.Color TRANSPARENT
public static final java.awt.Color BLACK
public static final java.awt.Color BLUE
public static final java.awt.Color CYAN
public static final java.awt.Color DARKGRAY
public static final java.awt.Color GRAY
public static final java.awt.Color GREEN
public static final java.awt.Color LIGHTGRAY
public static final java.awt.Color MAGENTA
public static final java.awt.Color PINK
public static final java.awt.Color RED
public static final java.awt.Color WHITE
public static final java.awt.Color YELLOW
public java.awt.Color gridColor
public int nbHorzCells
public int nbVertCells
public int cellSize
public java.lang.String bgImagePath
public int bgImagePosX
public int bgImagePosY
public java.awt.Color bgColor
public int simulationPeriod
public static int nbRotSprites
public static final java.lang.Object monitor
Constructor Detail |
---|
public GameGrid()
public GameGrid(boolean isNavigation)
isNavigation
- if true, a navigation bar is shownpublic GameGrid(int nbHorzCells, int nbVertCells, int cellSize)
nbHorzCells
- the number of horizontal cellsnbVertCells
- the number of vertical cellscellSize
- the side length (in pixelUnits) of the cellpublic GameGrid(int nbHorzCells, int nbVertCells)
nbHorzCells
- the number of horizontal cellsnbVertCells
- the number of vertical cellspublic GameGrid(int nbHorzCells, int nbVertCells, int cellSize, boolean isNavigation)
nbHorzCells
- the number of horizontal cellsnbVertCells
- the number of vertical cellscellSize
- the side length (in pixel units) of the cellisNavigation
- if true, a navigation bar is shownpublic GameGrid(int nbHorzCells, int nbVertCells, int cellSize, java.lang.String bgImagePath)
nbHorzCells
- the number of horizontal cellsnbVertCells
- the number of vertical cellscellSize
- the side length (in pixel units) of the cellbgImagePath
- the path or URL to a background image (if null, no background image)public GameGrid(int nbHorzCells, int nbVertCells, int cellSize, java.awt.Color gridColor)
nbHorzCells
- the number of horizontal cellsnbVertCells
- the number of vertical cellscellSize
- the side length (in pixel units) of the cellgridColor
- the color of the grid (if null, no grid is shown)public GameGrid(int nbHorzCells, int nbVertCells, int cellSize, java.awt.Color gridColor, java.lang.String bgImagePath)
nbHorzCells
- the number of horizontal cellsnbVertCells
- the number of vertical cellscellSize
- the side length (in pixel units) of the cellgridColor
- the color of the grid (if null, no grid is shown)bgImagePath
- the path or URL to a background image (if null, no background image)public GameGrid(int nbHorzCells, int nbVertCells, int cellSize, java.awt.Color gridColor, boolean isNavigation)
nbHorzCells
- the number of horizontal cellsnbVertCells
- the number of vertical cellscellSize
- the side length (in pixel units) of the cellgridColor
- the color of the grid (if null, no grid is shown)isNavigation
- if true, a navigation bar is shownpublic GameGrid(int nbHorzCells, int nbVertCells, int cellSize, java.awt.Color gridColor, java.lang.String bgImagePath, boolean isNavigation)
nbHorzCells
- the number of horizontal cellsnbVertCells
- the number of vertical cellscellSize
- the side length (in pixel units) of the cellgridColor
- the color of the grid (if null, no grid is shown)bgImagePath
- the path to a background image (if null, no background image)isNavigation
- if true, a navigation bar is shownpublic GameGrid(int nbHorzCells, int nbVertCells, int cellSize, java.awt.Color gridColor, java.lang.String bgImagePath, boolean isNavigation, boolean undecorated)
nbHorzCells
- the number of horizontal cellsnbVertCells
- the number of vertical cellscellSize
- the side length (in pixel units) of the cellgridColor
- the color of the grid (if null, no grid is shown)bgImagePath
- the path to a background image (if null, no background image)isNavigation
- if true, a navigation bar is shownundecorated
- if true, the window has no title bar and no borderspublic GameGrid(int nbHorzCells, int nbVertCells, int cellSize, java.awt.Color gridColor, java.lang.String bgImagePath, boolean isNavigation, int nbRotSprites)
nbHorzCells
- the number of horizontal cellsnbVertCells
- the number of vertical cellscellSize
- the side length (in pixel units) of the cellgridColor
- the color of the grid (if null, no grid is shown)bgImagePath
- the path to a background image (if null, no background image)isNavigation
- if true, a navigation bar is shownnbRotSprites
- the number of preloaded rotated sprite images for
all actors (default: 60). Be aware that if you change this parameter, your actors
must be created AFTER the GameGrid constructor terminates, because the
actor's creation process uses this parameterpublic GameGrid(int nbHorzCells, int nbVertCells, int cellSize, java.awt.Color gridColor, java.lang.String bgImagePath, boolean isNavigation, int nbRotSprites, boolean undecorated)
nbHorzCells
- the number of horizontal cellsnbVertCells
- the number of vertical cellscellSize
- the side length (in pixel units) of the cellgridColor
- the color of the grid (if null, no grid is shown)bgImagePath
- the path to a background image (if null, no background image)isNavigation
- if true, a navigation bar is shownnbRotSprites
- the number of preloaded rotated sprite images for
all actors (default: 60). Be aware that if you change this parameter, your actors
must be created AFTER the GameGrid constructor terminates, because the
actor's creation process uses this parameterundecorated
- if true, the window has no title bar and no bordersActor
Method Detail |
---|
public java.lang.String getBgImagePath()
public void setBgImagePath(java.lang.String bgImagePath)
bgImagePath
- the path to the image file; if null, no background
image is assumedpublic int getBgImagePosX()
public void setBgImagePosX(int x)
x
- the ULX of the background imagepublic int getBgImagePosY()
public void setBgImagePosY(int y)
y
- the ULY of the background imagepublic java.awt.Point getBgImagePos()
public void setBgImagePos(java.awt.Point point)
point
- the ULX/ULY of the background imagepublic java.awt.Color getBgColor()
public void setBgColor(java.awt.Color color)
color
- the background color. Alpha channel ignoredpublic void setBgColor(int r, int g, int b)
r
- the red value of the color RGBg
- the green value of the color RGBb
- the blue value of the color RGBpublic int getNbHorzCells()
public void setNbHorzCells(int nbHorzCells)
nbHorzCells
- the number of cells in horizontal directionpublic int getNbVertCells()
public void setNbVertCells(int nbVertCells)
nbVertCells
- the number of cells in vertical directionpublic java.awt.Color getGridColor()
public void setGridColor(java.awt.Color color)
color
- the grid color; null, if no grid is desiredpublic int getNbHorzPix()
public int getNbVertPix()
public int getPgWidth()
public int getPgHeight()
public int getCellSize()
public void setCellSize(int cellSize)
cellSize
- the length of the cell side in pixel units.public GGBackground getBg()
public GGPanel getPanel()
public GGPanel getPanel(double xmin, double xmax, double ymin, double ymax)
public void addActorNoRefresh(Actor actor, Location location)
actor
- the actor to be added at the end of the scene listlocation
- the location of the actor (cell indices, value copy)public void addActorNoRefresh(Actor actor, Location location, double direction)
actor
- the actor to be added at the end of the scene listlocation
- the location of the actor (cell indices, value copy)direction
- the direction (clockwise in degrees, 0 to east)public void addActor(Actor actor, Location location, double direction)
actor
- the actor to be added at the end of the scene listlocation
- the location of the actor (cell indices, value copy)direction
- the direction (clockwise in degrees, 0 to east)public void addActor(Actor actor, Location location)
actor
- the actor to be added at the end of the scene listlocation
- the location of the actor (cell indices, value copy)public void addActor(Actor actor, Location location, Location.CompassDirection compassDir)
actor
- the actor to be added at the end of the scene listlocation
- the location of the actor (cell indices)compassDir
- the compass directionpublic java.awt.image.BufferedImage getImage()
public void refresh()
public void actAll()
public void setActOrder(java.lang.Class... classes)
classes
- the classes in desired act orderpublic void setPaintOrder(java.lang.Class... classes)
classes
- the classes in desired paint orderpublic static void delay(long time)
time
- the delay time (in ms)public void doRun()
public void doPause()
public void doStep()
public void doReset()
public javax.swing.JFrame getFrame()
public int getSimulationPeriod()
public void setSimulationPeriod(int millisec)
millisec
- the period of the simulation loop (in milliseconds)public boolean kbhit()
public char getKeyChar()
public int getKeyCharWait()
public int getKeyCode()
public int getKeyCodeWait()
public int getKeyModifiers()
public java.lang.String getKeyModifiersText()
public boolean isKeyPressed(int keyCode)
keyCode
- the code of the key you are interested in (a constant
defined in java.awt.event.KeyCode).
public void addKeyListener(GGKeyListener listener)
listener
- the GGKeyListener to registerpublic void removeKeyListener(GGKeyListener listener)
listener
- the GGKeyListener to removepublic boolean isInGrid(Location location)
public boolean isAtBorder(Location location)
public java.util.ArrayList<Location> getOccupiedLocations()
public java.util.ArrayList<Location> getEmptyLocations()
public java.util.ArrayList<Actor> getActors()
public java.util.ArrayList<Actor> getActorsAt(Location location, java.lang.Class clazz)
location
- the location of the cellclazz
- class type of the actors to be returned (e.g. Fish.class),
if clazz is null, all actors are returned
public java.util.ArrayList<Actor> getActorsAt(Location location)
public java.util.ArrayList<Actor> getActors(java.lang.Class clazz)
clazz
- the class of the actors to look for, if null all actors are returned
public Actor getOneActorAt(Location location, java.lang.Class clazz)
location
- the location of the cellclazz
- the class of the actors to look for, if null all actors are considered
public Actor getOneActor(java.lang.Class clazz)
clazz
- the class of the actors to look for, if null all actors are considered
public Actor getOneActorAt(Location location)
location
- the location of the cell
public int getNumberOfActors()
public int getNumberOfActorsAt(Location location)
public int getNumberOfActorsAt(Location location, java.lang.Class clazz)
public int getNumberOfActors(java.lang.Class clazz)
public boolean isEmpty(Location location)
public boolean removeActor(Actor actor)
actor
- the actor to be removed
public int removeAllActors()
public int removeActors(java.lang.Class clazz)
clazz
- class of the actors to be removed, if null all actors are removed
public int removeActorsAt(Location location, java.lang.Class clazz)
location
- the location of the cellclazz
- class of the actors to be removed, if null all actors are removed
public int removeActorsAt(Location location)
location
- the location of the cell
public Location getRandomLocation()
public double getRandomDirection()
public Location getRandomEmptyLocation()
public boolean isRunning()
public boolean isPaused()
public boolean isActorColliding(Actor a1, Actor a2)
a1
- the first actora2
- the second actor
public boolean isTileColliding(Actor a, Location location)
a
- the actor to check for collisionlocation
- the tile location
public SoundPlayer playSound(java.lang.Object obj, GGSound sound)
playSound(GGSound sound)
sound
- the named sound sample from the GGSound enumerationobj
- object whose class loader is used to load the resourcepublic SoundPlayer playSound(GGSound sound)
sound
- the named sound sample from the GGSound enumerationpublic SoundPlayer playLoop(java.lang.Object obj, GGSound sound)
playLoop(GGSound sound)
sound
- the named sound sample from the GGSound enumerationobj
- object whose class loader is used to load the resourcepublic SoundPlayer playLoop(GGSound sound)
sound
- the named sound sample from the GGSound enumerationpublic SoundPlayer playSound(java.lang.Object obj, java.lang.String audioPathname)
obj
- object whose class loader is used to load the resourceaudioPathname
- path to the sound filepublic SoundPlayer playSound(java.lang.String audioPathname)
audioPathname
- path to the sound filepublic SoundPlayer playLoop(java.lang.Object obj, java.lang.String audioPathname)
obj
- object whose class loader is used to load the resourceaudioPathname
- path to the sound filepublic SoundPlayer playLoop(java.lang.String audioPathname)
audioPathname
- path to the sound filepublic SoundPlayerExt playSoundExt(java.lang.Object obj, java.lang.String audioPathname)
public SoundPlayerExt playSoundExt(java.lang.String audioPathname)
public SoundPlayerExt playLoopExt(java.lang.Object obj, java.lang.String audioPathname)
public SoundPlayerExt playLoopExt(java.lang.String audioPathname)
public boolean removeMouseListener(GGMouseListener listener)
listener
- the listener to remove
public void addMouseListener(GGMouseListener listener, int mouseEventMask)
listener
- the GGMouseListener to registermouseEventMask
- an OR-combinaton of constants defined in class GGMousepublic void setMouseEnabled(boolean enabled)
enabled
- if true, the registered callbacks are enabled; otherwise disabledpublic java.awt.Point toPoint(Location location)
location
- the indices of the cell
public Location toLocation(java.awt.Point pt)
pt
- a point of pixels coordinates
public Location toLocation(int x, int y)
x
- x-coordinate (in pixels)y
- y-coordinate (in pixels)
public Location toLocationInGrid(java.awt.Point pt)
pt
- a point of pixels coordinates
public Location toLocationInGrid(int x, int y)
x
- x-coordinate (in pixels)y
- y-coordinate (in pixels)
public void setActorOnTop(Actor actor)
actor
- the actor to put on topsetPaintOrder(Class... classes)
public void setActorOnBottom(Actor actor)
actor
- the actor to put on the bottomsetPaintOrder(Class... classes)
public Actor shiftSceneOrder(java.util.ArrayList<Actor> actors, boolean forward)
actors
- a list of actors to shiftforward
- if true, shifts to the right (forward); otherwise
shifts to the left (backward)
public Actor reverseSceneOrder(java.util.ArrayList<Actor> actors)
actors
- a list of actors to shift
public void setSceneOrder(java.util.ArrayList<Actor> actors)
actors
- a list of actors to rearrangepublic java.util.ArrayList<Actor> getPaintOrderList(java.lang.Class clazz)
clazz
- the class of actors included in the list; if null, all actors are included
public java.util.ArrayList<Actor> getPaintOrderList()
public java.util.ArrayList<Actor> getTouchedActors(java.lang.Class clazz)
clazz
- the class of actors checked for a touch. If null, all
actors are checked.
public void stopGameThread()
public void paint(java.awt.Graphics g)
paint
in class java.awt.Canvas
public void addActListener(GGActListener listener)
listener
- the GGActListener to registerpublic GGTileMap createTileMap(int nbHorzTiles, int nbVertTiles, int tileWidth, int tileHeight)
nbHorzTiles
- the number of horizontal tilesnbVertTiles
- the number of vertical tilestileWidth
- the width of each tile (in pixels)tileHeight
- the height of each tile (in pixels)public GGTileMap getTileMap()
public int getNbCycles()
public void act()
act
in interface GGActListener
public void reset()
public void show()
show
in class java.awt.Component
public void hide()
hide
in class java.awt.Component
public void setTitle(java.lang.String text)
text
- the text to displaypublic void addNavigationListener(GGNavigationListener listener)
listener
- the GGNavigationListener to registerpublic void addResetListener(GGResetListener listener)
listener
- the GGActListener to registerpublic void addExitListener(GGExitListener listener)
listener
- the GGActListener to registerpublic static java.lang.String getVersion()
public void getStackTrace(java.lang.String s)
s
- the stack trace informationpublic void setPosition(int ulx, int uly)
ulx
- the upper left vertex x-coordinate of the window in pixelsuly
- the upper left vertex y-coordinate of the window in pixelspublic java.awt.Point getPosition()
public boolean isUndecorated()
public void addWindowStateListener(GGWindowStateListener listener)
listener
- the GGFrameStateListener to registerpublic void activate()
public void addStatusBar(int height)
height
- the height in pixels of the status windowpublic void setStatusText(java.lang.String text)
text
- the new text to show in the status barpublic void setStatusText(java.lang.String text, java.awt.Font font, java.awt.Color color)
text
- the new text to show in the status barfont
- the text fontcolor
- the text colorpublic void showStatusBar(boolean show)
show
- if true, a hidden status bar is shown even if the game grid
window is hidden; if false, the status bar is hidden and not shown until
showStatusBar(true) is called.public java.awt.Dimension getAreaSize()
public Location getMouseLocation()
public java.util.ArrayList<Location> getDiagonalLocations(Location location, boolean up)
location
- the location where the diagonal line is fixedup
- if true, the increasing diagonal line (y = x + b) is used; otherwise
the decreasing diagonal line (y = -x + b) is used
public java.util.ArrayList<Location> getLineLocations(Location loc1, Location loc2, boolean interjacent)
loc1
- the location of the first cellloc2
- the location of the second cellinterjacent
- if true, only location between loc1 and loc2 are
included
public void setActEnabled(boolean enable)
enable
- if true, a running simulation cycle is interrupted; otherwise
a running simulation cycles is reenabledpublic void setDoubleClickDelay(int delay)
delay
- the double click delay time in milliseconds used when a
double-click event is registeredpublic int getDoubleClickDelay()
public boolean isShown()
public void addKeyRepeatListener(GGKeyRepeatListener listener)
listener
- the GGKeyRepeatListener to registerpublic void removeKeyRepeatListener(GGKeyRepeatListener listener)
listener
- the GGKeyRepeatListener to removepublic int setKeyRepeatPeriod(int keyRepeatPeriod)
keyRepeatPeriod
- the new key repeat period (in ms)
public java.util.ArrayList<java.lang.Integer> getPressedKeyCodes()
public static void setClosingMode(GameGrid.ClosingMode closingMode)
closingMode
- one of the items in enum GameGrid.ClosingModepublic static GameGrid.ClosingMode getClosingMode()
public static boolean isDisposed()
public void dispose()
public static void disposeAll()
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |