Class Stage
java.lang.Object
org.openpatch.scratch.Stage
The Stage class represents a stage where various elements such as sprites,
texts, pens, and
backdrops can be added and manipulated. It provides methods to manage these
elements, handle
events, and control the stage's appearance and behavior.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfacestatic interfacestatic interfacestatic interfacestatic interfacestatic interfacestatic interface -
Constructor Summary
ConstructorsConstructorDescriptionStage()Constructs a new Stage with default dimensions.Stage(boolean fullScreen) Constructs a new Stage.Stage(boolean fullScreen, int width, int height) Constructs a new Stage with the specified width and height.Constructs a new Stage with the specified parameters.Constructs a new Stage with the specified fullscreen mode and assets path.Stage(int width, int height) Constructs a new Stage with the specified width and height.Constructs a new Stage with the specified width, height, and assets path. -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds a Pen object to the stage.voidAdd a text object to the stagevoidAdd a sprite object to the stagevoidaddBackdrop(String name, String imagePath) Add a backdrop to the stage.voidaddBackdrop(String name, String imagePath, boolean stretch) Add a backdrop to the stage.Adds a new shader to the sprite.voidAdd a sound to the stage.voidaddStampsToBackground(Queue<Stamp> stamps) voidaddStampsToBackground(Stamp stamp) voidaddStampsToForeground(Queue<Stamp> stamps) voidaddStampsToForeground(Stamp stamp) voidAdd a new timer by name.voidBroadcasts a message to all sprites in the stage.voidBroadcasts a message to all sprites in the stage.voidchangeColor(double h) Changes the background color by adding a step to the hue value.voidchangeTint(double step) Changes the tint for the current backdropvoidchangeTransparency(double step) Changes the transparency for the current costume.<T extends Sprite>
longReturns the number of sprites of the specified class.longReturns the number of pens<T extends Pen>
longcountPensOf(Class<T> c) Returns the number of pens of the specified class.longReturns the number of sprites in the stage.<T extends Sprite>
longcountSpritesOf(Class<T> c) Returns the number of sprites of the specified class.longReturns the number of texts in the stage.<T extends Text>
longcountTextsOf(Class<T> c) Returns the number of texts of the specified class.voidDisables the sorting of sprites.voidDisplays the given text on the stage.voidDisplays the given text on the screen for a specified duration.voiddraw(processing.core.PGraphics buffer) voidEnables the sorting of sprites using y-sorting.voideraseAll()Erases all lines on the pen layer.voidWhen this method is called, the background buffer will be erased.voidWhen this method is called, the foreground buffer will be erased.voideraseUI()This method marks the UI buffer to be erased, which will be processed in the next update cycle.voidexit()Close the window and therefore the whole application.Find sprites of a given class.findPensOf(Class<T> c) Find texts of a given class.findSpritesOf(Class<T> c) Find sprites of a given class.findTextsOf(Class<T> c) Find texts of a given class.getAll()Retrieves a list of all sprites in the current stage.int[]Returns the pixels of the background buffer.Retrieves the current camera instance associated with this stage.getColor()Returns the current color of the stage.intReturns the current backdrop indexReturns the current backdrop nameintReturns the current day of the monthintReturns the current day of the weekintReturns the current hourintReturns the current millisecondintReturns the current minuteintReturns the current monthintReturns the current secondRetrieves the current shader.intRetrieves the index of the current shader.Retrieves the name of the current shader.intReturns the current yearintReturns the days since 2010/01/01doubleGets the seconds passed since the last frame.int[]Returns the pixels of the foreground buffer.doubleGet the frame rate of the application.intReturn the height of the current costume or the pen size, when no costume is available.getMouse()Returns the current position of the mouse cursor as a Vector2doubleReturns the current x-position of the mouse cursordoubleReturns the current y-position of the mouse cursorint[]Returns the pixels of the main buffer.Retrieves a shader by name.getTimer()Returns the timerReturns a timer by nameintgetWidth()Return the width of the current costume or the pen size, when no costume is available.Get the Window object.voidgoLayersBackwards(Sprite sprite, int number) Moves the specified sprite backwards by a given number of layers in the sprite list.voidgoLayersForwards(Sprite sprite, int number) Moves the specified sprite forward by a given number of layers in the sprite list.voidgoToBackLayer(Sprite sprite) Moves the specified sprite to the back layer of the stage.voidgoToFrontLayer(Sprite sprite) Moves the specified sprite to the front layer.voidgoToUILayer(Sprite sprite) Moves the specified sprite to the UI layer by removing it from the current list of sprites.booleanisDebug()Checks if the application is in debug mode.booleanisKeyPressed(int keyCode) Returns true if the key is pressedbooleanReturns true is the mouse button is downbooleanChecks if the sorting of sprites is enabled.booleanisSoundPlaying(String name) Returns true if the sound if playingvoidkeyEvent(processing.event.KeyEvent e) voidmouseEvent(processing.event.MouseEvent e) voidSwitch to the next backdrop.voidSets the next shader as the current shader.intpickRandom(int from, int to) Returns a random integer between the specified range (inclusive).voidPlays a sound.voidpre()voidSwitch to the previous backdrop.voidSwitch to a random backdrop.voidRemoves all sprites of the specified class from the stage.voidRemoves the specified pen from the stage.voidRemoves the specified text from the stage.voidRemoves the specified sprite from the stage.voidRemoves all elements from the stage.voidRemoves all pens from the stage.voidRemoves all sprites from the stage.voidRemoves all texts from the stage.voidremoveBackdrop(String name) Remove a backdrop from the stage.voidremoveSound(String name) Remove a sound from the stage.voidremoveTimer(String name) Remove a timer by name.voidvoidrun()Executes the main logic of the stage.voidsetColor(double h) Sets the background color via a hue valuevoidsetColor(double r, double g, double b) Sets the background color via a rgb valuevoidSets the color of the stage.voidSets the cursor image for the stage.voidSets the cursor image and its active spot coordinates.voidsetDebug(boolean debug) Enables or disables the debug mode for the application.voidsetRun(Stage.RunHandler run) Sets the main logic of the stage to be executed during each frame.voidsetSorter(Comparator<? super Sprite> sorter) Sets a custom sorter for the sprites.voidsetTextureSampling(int mode) Sets the texture sampling mode Point sampling: both magnification and minification filtering are set to nearest.voidsetTint(double h) Sets the tint for the current backdrop with a hue.voidsetTint(double r, double g, double b) Sets the tint for the current backdrop with rgb.voidsetTransparency(double transparency) Sets the transparency of the current backdrop.voidsetWhenBackdropSwitches(Stage.WhenBackdropSwitchesHandler whenBackdropSwitches) Sets the handler for when the backdrop switches.voidsetWhenIReceive(Stage.WhenIReceiveHandler whenIReceive) Sets the handler for when a message is received.voidsetWhenKeyPressed(Stage.WhenKeyPressedHandler whenKeyPressed) Sets the handler for key press events.voidsetWhenKeyReleased(Stage.WhenKeyReleasedHandler whenKeyReleased) Sets the handler for key release events.voidsetWhenMouseClicked(Stage.WhenMouseClickedHandler whenMouseClicked) Sets the handler for mouse click events.voidsetWhenMouseWheelMoved(Stage.WhenMouseWheelMovedHandler whenMouseWheelMoved) Sets the handler for mouse wheel movement events.voidStops the playing of all sounds of the stage.voidStops the playing of the sound with the given namevoidswitchBackdrop(String name) Switch to a backdrop by name.voidswitchShader(double index) Switch to a shader by index.voidswitchShader(String name) Switch to a shader by name.voidwait(int millis) Stop the execution of the whole applications for the given milliseconds.voidwhenBackdropSwitches(String name) This method is called when the backdrop switches to the specified name.voidwhenIReceive(Object message) This method is called when a message is received.voidwhenIReceive(String message) This method is called when a specific message is received.voidwhenKeyPressed(int keyCode) This method is called when a key is pressed.voidwhenKeyReleased(int keyCode) This method is called when a key is released.voidwhenMouseClicked(MouseCode mouseEvent) This method is called when a mouse click event occurs.voidwhenMouseWheelMoved(int steps) This method is called when the mouse wheel is moved.
-
Constructor Details
-
Stage
public Stage()Constructs a new Stage with default dimensions. The default width is 480 pixels and the default height is 360 pixels. -
Stage
public Stage(int width, int height) Constructs a new Stage with the specified width and height.- Parameters:
width- the width of the stageheight- the height of the stage
-
Stage
Constructs a new Stage with the specified width, height, and assets path.- Parameters:
width- the width of the stageheight- the height of the stageassets- the path to the assets directory
-
Stage
public Stage(boolean fullScreen) Constructs a new Stage.- Parameters:
fullScreen- a boolean indicating whether the stage should be in full screen mode.
-
Stage
Constructs a new Stage with the specified fullscreen mode and assets path.- Parameters:
fullScreen- a boolean indicating whether the stage should be in fullscreen modeassets- the path to the assets directory
-
Stage
public Stage(boolean fullScreen, int width, int height) Constructs a new Stage with the specified width and height.- Parameters:
fullScreen- whether the stage should be in full screen modewidth- the width of the stageheight- the height of the stage
-
Stage
Constructs a new Stage with the specified parameters.- Parameters:
fullScreen- whether the stage should be in full screen modewidth- the width of the stageheight- the height of the stageassets- the path to the assets directory
-
-
Method Details
-
setDebug
public void setDebug(boolean debug) Enables or disables the debug mode for the application.- Parameters:
debug- a boolean value wheretrueenables debug mode andfalsedisables it.
-
isDebug
public boolean isDebug()Checks if the application is in debug mode.- Returns:
- true if the application is in debug mode, false otherwise.
-
add
-
add
-
add
Adds a Pen object to the stage.- Parameters:
pen- the Pen object to be added to the stage
-
setTextureSampling
public void setTextureSampling(int mode) Sets the texture sampling mode Point sampling: both magnification and minification filtering are set to nearest. Linear sampling: magnification filtering is nearest, minification set to linear Bilinear sampling: both magnification filtering is set to linear and minification either to linear-mipmap-nearest (linear interpolation is used within a mipmap, but not between different mipmaps). Trilinear sampling: magnification filtering set to linear, minification to linear-mipmap-linear, which offers the best mipmap quality since linear interpolation to compute the value in each of two maps and then interpolates linearly between these two values.- Parameters:
mode- the texture sampling mode. 2: Point Sampling. 3: Linear. 4: Bilinear. 5: Trilinear.
-
addShader
Adds a new shader to the sprite. If a shader with the received name already exists, this method- Parameters:
name-fragmentShaderPath- The path to the fragment shadervertexShaderPath- The path to the vertex shader- Returns:
- the shader
-
switchShader
Switch to a shader by name.- Parameters:
name- the name of a shader
-
switchShader
public void switchShader(double index) Switch to a shader by index.- Parameters:
index- the index of a shader
-
resetShader
public void resetShader() -
getShader
-
nextShader
public void nextShader()Sets the next shader as the current shader. -
getCurrentShaderName
Retrieves the name of the current shader.- Returns:
- the name of the current shader, or null if no shaders exist
-
getCurrentShaderIndex
public int getCurrentShaderIndex()Retrieves the index of the current shader.- Returns:
- the index of the current shader
-
getCurrentShader
Retrieves the current shader.- Returns:
- the current shader, or null if no shaders exist
-
goLayersBackwards
Moves the specified sprite backwards by a given number of layers in the sprite list. If the resulting position is less than zero, the sprite is moved to the first position. If the resulting position is greater than the last index, the sprite is moved to the last position.- Parameters:
sprite- the sprite to be moved backwards in the layer ordernumber- the number of layers to move the sprite backwards
-
goLayersForwards
Moves the specified sprite forward by a given number of layers in the sprite list. If the resulting position is out of bounds, it will be adjusted to the nearest valid position.- Parameters:
sprite- the sprite to be moved forward in the layer ordernumber- the number of layers to move the sprite forward
-
goToFrontLayer
Moves the specified sprite to the front layer.- Parameters:
sprite- the sprite to be moved to the front layer
-
goToBackLayer
Moves the specified sprite to the back layer of the stage.- Parameters:
sprite- the sprite to be moved to the back layer
-
goToUILayer
Moves the specified sprite to the UI layer by removing it from the current list of sprites.- Parameters:
sprite- the sprite to be moved to the UI layer
-
setSorter
Sets a custom sorter for the sprites. Use enableYSort() to enable the sorting of sprites using the y-coordinates. This overwrites goToBackLayer(), goToFrontLayer(), goLayersBackwards() and goLayersForwards().- Parameters:
sorter- the comparator used to sort the sprites- See Also:
-
enableYSort
public void enableYSort()Enables the sorting of sprites using y-sorting. This means that sprites with a lower y-coordinate will be drawn on top of sprites with a higher y-coordinate. This sorting respects the height of the sprites. This overwrites goToBackLayer(), goToFrontLayer(), goLayersBackwards() and goLayersForwards(). -
disableSort
public void disableSort()Disables the sorting of sprites. -
isSortEnabled
public boolean isSortEnabled()Checks if the sorting of sprites is enabled.- Returns:
- true if sorting is enabled, false otherwise
-
getAll
-
remove
Removes the specified sprite from the stage.- Parameters:
sprite- the sprite to be removed
-
remove
Removes the specified pen from the stage.- Parameters:
pen- the pen to be removed
-
remove
Removes the specified text from the stage.- Parameters:
text- the text to be removed
-
removeAll
public void removeAll()Removes all elements from the stage. -
removeAllSprites
public void removeAllSprites()Removes all sprites from the stage. -
removeAllTexts
public void removeAllTexts()Removes all texts from the stage. -
removeAllPens
public void removeAllPens()Removes all pens from the stage. -
remove
-
find
-
findSpritesOf
-
findTextsOf
-
findPensOf
-
count
-
countSprites
public long countSprites()Returns the number of sprites in the stage.- Returns:
- the number of sprites
-
countSpritesOf
-
countTexts
public long countTexts()Returns the number of texts in the stage.- Returns:
- the number of texts
-
countTextsOf
-
countPens
public long countPens()Returns the number of pens- Returns:
- the number of pens
-
countPensOf
-
addBackdrop
-
addBackdrop
-
removeBackdrop
Remove a backdrop from the stage.- Parameters:
name- of the backdrop
-
switchBackdrop
Switch to a backdrop by name.- Parameters:
name- the name of a backdrop
-
whenBackdropSwitches
This method is called when the backdrop switches to the specified name. Override this method to add custom behavior.- Parameters:
name- the name of the backdrop to switch to
-
setWhenBackdropSwitches
Sets the handler for when the backdrop switches.- Parameters:
whenBackdropSwitches- the handler to set
-
nextBackdrop
public void nextBackdrop()Switch to the next backdrop. -
previousBackdrop
public void previousBackdrop()Switch to the previous backdrop. -
randomBackdrop
public void randomBackdrop()Switch to a random backdrop. -
getCurrentBackdropName
-
getCurrentBackdropIndex
public int getCurrentBackdropIndex()Returns the current backdrop index- Returns:
- a backdrop index
-
eraseAll
public void eraseAll()Erases all lines on the pen layer. -
eraseBackground
public void eraseBackground()When this method is called, the background buffer will be erased. -
eraseForeground
public void eraseForeground()When this method is called, the foreground buffer will be erased. -
getForegroundPixels
public int[] getForegroundPixels()Returns the pixels of the foreground buffer.- Returns:
- the pixels of the foreground buffer
-
getBackgroundPixels
public int[] getBackgroundPixels()Returns the pixels of the background buffer.- Returns:
- the pixels of the background buffer
-
getPixels
public int[] getPixels()Returns the pixels of the main buffer.- Returns:
- the pixels of the main buffer
-
eraseUI
public void eraseUI()This method marks the UI buffer to be erased, which will be processed in the next update cycle. -
addSound
-
removeSound
Remove a sound from the stage.- Parameters:
name- the sound name
-
playSound
-
stopAllSounds
public void stopAllSounds()Stops the playing of all sounds of the stage. -
stopSound
Stops the playing of the sound with the given name- Parameters:
name- Name of the sound
-
isSoundPlaying
-
setColor
public void setColor(double h) Sets the background color via a hue value- Parameters:
h- a hue value [0...255]
-
setColor
public void setColor(double r, double g, double b) Sets the background color via a rgb value- Parameters:
r- a red value [0...255]g- a green value [0...255]b- a blue value [0...255]
-
getColor
-
setColor
Sets the color of the stage.- Parameters:
c- the new color to be set- See Also:
-
changeColor
public void changeColor(double h) Changes the background color by adding a step to the hue value.- Parameters:
h- a step value
-
setTint
public void setTint(double r, double g, double b) Sets the tint for the current backdrop with rgb.- Parameters:
r- a red value [0...255]g- a green value [0...255]b- a blue value [0...255]- See Also:
-
setTint
public void setTint(double h) Sets the tint for the current backdrop with a hue.- See Also:
-
changeTint
public void changeTint(double step) Changes the tint for the current backdrop- Parameters:
step- a step value- See Also:
-
setTransparency
public void setTransparency(double transparency) Sets the transparency of the current backdrop.- Parameters:
transparency- a transparency value [0...1]- See Also:
-
changeTransparency
public void changeTransparency(double step) Changes the transparency for the current costume.- Parameters:
step- a step value- See Also:
-
getWidth
public int getWidth()Return the width of the current costume or the pen size, when no costume is available.- Returns:
- the width of the sprite
-
getHeight
public int getHeight()Return the height of the current costume or the pen size, when no costume is available.- Returns:
- the height of the sprite
-
getTimer
-
getTimer
-
addTimer
Add a new timer by name. Overwriting default is not permitted.- Parameters:
name- the name of the timer
-
removeTimer
Remove a timer by name. Removing of default is not permitted.- Parameters:
name- the name of the timer
-
mouseEvent
public void mouseEvent(processing.event.MouseEvent e) - Parameters:
e-
-
whenMouseClicked
This method is called when a mouse click event occurs. Overwrite this method to add custom behavior.- Parameters:
mouseEvent- The mouse event that triggered this method.
-
setWhenMouseClicked
Sets the handler for mouse click events.- Parameters:
whenMouseClicked- a handler that takes a Stage and MouseCode as
-
whenMouseWheelMoved
public void whenMouseWheelMoved(int steps) This method is called when the mouse wheel is moved. Overwrite this method to add custom behavior.- Parameters:
steps- the number of steps the mouse wheel has moved. Positive values indicate movement away from the user, while negative values indicate movement towards the user.
-
setWhenMouseWheelMoved
Sets the handler for mouse wheel movement events.- Parameters:
whenMouseWheelMoved- a handler that takes a Stage and an integer
-
getMouseX
public double getMouseX()Returns the current x-position of the mouse cursor- Returns:
- x-position
-
getMouseY
public double getMouseY()Returns the current y-position of the mouse cursor- Returns:
- y-position
-
getMouse
Returns the current position of the mouse cursor as a Vector2- Returns:
- mouse position
-
isMouseDown
public boolean isMouseDown()Returns true is the mouse button is down- Returns:
- mouse button down
-
whenKeyPressed
public void whenKeyPressed(int keyCode) This method is called when a key is pressed. Override this method to add custom behavior.- Parameters:
keyCode- the code of the key that was pressed
-
setWhenKeyPressed
Sets the handler for key press events.- Parameters:
whenKeyPressed- a handler that takes a Stage and an Integer (the key code) as arguments
-
whenKeyReleased
public void whenKeyReleased(int keyCode) This method is called when a key is released. Override this method to add custom behavior.- Parameters:
keyCode- the code of the key that was released
-
setWhenKeyReleased
Sets the handler for key release events.- Parameters:
whenKeyReleased- a handler that takes a Stage and an Integer (the key code) as arguments
-
keyEvent
public void keyEvent(processing.event.KeyEvent e) - Parameters:
e-
-
isKeyPressed
public boolean isKeyPressed(int keyCode) Returns true if the key is pressed- Parameters:
keyCode- a key code- Returns:
- key pressed
-
getDeltaTime
public double getDeltaTime()Gets the seconds passed since the last frame.- Returns:
- secons since last frame
-
getCurrentYear
public int getCurrentYear()Returns the current year- Returns:
- current year
-
getCurrentMonth
public int getCurrentMonth()Returns the current month- Returns:
- current month
-
getCurrentDayOfWeek
public int getCurrentDayOfWeek()Returns the current day of the week- Returns:
- current day of the week
-
getCurrentDay
public int getCurrentDay()Returns the current day of the month- Returns:
- current day of the month
-
getCurrentHour
public int getCurrentHour()Returns the current hour- Returns:
- current hour
-
getCurrentMinute
public int getCurrentMinute()Returns the current minute- Returns:
- current minute
-
getCurrentSecond
public int getCurrentSecond()Returns the current second- Returns:
- current second
-
getCurrentMillisecond
public int getCurrentMillisecond()Returns the current millisecond- Returns:
- current millisecond
-
getDaysSince2000
public int getDaysSince2000()Returns the days since 2010/01/01- Returns:
- days since 2010/01/01
-
pickRandom
public int pickRandom(int from, int to) Returns a random integer between the specified range (inclusive).- Parameters:
from- the lower bound of the range (inclusive)to- the upper bound of the range (inclusive)- Returns:
- a random integer between
fromandto(inclusive)
-
display
Displays the given text on the stage.- Parameters:
text- the text to be displayed
-
display
Displays the given text on the screen for a specified duration.- Parameters:
text- The text to be displayed.millis- The duration in milliseconds for which the text will be displayed.
-
broadcast
Broadcasts a message to all sprites in the stage. Each sprite will execute its `whenIReceive` method with the given message.- Parameters:
message- The message to broadcast to all sprites.
-
broadcast
Broadcasts a message to all sprites in the stage. Each sprite will execute its `whenIReceive` method with the given message.- Parameters:
message- The message to broadcast to all sprites.
-
whenIReceive
This method is called when a specific message is received. Override this method to add custom behavior.- Parameters:
message- The message that triggers this method.
-
whenIReceive
This method is called when a message is received.- Parameters:
message- The message object that is received.
-
setWhenIReceive
Sets the handler for when a message is received.- Parameters:
whenIReceive- a handler that takes a Stage and a String (the message) as arguments
-
setCursor
Sets the cursor image for the stage.- Parameters:
path- the file path to the cursor image
-
setCursor
Sets the cursor image and its active spot coordinates.- Parameters:
path- the file path to the cursor imagex- the x-coordinate of the cursor's active spoty- the y-coordinate of the cursor's active spot
-
getCamera
Retrieves the current camera instance associated with this stage.- Returns:
- the current Camera object.
-
wait
public void wait(int millis) Stop the execution of the whole applications for the given milliseconds.- Parameters:
millis- Milliseconds
-
getFrameRate
public double getFrameRate()Get the frame rate of the application.- Returns:
- the frame rate
-
getWindow
-
run
public void run()Executes the main logic of the stage. This method should be overridden by subclasses to define the specific behavior of the stage. -
addStampsToForeground
- Parameters:
stamp-
-
addStampsToForeground
-
addStampsToBackground
- Parameters:
stamp-
-
addStampsToBackground
-
setRun
Sets the main logic of the stage to be executed during each frame.- Parameters:
run- the RunHandler that defines the main logic of the stage
-
pre
public void pre() -
exit
public void exit()Close the window and therefore the whole application. -
draw
public void draw(processing.core.PGraphics buffer) - Parameters:
buffer-
-