Class Window
java.lang.Object
org.openpatch.scratch.Window
The window of a program: there is one, created first, and it shows one stage at a time.
public static void main(String[] args) {
Window window = new Window(480, 360);
window.setStage(new MyStage());
}
The constructor returns at once; the window runs on a thread of its own and the game ticks
60 times a second of game time. main may carry on, e.g. with
stage.waitSeconds(…) between steps.
-
Constructor Summary
ConstructorsConstructorDescriptionWindow()A window of 480 × 360, Scratch's stage size.Window(int width, int height) A window whose stage has this size in stage pixels.A window of a size that loads a folder of assets first.A window of 480 × 360 that loads every image, sound and font in a folder before the first frame, showing a loading screen. -
Method Summary
Modifier and TypeMethodDescriptionvoidPrints values, but only in debug mode.voidexit()Ends the program:whenExits()runs first.static doubledoubleintstatic WindowgetStage()intgetWidth()booleanisDebug()booleanisPaused()static booleanstatic booleanbooleanvoidpause()Pauses the game: no ticks run; keys and the mouse still reach the stage and sprites.voidresume()Goes on afterpause().voidsetDebug(boolean debug) Debug mode shows what its panel in the bottom right corner has ticked: hitboxes, positions, the mouse and camera, touching pairs, hidden sprites, the clones and the frame rate, and it letsdebug(…)print.voidsetGameSpeed(double speed) How fast the game runs: 1 is normal, 2 twice as fast, 0.5 half as fast; up to 8.voidShows a stage from the next tick on.voidstep()While paused: runs exactly one tick.voidtransitionToStage(Supplier<? extends Stage> stage, double seconds) Fades to a stage made now, on the game thread: a slow constructor shows the loading screen instead of freezing the window.voidtransitionToStage(Supplier<? extends Stage> stage, Transition transition, double seconds) Shows a stage made now through a transition.voidtransitionToStage(Stage stage, double seconds) Fades to a stage through black.static voidUses an extension that does not register itself throughMETA-INF/services(one in your own project).static voiduseFrameSmoothing(double percent) Frame smoothing: each frame is presented mixed with the one before it, this much in percent.static voidOpens the window full screen (a borderless window at the screen's size).static voidPixel-art mode: images are not smoothed, the stage scales by whole numbers only, the default font is Jersey 10 and font sizes are rounded to its pixels.static voidSmooth pixel art (Godot's "canvas items" way of showing pixel art): asusePixelArt(), but the camera moves without snapping and zoom works at any factor.static voiduseSplashLogo(String path) The logo on the loading screen.voidCalled when the program ends, on the game thread.
-
Constructor Details
-
Window
public Window()A window of 480 × 360, Scratch's stage size. -
Window
A window of 480 × 360 that loads every image, sound and font in a folder before the first frame, showing a loading screen.- Parameters:
assets- the folder, e.g."assets"
-
Window
public Window(int width, int height) A window whose stage has this size in stage pixels. The window opens at that size (in pixel art at the largest whole multiple that fits the screen) and scales the stage to fit when it is resized.- Parameters:
width- the stage widthheight- the stage height
-
Window
A window of a size that loads a folder of assets first.- Parameters:
width- the stage widthheight- the stage heightassets- the folder, or null
-
-
Method Details
-
getInstance
- Returns:
- the window, or null before it was created
-
useFullScreen
public static void useFullScreen()Opens the window full screen (a borderless window at the screen's size). Call it beforenew Window. -
usePixelArt
public static void usePixelArt()Pixel-art mode: images are not smoothed, the stage scales by whole numbers only, the default font is Jersey 10 and font sizes are rounded to its pixels. Call it beforenew Window. -
isPixelArt
public static boolean isPixelArt()- Returns:
- whether pixel-art mode is on
-
useSmoothPixelArt
public static void useSmoothPixelArt()Smooth pixel art (Godot's "canvas items" way of showing pixel art): asusePixelArt(), but the camera moves without snapping and zoom works at any factor. The world is drawn at the window's resolution with the sprites still sampled by nearest neighbour, so every pixel stays a sharp square while things move — the grid wobbles by a screen pixel instead of the picture stepping whole stage pixels (the look of Hyper Light Drifter, as Godot's guide puts it). With it, scrolling does not needuseFrameSmoothing(double). Call it beforenew Window. -
isSmoothPixelArt
public static boolean isSmoothPixelArt()- Returns:
- whether the pixel art scrolls smoothly (see
useSmoothPixelArt())
-
useFrameSmoothing
public static void useFrameSmoothing(double percent) Frame smoothing: each frame is presented mixed with the one before it, this much in percent. A screen that holds each frame until the next (every 60 Hz LCD) smears whatever moves, because the eye gets one picture per 16.7 ms; two pictures per frame (50 is a good value) look like one picture per 8.3 ms and smear about half as much. The world keeps its pixels: the moment nothing moves, current and previous frame are the same and the picture is fully sharp. While things move, edges are softer — that is the trade. Call it beforenew Window; 0 (the default) turns it off.- Parameters:
percent- how much of the previous frame each presented one keeps, 0 to 100
-
getFrameSmoothing
public static double getFrameSmoothing()- Returns:
- the frame smoothing in percent, 0 when off
-
useSplashLogo
The logo on the loading screen. Call it beforenew Window.- Parameters:
path- an image
-
use
Uses an extension that does not register itself throughMETA-INF/services(one in your own project). Call it beforenew Window.- Parameters:
extension- the extension
-
pause
public void pause()Pauses the game: no ticks run; keys and the mouse still reach the stage and sprites. -
resume
public void resume()Goes on afterpause(). -
isPaused
public boolean isPaused()- Returns:
- whether the game is paused
-
step
public void step()While paused: runs exactly one tick. -
setGameSpeed
public void setGameSpeed(double speed) How fast the game runs: 1 is normal, 2 twice as fast, 0.5 half as fast; up to 8. Each tick still is 1/60 s of game time; there are more or fewer of them per second.- Parameters:
speed- the game speed
-
getGameSpeed
public double getGameSpeed()- Returns:
- the game speed
-
isDebug
public boolean isDebug()- Returns:
- whether debug mode is on
-
setDebug
public void setDebug(boolean debug) Debug mode shows what its panel in the bottom right corner has ticked: hitboxes, positions, the mouse and camera, touching pairs, hidden sprites, the clones and the frame rate, and it letsdebug(…)print. F12 switches it too, and-Dscratch4j.debug=…presets which options are on.- Parameters:
debug- on or off
-
debug
Prints values, but only in debug mode.- Parameters:
values- what to print
-
getWidth
public int getWidth()- Returns:
- the stage width in stage pixels
-
getHeight
public int getHeight()- Returns:
- the stage height in stage pixels
-
setStage
Shows a stage from the next tick on.- Parameters:
stage- the stage
-
getStage
- Returns:
- the stage shown
-
transitionToStage
Fades to a stage through black.- Parameters:
stage- the stageseconds- how long
-
transitionToStage
-
transitionToStage
public void transitionToStage(Supplier<? extends Stage> stage, Transition transition, double seconds) Shows a stage made now through a transition.window.transitionToStage(() -> new Level2(), Transition.SLIDE_LEFT, 0.5);- Parameters:
stage- makes the stagetransition- howseconds- how long
-
isTransitioning
public boolean isTransitioning()- Returns:
- whether a transition is in progress
-
exit
public void exit()Ends the program:whenExits()runs first. -
whenExits
public void whenExits()Called when the program ends, on the game thread. Override it to save, say. -
getLibraryVersion
- Returns:
- the version of Scratch for Java, e.g. "7.0.0"
-
getLibraryTitle
- Returns:
- "Scratch for Java"
-