Package sglib
Class Stage
- java.lang.Object
-
- java.awt.Component
-
- java.awt.Container
-
- java.awt.Window
-
- java.awt.Frame
-
- javax.swing.JFrame
-
- sglib.Stage
-
- All Implemented Interfaces:
java.awt.event.KeyListener
,java.awt.event.MouseListener
,java.awt.event.MouseMotionListener
,java.awt.image.ImageObserver
,java.awt.MenuContainer
,java.io.Serializable
,java.util.EventListener
,javax.accessibility.Accessible
,javax.swing.RootPaneContainer
,javax.swing.WindowConstants
public class Stage extends javax.swing.JFrame implements java.awt.event.KeyListener, java.awt.event.MouseListener, java.awt.event.MouseMotionListener
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static int
BUTTON_LEFT
static int
BUTTON_NONE
static int
BUTTON_RIGHT
static int
repaintDelay
-
Fields inherited from class java.awt.Frame
CROSSHAIR_CURSOR, DEFAULT_CURSOR, E_RESIZE_CURSOR, HAND_CURSOR, ICONIFIED, MAXIMIZED_BOTH, MAXIMIZED_HORIZ, MAXIMIZED_VERT, MOVE_CURSOR, N_RESIZE_CURSOR, NE_RESIZE_CURSOR, NORMAL, NW_RESIZE_CURSOR, S_RESIZE_CURSOR, SE_RESIZE_CURSOR, SW_RESIZE_CURSOR, TEXT_CURSOR, W_RESIZE_CURSOR, WAIT_CURSOR
-
Fields inherited from class java.awt.Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addObject(StageObject obj)
Add an object to stage's list of stage objects.void
addVariable(java.lang.String name, double d)
Add a new mixed variable and set initial double value.void
addVariable(java.lang.String name, int i)
Add a new mixed variable and set initial integer value.void
addVariable(java.lang.String name, java.lang.String s)
Add a new mixed variable and set initial string value.void
changeVariable(java.lang.String name, double d)
Add double value to current value of mixed variable.void
changeVariable(java.lang.String name, int i)
Add integer value to current value of mixed variable.void
changeVariable(java.lang.String name, java.lang.String s)
Concatenate string to current value (as string) of mixed variable.int
getHeight()
Get height of stage (excluding boundary).int
getMouseX()
Get x-value of pointer (border is excluded)int
getMouseY()
Get y-value of pointer (border is excluded)StageObject
getObject(java.lang.String name)
Get an object by its name.double
getVariableAsDouble(java.lang.String name)
Get mixed variable value as double.int
getVariableAsInt(java.lang.String name)
Get mixed variable value as integer.java.lang.String
getVariableAsString(java.lang.String name)
Get mixed variable value as string.int
getWidth()
Get width of stage (excluding boundary).boolean
isButtonPressed(int button)
Return state of mouse button (1=left, 2=right)boolean
isButtonReleased(int button)
Checks whether mouse button (1=left, 2=right) was (recently) released.boolean
isKeyPressed(java.lang.String key)
Check if key is pressed.boolean
isKeyReleased(java.lang.String key)
Check if key was (recently) released.boolean
isPointerOnObject(StageObject obj)
Checks whether mouse pointer is on object.boolean
isRunning()
Return stage's current run status.void
keyPressed(java.awt.event.KeyEvent e)
Set key state true if key was pressed.void
keyReleased(java.awt.event.KeyEvent e)
Set key state false if key was released.void
keyTyped(java.awt.event.KeyEvent e)
Dummy function to satisfy interface.void
loadBackground(java.lang.String fname)
Set background of stage.void
mouseClicked(java.awt.event.MouseEvent e)
Dummy function to satisfy interface.void
mouseDragged(java.awt.event.MouseEvent e)
Update internal mouse position when mouse is moved. (0, 0) is left upper corner of panel (not frame, thus excluding the border).void
mouseEntered(java.awt.event.MouseEvent e)
Dummy function to satisfy interface.void
mouseExited(java.awt.event.MouseEvent e)
Dummy function to satisfy interface.void
mouseMoved(java.awt.event.MouseEvent e)
Update internal mouse position when mouse is moved. (0, 0) is left upper corner of panel (not frame, thus excluding the border).void
mousePressed(java.awt.event.MouseEvent e)
Set mouse button state for left (button1) or right (button2) mouse button.void
mouseReleased(java.awt.event.MouseEvent e)
Clear mouse button state for left (button1) and right (button2) mouse button.java.lang.String
read(java.lang.String msg)
Show dialog for input.void
removeObject(java.lang.String id)
Completely remove an object from the stage.void
setVariable(java.lang.String name, double d)
Set mixed variable to a double value.void
setVariable(java.lang.String name, int i)
Set mixed variable to an integer value.void
setVariable(java.lang.String name, java.lang.String s)
Set mixed variable to a string value.void
start()
Start a thread for each stage object to run its run() method.void
start(int ms)
Start a thread for each stage object to run its run() method and set delay between calls to act to ms milliseconds.void
stop()
Set stopped true which will result in StageObject's default run method to cancel.java.lang.String
waitForKey()
Block until a key is pressed.-
Methods inherited from class javax.swing.JFrame
getAccessibleContext, getContentPane, getDefaultCloseOperation, getGlassPane, getGraphics, getJMenuBar, getLayeredPane, getRootPane, getTransferHandler, isDefaultLookAndFeelDecorated, remove, repaint, setContentPane, setDefaultCloseOperation, setDefaultLookAndFeelDecorated, setGlassPane, setIconImage, setJMenuBar, setLayeredPane, setLayout, setTransferHandler, update
-
Methods inherited from class java.awt.Frame
addNotify, getCursorType, getExtendedState, getFrames, getIconImage, getMaximizedBounds, getMenuBar, getState, getTitle, isResizable, isUndecorated, remove, removeNotify, setBackground, setCursor, setExtendedState, setMaximizedBounds, setMenuBar, setOpacity, setResizable, setShape, setState, setTitle, setUndecorated
-
Methods inherited from class java.awt.Window
addPropertyChangeListener, addPropertyChangeListener, addWindowFocusListener, addWindowListener, addWindowStateListener, applyResourceBundle, applyResourceBundle, createBufferStrategy, createBufferStrategy, dispose, getBackground, getBufferStrategy, getFocusableWindowState, getFocusCycleRootAncestor, getFocusOwner, getFocusTraversalKeys, getIconImages, getInputContext, getListeners, getLocale, getModalExclusionType, getMostRecentFocusOwner, getOpacity, getOwnedWindows, getOwner, getOwnerlessWindows, getShape, getToolkit, getType, getWarningString, getWindowFocusListeners, getWindowListeners, getWindows, getWindowStateListeners, hide, isActive, isAlwaysOnTop, isAlwaysOnTopSupported, isAutoRequestFocus, isFocusableWindow, isFocusCycleRoot, isFocused, isLocationByPlatform, isOpaque, isShowing, isValidateRoot, pack, paint, postEvent, removeWindowFocusListener, removeWindowListener, removeWindowStateListener, reshape, setAlwaysOnTop, setAutoRequestFocus, setBounds, setBounds, setCursor, setFocusableWindowState, setFocusCycleRoot, setIconImages, setLocation, setLocation, setLocationByPlatform, setLocationRelativeTo, setMinimumSize, setModalExclusionType, setSize, setSize, setType, setVisible, show, toBack, toFront
-
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getAlignmentX, getAlignmentY, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalPolicy, getInsets, getLayout, getMaximumSize, getMinimumSize, getMousePosition, getPreferredSize, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, print, printComponents, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusTraversalKeys, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setFont, transferFocusDownCycle, validate
-
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, contains, contains, createImage, createImage, createVolatileImage, createVolatileImage, disable, dispatchEvent, enable, enable, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBaseline, getBaselineResizeBehavior, getBounds, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocation, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getSize, getTreeLock, getX, getY, gotFocus, handleEvent, hasFocus, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isDoubleBuffered, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, prepareImage, prepareImage, printAll, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resize, resize, revalidate, setComponentOrientation, setDropTarget, setEnabled, setFocusable, setFocusTraversalKeysEnabled, setForeground, setIgnoreRepaint, setLocale, setMaximumSize, setMixingCutoutShape, setName, setPreferredSize, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle
-
-
-
-
Field Detail
-
BUTTON_NONE
public static final int BUTTON_NONE
- See Also:
- Constant Field Values
-
BUTTON_LEFT
public static final int BUTTON_LEFT
- See Also:
- Constant Field Values
-
BUTTON_RIGHT
public static final int BUTTON_RIGHT
- See Also:
- Constant Field Values
-
repaintDelay
public static int repaintDelay
-
-
Constructor Detail
-
Stage
public Stage(int width, int height)
Constructor for stage.- Parameters:
width
- of stage (excluding boundary)height
- of stage (excluding boundary)
-
Stage
public Stage(java.lang.String title, int width, int height)
Constructor for stage.- Parameters:
title
- displayed in windowwidth
- of stage (excluding boundary)height
- of stage (excluding boundary)
-
Stage
public Stage(java.lang.String title, int width, int height, java.lang.String bkgnd)
Constructor for stage.- Parameters:
title
- displayed in windowwidth
- of stage (excluding boundary)height
- of stage (excluding boundary)bkgnd
- of stage
-
-
Method Detail
-
loadBackground
public void loadBackground(java.lang.String fname)
Set background of stage.- Parameters:
fname
- image name in resource path (PNG, GIF, JPG)
-
getWidth
public int getWidth()
Get width of stage (excluding boundary).- Overrides:
getWidth
in classjava.awt.Component
- Returns:
- stage's width
-
getHeight
public int getHeight()
Get height of stage (excluding boundary).- Overrides:
getHeight
in classjava.awt.Component
- Returns:
- stage's height
-
start
public void start(int ms)
Start a thread for each stage object to run its run() method and set delay between calls to act to ms milliseconds. If threads were already started only act delay is changed.- Parameters:
ms
- act() delay in milliseconds
-
start
public void start()
Start a thread for each stage object to run its run() method. Has no effect if threads were already started.
-
stop
public void stop()
Set stopped true which will result in StageObject's default run method to cancel. Makes no sense if start has not been called and no threads are running.
-
isRunning
public boolean isRunning()
Return stage's current run status.- Returns:
- True if threads not stopped yet (or no threads).
-
addObject
public void addObject(StageObject obj)
Add an object to stage's list of stage objects. If Stage.start() has already been called a new thread is started to execute object's run method.- Parameters:
obj
- stage object to be added
-
getObject
public StageObject getObject(java.lang.String name)
Get an object by its name. If object is not found return null without raising an exception.- Parameters:
name
- name of object- Returns:
- reference to object
-
removeObject
public void removeObject(java.lang.String id)
Completely remove an object from the stage. If object is not found no action is performed and no exception is raised.- Parameters:
id
- name of object
-
keyTyped
public void keyTyped(java.awt.event.KeyEvent e)
Dummy function to satisfy interface.- Specified by:
keyTyped
in interfacejava.awt.event.KeyListener
- Parameters:
e
- key event
-
keyPressed
public void keyPressed(java.awt.event.KeyEvent e)
Set key state true if key was pressed.- Specified by:
keyPressed
in interfacejava.awt.event.KeyListener
- Parameters:
e
- key event
-
keyReleased
public void keyReleased(java.awt.event.KeyEvent e)
Set key state false if key was released.- Specified by:
keyReleased
in interfacejava.awt.event.KeyListener
- Parameters:
e
- key event
-
isKeyPressed
public boolean isKeyPressed(java.lang.String key)
Check if key is pressed. Key id is given as string: SPACE, ENTER, LEFT, RIGHT, UP, DOWN or A - Z (lower/upper case) or 0-9. If key id is not found false is returned without raising an exception.- Parameters:
key
- id as string- Returns:
- whether key is pressed
-
isKeyReleased
public boolean isKeyReleased(java.lang.String key)
Check if key was (recently) released. Key id is given as string: SPACE, ENTER, LEFT, RIGHT, UP, DOWN or A - Z (lower/upper case) or 0-9. If key id is not found false is returned without raising an exception. Only for single-threaded use!- Parameters:
key
- id as string- Returns:
- whether key is pressed
-
waitForKey
public java.lang.String waitForKey()
Block until a key is pressed. Return key as String if any of the standard keys are pressed: SPACE, ENTER, LEFT, RIGHT, UP, DOWN, or A - Z (lower/upper case) or 0-9.- Returns:
- Key id as String
-
mouseClicked
public void mouseClicked(java.awt.event.MouseEvent e)
Dummy function to satisfy interface.- Specified by:
mouseClicked
in interfacejava.awt.event.MouseListener
- Parameters:
e
- mouse event
-
mouseEntered
public void mouseEntered(java.awt.event.MouseEvent e)
Dummy function to satisfy interface.- Specified by:
mouseEntered
in interfacejava.awt.event.MouseListener
- Parameters:
e
- mouse event
-
mouseExited
public void mouseExited(java.awt.event.MouseEvent e)
Dummy function to satisfy interface.- Specified by:
mouseExited
in interfacejava.awt.event.MouseListener
- Parameters:
e
- mouse event
-
mousePressed
public void mousePressed(java.awt.event.MouseEvent e)
Set mouse button state for left (button1) or right (button2) mouse button.- Specified by:
mousePressed
in interfacejava.awt.event.MouseListener
- Parameters:
e
- mouse event
-
mouseReleased
public void mouseReleased(java.awt.event.MouseEvent e)
Clear mouse button state for left (button1) and right (button2) mouse button. Also set StageObject.clicked if pointer is on object. Is reset by checking with StageObject.wasClicked().- Specified by:
mouseReleased
in interfacejava.awt.event.MouseListener
- Parameters:
e
- mouse event
-
isButtonPressed
public boolean isButtonPressed(int button)
Return state of mouse button (1=left, 2=right)- Returns:
- whether mouse button is currently pressed
-
isButtonReleased
public boolean isButtonReleased(int button)
Checks whether mouse button (1=left, 2=right) was (recently) released. Only for single-threaded use!- Returns:
- True if button was pressed and released
-
mouseMoved
public void mouseMoved(java.awt.event.MouseEvent e)
Update internal mouse position when mouse is moved. (0, 0) is left upper corner of panel (not frame, thus excluding the border).- Specified by:
mouseMoved
in interfacejava.awt.event.MouseMotionListener
- Parameters:
e
- mouse event
-
mouseDragged
public void mouseDragged(java.awt.event.MouseEvent e)
Update internal mouse position when mouse is moved. (0, 0) is left upper corner of panel (not frame, thus excluding the border).- Specified by:
mouseDragged
in interfacejava.awt.event.MouseMotionListener
- Parameters:
e
- mouse event
-
getMouseX
public int getMouseX()
Get x-value of pointer (border is excluded)- Returns:
- x-value of mouse position
-
getMouseY
public int getMouseY()
Get y-value of pointer (border is excluded)- Returns:
- y-value of mouse position
-
isPointerOnObject
public boolean isPointerOnObject(StageObject obj)
Checks whether mouse pointer is on object. As a simplification the object is assumed to be circular with width as radius (thus it won't work properly for stretched out objects).- Parameters:
obj
- target object- Returns:
- whether mouse is on object
-
addVariable
public void addVariable(java.lang.String name, int i)
Add a new mixed variable and set initial integer value. A mixed variable can be set and get as string, integer or double.- Parameters:
name
- unique identifieri
- initial integer value
-
addVariable
public void addVariable(java.lang.String name, double d)
Add a new mixed variable and set initial double value. A mixed variable can be set and get as string, integer or double.- Parameters:
name
- unique identifierd
- initial double value
-
addVariable
public void addVariable(java.lang.String name, java.lang.String s)
Add a new mixed variable and set initial string value. A mixed variable can be set and get as string, integer or double.- Parameters:
name
- unique identifiers
- initial String value
-
setVariable
public void setVariable(java.lang.String name, int i)
Set mixed variable to an integer value.- Parameters:
name
- unique identifieri
- new integer value
-
setVariable
public void setVariable(java.lang.String name, double d)
Set mixed variable to a double value.- Parameters:
name
- unique identifierd
- new double value
-
setVariable
public void setVariable(java.lang.String name, java.lang.String s)
Set mixed variable to a string value.- Parameters:
name
- unique identifiers
- new string value
-
changeVariable
public void changeVariable(java.lang.String name, int i)
Add integer value to current value of mixed variable. If variable is not found, no action is performed and no exception is raised.- Parameters:
name
- unique identifieri
- value to be added
-
changeVariable
public void changeVariable(java.lang.String name, double d)
Add double value to current value of mixed variable. If variable is not found, no action is performed and no exception is raised.- Parameters:
name
- unique identifierd
- value to be added
-
changeVariable
public void changeVariable(java.lang.String name, java.lang.String s)
Concatenate string to current value (as string) of mixed variable. If variable is not found, no action is performed and no exception is raised.- Parameters:
name
- unique identifiers
- string to be concatenated
-
getVariableAsInt
public int getVariableAsInt(java.lang.String name)
Get mixed variable value as integer. If mixed variable is not found 0 is returned and no exception is raised.- Parameters:
name
- unique identifier- Returns:
- mixed variable value as integer
-
getVariableAsDouble
public double getVariableAsDouble(java.lang.String name)
Get mixed variable value as double. If mixed variable is not found 0 is returned and no exception is raised.- Parameters:
name
- unique identifier- Returns:
- mixed variable value as double
-
getVariableAsString
public java.lang.String getVariableAsString(java.lang.String name)
Get mixed variable value as string. If mixed variable is not found empty string "" is returned and no exception is raised.- Parameters:
name
- unique identifier- Returns:
- mixed variable value as string
-
read
public java.lang.String read(java.lang.String msg)
Show dialog for input.- Parameters:
msg
- Message displayed for user.- Returns:
- Input as String.
-
-