Package sglib
Class StageObject
- java.lang.Object
-
- sglib.StageObject
-
- All Implemented Interfaces:
java.lang.Runnable
public class StageObject extends java.lang.Object implements java.lang.Runnable
-
-
Constructor Summary
Constructors Constructor Description StageObject(java.lang.String id, int x, int y, java.lang.String img, int scale)
Constructors for stage object.StageObject(Stage stage, int x, int y)
Constructors for stage object.StageObject(Stage stage, int x, int y, java.lang.String img)
Constructors for stage object.StageObject(Stage stage, int x, int y, java.lang.String img, int scale)
Constructors for stage object.StageObject(Stage stage, java.lang.String id, int x, int y, java.lang.String img)
Constructors for stage object.StageObject(Stage stage, java.lang.String id, int x, int y, java.lang.String img, int scale)
Constructors for stage object.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
act()
Action method called by run every program cycle.void
addImage(java.lang.String img)
Add an image without unique identifier (use image name as id) for use of setImage with index only.void
addImage(java.lang.String id, java.lang.String img)
Add a new image with a unique identifier.void
bounceOffBoundary()
Let object bounce off the boundary (thus update the angle) depending on StageObject.hitBoundary (which may be either BOTTOM, TOP, LEFT or RIGHT) and reset StageObject.hitBoundary to NONE.void
changeX(double mod)
Modify x-value of position and check whether out of stage boundary.void
changeY(double mod)
Modify y-value of position and check whether out of stage boundary.boolean
checkCollision(StageObject obj)
Check whether two objects collide.void
draw(java.awt.Graphics g)
Draw image if not hidden.double
getAngle()
Get rotation angle of object from 0 to 360 degrees. 0 points east, going counter clock wise.double
getDistance(double px, double py)
Get distance from object to position (px,py).double
getDistance(StageObject obj)
Get distance from this object to object obj.double
getHeight()
Get current height of object.int
getHitBoundary()
Return which part of boundary has been hit.java.lang.String
getId()
Get unique identifier of this stage object.int
getRotationStyle()
Get current rotation style.double
getScaleFactor()
Get current scale factor (100 = 100%)double
getWidth()
Get current width of object.double
getX()
Return x-value of objects position (center).double
getY()
Return y-value of objects position (center).boolean
hasHitBoundary()
Return true if last update of position caused object to hit the stage boundaryvoid
hide()
Hide object from stage.boolean
isHidden()
Return whether object is currently hidden.void
lookAt(double px, double py)
Rotate object (update angle) to point to given position.void
lookAt(StageObject obj)
Rotate object (update angle) to point to given object.void
lookAtPointer()
Rotate object (update angle) to point to mouse cursor position.void
move(double steps)
Move object forward in direction of current rotation angle.void
moveTo(double px, double py)
Move object directly to a position on stage.void
moveTo(StageObject obj)
Move object directly to position of another object.void
nextImage()
Select next sprite in list (thus change appearance).void
rotate(double mod)
Rotate object (in degrees).void
rotateLeft(double mod)
Rotate object by some degrees to the left.void
rotateRight(double mod)
Rotate object by some degrees to the right.void
run()
Called on stage.start() when threads are started and calls act() every program cycle.void
scale(double perc)
Set scale factor for object in percent (100 = original size).void
scaleDown(double mod)
Modify current scale factor by subtracting this value.void
scaleUp(double mod)
Modify current scale factor by adding this value.void
setId(java.lang.String id)
Change id used to look up object.void
setImage(int pos)
Choose image by index in list (thus change appearance).void
setImage(java.lang.String id)
Select image (thus change appearance).void
setRotation(double angle)
Directly set rotation of object.void
setRotationStyle(int style)
By default object is rotated according to current angle (style FULL).void
setStage(Stage stage)
Bind a stage object to a stage.void
setText(java.lang.String t, int s)
Set text and font size.void
setX(double nx)
Set x-value of position and check whether out of stage boundary.void
setY(double ny)
Set y-value of position and check whether out of stage boundary.void
show()
Show object on stage (that means draw it).boolean
touches(StageObject obj)
Same as checkCollision().void
wait(int msecs)
Wait some time (given in milliseconds).boolean
wasClicked()
Check whether object was clicked (mouse pointer on it).boolean
wasLeftClicked()
Check whether object was clicked with left mouse button.boolean
wasRightClicked()
Check whether object was clicked with right mouse button.
-
-
-
Field Detail
-
NONE
public static final int NONE
- See Also:
- Constant Field Values
-
TOP
public static final int TOP
- See Also:
- Constant Field Values
-
RIGHT
public static final int RIGHT
- See Also:
- Constant Field Values
-
BOTTOM
public static final int BOTTOM
- See Also:
- Constant Field Values
-
LEFT
public static final int LEFT
- See Also:
- Constant Field Values
-
FULL
public static final int FULL
- See Also:
- Constant Field Values
-
MIRRORED
public static final int MIRRORED
- See Also:
- Constant Field Values
-
actDelay
public static int actDelay
-
clicked
public int clicked
-
-
Constructor Detail
-
StageObject
public StageObject(java.lang.String id, int x, int y, java.lang.String img, int scale)
Constructors for stage object.- Parameters:
id
- unique identifier for this object (e.g. "Cat")x
- initial position of objecty
- initial position of objectimg
- image name in resource path (either a PNG, GIF or JPG file)scale
- scaling factor in percent (100 = original size)
-
StageObject
public StageObject(Stage stage, int x, int y)
Constructors for stage object.- Parameters:
stage
- stage for objectx
- initial position of objecty
- initial position of object
-
StageObject
public StageObject(Stage stage, int x, int y, java.lang.String img)
Constructors for stage object.- Parameters:
stage
- stage for objectx
- initial position of objecty
- initial position of objectimg
- image name in resource path (either a PNG, GIF or JPG file)
-
StageObject
public StageObject(Stage stage, int x, int y, java.lang.String img, int scale)
Constructors for stage object.- Parameters:
stage
- stage for objectx
- initial position of objecty
- initial position of objectimg
- image name in resource path (either a PNG, GIF or JPG file)scale
- scaling factor in percent (100 = original size)
-
StageObject
public StageObject(Stage stage, java.lang.String id, int x, int y, java.lang.String img, int scale)
Constructors for stage object.- Parameters:
stage
- stage for objectid
- unique identifier for this object (e.g. "Cat")x
- initial position of objecty
- initial position of objectimg
- image name in resource path (either a PNG, GIF or JPG file)scale
- scaling factor in percent (100 = original size)
-
StageObject
public StageObject(Stage stage, java.lang.String id, int x, int y, java.lang.String img)
Constructors for stage object.- Parameters:
stage
- stage for objectid
- unique identifier for this object (e.g. "Cat")x
- initial position of objecty
- initial position of objectimg
- image name in resource path (either a PNG, GIF or JPG file)
-
-
Method Detail
-
setStage
public void setStage(Stage stage)
Bind a stage object to a stage. The stage object can access stage's functionality by this reference (e.g., status of input devices).- Parameters:
stage
- Stage reference
-
setId
public void setId(java.lang.String id)
Change id used to look up object.- Parameters:
id
-
-
getId
public java.lang.String getId()
Get unique identifier of this stage object.- Returns:
- object's identifier
-
getX
public double getX()
Return x-value of objects position (center).- Returns:
- x-value of object's position
-
getY
public double getY()
Return y-value of objects position (center).- Returns:
- y-value of object's position
-
getAngle
public double getAngle()
Get rotation angle of object from 0 to 360 degrees. 0 points east, going counter clock wise.- Returns:
- rotation angle of object
-
setX
public void setX(double nx)
Set x-value of position and check whether out of stage boundary.- Parameters:
nx
- new x-value for position
-
setY
public void setY(double ny)
Set y-value of position and check whether out of stage boundary.- Parameters:
ny
- new y-value for position
-
changeX
public void changeX(double mod)
Modify x-value of position and check whether out of stage boundary.- Parameters:
mod
- value added to x-value of position
-
changeY
public void changeY(double mod)
Modify y-value of position and check whether out of stage boundary.- Parameters:
mod
- value added to y-value of position
-
moveTo
public void moveTo(double px, double py)
Move object directly to a position on stage. Check whether out of stage boundary.- Parameters:
px
- x-value of positionpy
- y-value of position
-
moveTo
public void moveTo(StageObject obj)
Move object directly to position of another object. Check whether out of stage boundary.- Parameters:
obj
- target object
-
move
public void move(double steps)
Move object forward in direction of current rotation angle. One step means one pixel of stage.- Parameters:
steps
- number of steps to be taken at once
-
setRotation
public void setRotation(double angle)
Directly set rotation of object.- Parameters:
angle
- rotation angle in degrees (0 = east, clockwise increment)
-
rotate
public void rotate(double mod)
Rotate object (in degrees). A positive values for mod means clockwise rotation, a negative value means counterclockwise rotation.- Parameters:
mod
- change of current angle in degrees
-
rotateLeft
public void rotateLeft(double mod)
Rotate object by some degrees to the left.- Parameters:
mod
- change of current angle in degrees
-
rotateRight
public void rotateRight(double mod)
Rotate object by some degrees to the right.- Parameters:
mod
- change of current angle in degrees
-
bounceOffBoundary
public void bounceOffBoundary()
Let object bounce off the boundary (thus update the angle) depending on StageObject.hitBoundary (which may be either BOTTOM, TOP, LEFT or RIGHT) and reset StageObject.hitBoundary to NONE.
-
lookAt
public void lookAt(double px, double py)
Rotate object (update angle) to point to given position.- Parameters:
px
- x-value of positionpy
- y-value of position
-
lookAt
public void lookAt(StageObject obj)
Rotate object (update angle) to point to given object.- Parameters:
obj
- object to point at
-
lookAtPointer
public void lookAtPointer()
Rotate object (update angle) to point to mouse cursor position.
-
getWidth
public double getWidth()
Get current width of object.- Returns:
- actual width (with current scaling) of object
-
getHeight
public double getHeight()
Get current height of object.- Returns:
- actual height (with current scaling) of object
-
show
public void show()
Show object on stage (that means draw it).
-
hide
public void hide()
Hide object from stage. This refers only to visibility. The object can still be used, it is just not drawn.
-
isHidden
public boolean isHidden()
Return whether object is currently hidden.- Returns:
- true if hidden or false if shown
-
addImage
public void addImage(java.lang.String id, java.lang.String img)
Add a new image with a unique identifier. An object may have multiple images. Note: The default sprite (added when object was created) has the object's name as identifier.- Parameters:
id
- unique identifierimg
- image name in resource path (either a PNG, GIF or JPG file)
-
addImage
public void addImage(java.lang.String img)
Add an image without unique identifier (use image name as id) for use of setImage with index only.
-
setImage
public void setImage(java.lang.String id)
Select image (thus change appearance). If there is no image with the given id as identifier the current image is kept without raising an exception.- Parameters:
id
- identifier of wanted sprite
-
setImage
public void setImage(int pos)
Choose image by index in list (thus change appearance). First (default) image has index 0. The order of images is as the order of addImage() calls. If the position is out of range the default image is set without raising an exception.- Parameters:
pos
- index in sprite list (starting at 0 for default sprite)
-
nextImage
public void nextImage()
Select next sprite in list (thus change appearance). Starts again with first sprite after last one (thus cycles endlessly through list).
-
setRotationStyle
public void setRotationStyle(int style)
By default object is rotated according to current angle (style FULL). This behavior can be changed with this method. NONE means the image is not rotated at all, MIRRORED means the image gets horizontally mirrored for angles between 90 to 270 degrees.- Parameters:
style
- rotation style NONE, MIRRORED or FULL
-
getRotationStyle
public int getRotationStyle()
Get current rotation style.- Returns:
- either StageObject.NONE, StageObject.MIRRORED or StageObject.FULL
-
scale
public void scale(double perc)
Set scale factor for object in percent (100 = original size). Maximum scale factor is 1000 (10 times larger than original image).- Parameters:
perc
- scale factor in percent
-
scaleUp
public void scaleUp(double mod)
Modify current scale factor by adding this value.- Parameters:
mod
- scale factor modification
-
scaleDown
public void scaleDown(double mod)
Modify current scale factor by subtracting this value.- Parameters:
mod
- scale factor modification
-
getScaleFactor
public double getScaleFactor()
Get current scale factor (100 = 100%)- Returns:
- current scale factor
-
hasHitBoundary
public boolean hasHitBoundary()
Return true if last update of position caused object to hit the stage boundary- Returns:
- whether boundary was hit
-
getHitBoundary
public int getHitBoundary()
Return which part of boundary has been hit.- Returns:
- either StageObject.BOTTOM, StageObject.TOP, StageObject.LEFT, StageObject.RIGHT
-
getDistance
public double getDistance(double px, double py)
Get distance from object to position (px,py).- Parameters:
px
- x-value of target positionpy
- y-value of target position- Returns:
- distance of object to given position
-
getDistance
public double getDistance(StageObject obj)
Get distance from this object to object obj.- Parameters:
obj
- target object- Returns:
- distance of object to given object
-
checkCollision
public boolean checkCollision(StageObject obj)
Check whether two objects collide. This is done by representing both objects as circles of half the object's width and checking the distance. Thus for now it only works well for almost square objects.- Parameters:
obj
- target object- Returns:
- true if objects overlap
-
touches
public boolean touches(StageObject obj)
Same as checkCollision().- Parameters:
obj
-- Returns:
- true if objects overlap
-
draw
public void draw(java.awt.Graphics g)
Draw image if not hidden.- Parameters:
g
- drawing context
-
wait
public void wait(int msecs)
Wait some time (given in milliseconds).- Parameters:
msecs
- time to wait in milliseconds
-
run
public void run()
Called on stage.start() when threads are started and calls act() every program cycle. Loops until stage is stopped by stage.stop().- Specified by:
run
in interfacejava.lang.Runnable
-
act
public void act()
Action method called by run every program cycle. By default StageObject takes no actions. Can be overwritten by derived objects to implement actual behavior.
-
setText
public void setText(java.lang.String t, int s)
Set text and font size. Displayed left-align and center position x,y.- Parameters:
t
- Texts
- size
-
wasClicked
public boolean wasClicked()
Check whether object was clicked (mouse pointer on it). This is checked and set by stage. Clicked is reset to 0.- Returns:
- True if clicked by any button
-
wasLeftClicked
public boolean wasLeftClicked()
Check whether object was clicked with left mouse button. This is checked and set by stage. A right click is not reset yet.- Returns:
- True if left clicked
-
wasRightClicked
public boolean wasRightClicked()
Check whether object was clicked with right mouse button. This is checked and set by stage. A left click is not reset yet.- Returns:
- True if right clicked
-
-