public class Sprite
extends java.lang.Object
implements java.awt.event.KeyListener, java.awt.event.MouseListener
Modifier and Type | Class and Description |
---|---|
static class |
Sprite.BoundaryPolicy
How to handle sprites that reach the boundaries of the board.
|
Modifier and Type | Field and Description |
---|---|
protected Board |
board
the game board.
|
protected java.awt.Rectangle |
bounds
The bounds of the sprite movement.
|
protected java.awt.Dimension |
dimension
Dimensions of the sprite.
|
protected ImageBase |
image
the image that is displayed.
|
protected Sprite.BoundaryPolicy |
policy
what happens when the sprite hits the boundaries.
|
protected java.awt.geom.Point2D.Double |
position
Position of the sprite.
|
protected Velocity |
velocity
velocity of the sprite.
|
protected boolean |
visible
indicates whether the sprite is visible.
|
Constructor and Description |
---|
Sprite(Board board,
java.awt.Point position,
Sprite.BoundaryPolicy policy,
ImagePack image)
Creates a new sprite.
|
Sprite(Board board,
java.awt.Point position,
Sprite.BoundaryPolicy policy,
SimpleImage image)
Creates a new sprite.
|
Modifier and Type | Method and Description |
---|---|
protected java.awt.Polygon |
absoluteBorder()
Calculate the border in relation to the current position.
|
void |
draw(java.awt.Graphics g)
Draw the sprite using the given graphics context.
|
void |
draw(java.awt.Graphics g,
java.awt.image.ImageObserver observer)
Draw the sprite.
|
protected void |
ensureBoundaryPolicy()
Ensure that the boundary policy of the sprite is obeyed.
|
java.awt.Dimension |
getDimension()
Get the dimension (width, height) of the sprite.
|
int |
getHeight()
Width of the sprite.
|
java.awt.Point |
getPosition()
Get the current position.
|
Velocity |
getVelocity()
Get the velocity of the sprite.
|
int |
getWidth()
Width of the sprite.
|
<T extends Sprite> |
intersects(java.util.List<T> others)
Checks the given other objects regarding an intersection
with this object.
|
boolean |
intersects(java.awt.Point p)
Checks whether the given point is inside the sprite or not.
|
boolean |
intersects(java.awt.Polygon p2)
Checks whether this sprite intersects with the given polygon.
|
boolean |
intersects(java.awt.Rectangle other)
Checks whether this sprite intersects with the given rectangle.
|
boolean |
intersects(Sprite other)
Checks whether this sprite intersects with the given sprite.
|
boolean |
isActive()
Is the sprite active, i.e. takes part in the collision detection.
|
boolean |
isVisible()
Is the sprite visible.
|
void |
keyPressed(java.awt.event.KeyEvent e) |
void |
keyReleased(java.awt.event.KeyEvent e) |
void |
keyTyped(java.awt.event.KeyEvent e) |
static java.awt.Polygon |
loadPolygonFromFile(java.lang.String path,
java.lang.String fileName)
Load a polygon definition from a file.
|
protected void |
mouseClicked()
Called when the mouse was clicked inside the sprite.
|
void |
mouseClicked(java.awt.event.MouseEvent e) |
void |
mouseEntered(java.awt.event.MouseEvent e) |
void |
mouseExited(java.awt.event.MouseEvent e) |
protected void |
mousePressed()
Called when the mouse was pressed inside the sprite.
|
void |
mousePressed(java.awt.event.MouseEvent e) |
void |
mouseReleased(java.awt.event.MouseEvent e) |
void |
move()
Move the sprite according to the defined speed.
|
void |
move(Velocity deltaV)
Moves the sprite.
|
void |
setActive(boolean active)
Sets activity state of the sprite, i.e. if the sprite should take
part in collision detection.
|
void |
setBorder(java.awt.Polygon border)
Sets the border of the sprite (for collision detection).
|
void |
setBounds(java.awt.Rectangle bounds)
Overwrite the bounds automatically determined from the
board.
|
void |
setPosition(java.awt.Point position)
Sets the position.
|
void |
setVelocity(Velocity velocity)
Set the velocity of the sprite.
|
void |
setVisible(java.lang.Boolean visible)
Sets the visibility.
|
void |
stop()
Stop the movement of the sprite.
|
protected Board board
protected java.awt.Rectangle bounds
protected java.awt.Dimension dimension
protected ImageBase image
protected Sprite.BoundaryPolicy policy
protected java.awt.geom.Point2D.Double position
protected Velocity velocity
protected boolean visible
public Sprite(Board board, java.awt.Point position, Sprite.BoundaryPolicy policy, ImagePack image)
board
- the board the sprite is displayed onposition
- position of the upper left corner of the spritepolicy
- the policy when the sprite reaches the boundaries
of the boardimage
- the imagespublic Sprite(Board board, java.awt.Point position, Sprite.BoundaryPolicy policy, SimpleImage image)
board
- the board the sprite is displayed onposition
- position of the upper left corner of the spritepolicy
- the policy when the sprite reaches the boundaries
of the boardimage
- the single imageprotected java.awt.Polygon absoluteBorder()
public void draw(java.awt.Graphics g)
null
.g
- graphics contextpublic void draw(java.awt.Graphics g, java.awt.image.ImageObserver observer)
g
- graphics contextobserver
- image observerprotected void ensureBoundaryPolicy()
public final java.awt.Dimension getDimension()
public final int getHeight()
getDimension().getHeight()
.public java.awt.Point getPosition()
public final Velocity getVelocity()
public final int getWidth()
getDimension().getWidth()
.public <T extends Sprite> boolean intersects(java.util.List<T> others)
T
- Type of the objectsothers
- the otherstrue
if there is an intersection, otherwise false
public boolean intersects(java.awt.Point p)
p
- the point to test for intersection with the spritetrue
if there is an intersection, otherwise false
public boolean intersects(java.awt.Polygon p2)
p2
- the polygon.true
if there is an intersection, otherwise false
public boolean intersects(java.awt.Rectangle other)
other
- the rectangle.true
if there is an intersection, otherwise false
public boolean intersects(Sprite other)
other
- the other sprite.true
if there is an intersection, otherwise false
public final boolean isActive()
public final boolean isVisible()
public void keyPressed(java.awt.event.KeyEvent e)
keyPressed
in interface java.awt.event.KeyListener
KeyListener.keyPressed(java.awt.event.KeyEvent)
public void keyReleased(java.awt.event.KeyEvent e)
keyReleased
in interface java.awt.event.KeyListener
KeyListener.keyReleased(java.awt.event.KeyEvent)
public void keyTyped(java.awt.event.KeyEvent e)
keyTyped
in interface java.awt.event.KeyListener
KeyListener.keyTyped(java.awt.event.KeyEvent)
public static java.awt.Polygon loadPolygonFromFile(java.lang.String path, java.lang.String fileName)
path
- directoryfileName
- name of the fileprotected void mouseClicked()
public void mouseClicked(java.awt.event.MouseEvent e)
mouseClicked
in interface java.awt.event.MouseListener
MouseListener.mouseClicked(java.awt.event.MouseEvent)
public void mouseEntered(java.awt.event.MouseEvent e)
mouseEntered
in interface java.awt.event.MouseListener
MouseListener.mouseEntered(java.awt.event.MouseEvent)
public void mouseExited(java.awt.event.MouseEvent e)
mouseExited
in interface java.awt.event.MouseListener
MouseListener.mouseExited(java.awt.event.MouseEvent)
protected void mousePressed()
public void mousePressed(java.awt.event.MouseEvent e)
mousePressed
in interface java.awt.event.MouseListener
MouseListener.mousePressed(java.awt.event.MouseEvent)
public void mouseReleased(java.awt.event.MouseEvent e)
mouseReleased
in interface java.awt.event.MouseListener
MouseListener.mouseReleased(java.awt.event.MouseEvent)
public void move()
public void move(Velocity deltaV)
deltaV
- delta velocity to add to the current velocity of the
spritepublic final void setActive(boolean active)
intersects
methods but is just there to provide a flag
for the user of the sprite to tag it as active/inactive.active
- state to be setpublic final void setBorder(java.awt.Polygon border)
border
- the border to setpublic void setBounds(java.awt.Rectangle bounds)
bounds
- the new boundspublic void setPosition(java.awt.Point position)
position
- The new Position to be set.public final void setVelocity(Velocity velocity)
velocity
- velocitypublic final void setVisible(java.lang.Boolean visible)
visible
- visibility statepublic void stop()