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 intBUTTON_LEFTstatic intBUTTON_NONEstatic intBUTTON_RIGHTstatic intrepaintDelay-
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 voidaddObject(StageObject obj)Add an object to stage's list of stage objects.voidaddVariable(java.lang.String name, double d)Add a new mixed variable and set initial double value.voidaddVariable(java.lang.String name, int i)Add a new mixed variable and set initial integer value.voidaddVariable(java.lang.String name, java.lang.String s)Add a new mixed variable and set initial string value.voidchangeVariable(java.lang.String name, double d)Add double value to current value of mixed variable.voidchangeVariable(java.lang.String name, int i)Add integer value to current value of mixed variable.voidchangeVariable(java.lang.String name, java.lang.String s)Concatenate string to current value (as string) of mixed variable.intgetHeight()Get height of stage (excluding boundary).intgetMouseX()Get x-value of pointer (border is excluded)intgetMouseY()Get y-value of pointer (border is excluded)StageObjectgetObject(java.lang.String name)Get an object by its name.doublegetVariableAsDouble(java.lang.String name)Get mixed variable value as double.intgetVariableAsInt(java.lang.String name)Get mixed variable value as integer.java.lang.StringgetVariableAsString(java.lang.String name)Get mixed variable value as string.intgetWidth()Get width of stage (excluding boundary).booleanisButtonPressed(int button)Return state of mouse button (1=left, 2=right)booleanisButtonReleased(int button)Checks whether mouse button (1=left, 2=right) was (recently) released.booleanisKeyPressed(java.lang.String key)Check if key is pressed.booleanisKeyReleased(java.lang.String key)Check if key was (recently) released.booleanisPointerOnObject(StageObject obj)Checks whether mouse pointer is on object.booleanisRunning()Return stage's current run status.voidkeyPressed(java.awt.event.KeyEvent e)Set key state true if key was pressed.voidkeyReleased(java.awt.event.KeyEvent e)Set key state false if key was released.voidkeyTyped(java.awt.event.KeyEvent e)Dummy function to satisfy interface.voidloadBackground(java.lang.String fname)Set background of stage.voidmouseClicked(java.awt.event.MouseEvent e)Dummy function to satisfy interface.voidmouseDragged(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).voidmouseEntered(java.awt.event.MouseEvent e)Dummy function to satisfy interface.voidmouseExited(java.awt.event.MouseEvent e)Dummy function to satisfy interface.voidmouseMoved(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).voidmousePressed(java.awt.event.MouseEvent e)Set mouse button state for left (button1) or right (button2) mouse button.voidmouseReleased(java.awt.event.MouseEvent e)Clear mouse button state for left (button1) and right (button2) mouse button.java.lang.Stringread(java.lang.String msg)Show dialog for input.voidremoveObject(java.lang.String id)Completely remove an object from the stage.voidsetVariable(java.lang.String name, double d)Set mixed variable to a double value.voidsetVariable(java.lang.String name, int i)Set mixed variable to an integer value.voidsetVariable(java.lang.String name, java.lang.String s)Set mixed variable to a string value.voidstart()Start a thread for each stage object to run its run() method.voidstart(int ms)Start a thread for each stage object to run its run() method and set delay between calls to act to ms milliseconds.voidstop()Set stopped true which will result in StageObject's default run method to cancel.java.lang.StringwaitForKey()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:
getWidthin classjava.awt.Component- Returns:
- stage's width
-
getHeight
public int getHeight()
Get height of stage (excluding boundary).- Overrides:
getHeightin 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:
keyTypedin 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:
keyPressedin 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:
keyReleasedin 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:
mouseClickedin interfacejava.awt.event.MouseListener- Parameters:
e- mouse event
-
mouseEntered
public void mouseEntered(java.awt.event.MouseEvent e)
Dummy function to satisfy interface.- Specified by:
mouseEnteredin interfacejava.awt.event.MouseListener- Parameters:
e- mouse event
-
mouseExited
public void mouseExited(java.awt.event.MouseEvent e)
Dummy function to satisfy interface.- Specified by:
mouseExitedin 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:
mousePressedin 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:
mouseReleasedin 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:
mouseMovedin 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:
mouseDraggedin 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.
-
-