Class Window

java.lang.Object
org.openpatch.scratch.Window

public class Window extends Object
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

    Constructors
    Constructor
    Description
    A window of 480 × 360, Scratch's stage size.
    Window(int width, int height)
    A window whose stage has this size in stage pixels.
    Window(int width, int height, String assets)
    A window of a size that loads a folder of assets first.
    Window(String assets)
    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 Type
    Method
    Description
    void
    debug(Object... values)
    Prints values, but only in debug mode.
    void
    Ends the program: whenExits() runs first.
    static double
     
    double
     
    int
     
    static Window
     
     
     
     
    int
     
    boolean
     
    boolean
     
    static boolean
     
    static boolean
     
    boolean
     
    void
    Pauses the game: no ticks run; keys and the mouse still reach the stage and sprites.
    void
    Goes on after pause().
    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 lets debug(…) print.
    void
    setGameSpeed(double speed)
    How fast the game runs: 1 is normal, 2 twice as fast, 0.5 half as fast; up to 8.
    void
    setStage(Stage stage)
    Shows a stage from the next tick on.
    void
    While paused: runs exactly one tick.
    void
    transitionToStage(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.
    void
    transitionToStage(Supplier<? extends Stage> stage, Transition transition, double seconds)
    Shows a stage made now through a transition.
    void
    transitionToStage(Stage stage, double seconds)
    Fades to a stage through black.
    static void
    use(Extension extension)
    Uses an extension that does not register itself through META-INF/services (one in your own project).
    static void
    useFrameSmoothing(double percent)
    Frame smoothing: each frame is presented mixed with the one before it, this much in percent.
    static void
    Opens the window full screen (a borderless window at the screen's size).
    static void
    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.
    static void
    Smooth pixel art (Godot's "canvas items" way of showing pixel art): as usePixelArt(), but the camera moves without snapping and zoom works at any factor.
    static void
    The logo on the loading screen.
    void
    Called when the program ends, on the game thread.

    Methods inherited from class Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Window

      public Window()
      A window of 480 × 360, Scratch's stage size.
    • Window

      public Window(String assets)
      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 width
      height - the stage height
    • Window

      public Window(int width, int height, String assets)
      A window of a size that loads a folder of assets first.
      Parameters:
      width - the stage width
      height - the stage height
      assets - the folder, or null
  • Method Details

    • getInstance

      public static Window 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 before new 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 before new 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): as usePixelArt(), 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 need useFrameSmoothing(double). Call it before new 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 before new 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

      public static void useSplashLogo(String path)
      The logo on the loading screen. Call it before new Window.
      Parameters:
      path - an image
    • use

      public static void use(Extension extension)
      Uses an extension that does not register itself through META-INF/services (one in your own project). Call it before new 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 after pause().
    • 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 lets debug(…) print. F12 switches it too, and -Dscratch4j.debug=… presets which options are on.
      Parameters:
      debug - on or off
    • debug

      public void debug(Object... values)
      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

      public void setStage(Stage stage)
      Shows a stage from the next tick on.
      Parameters:
      stage - the stage
    • getStage

      public Stage getStage()
      Returns:
      the stage shown
    • transitionToStage

      public void transitionToStage(Stage stage, double seconds)
      Fades to a stage through black.
      Parameters:
      stage - the stage
      seconds - how long
    • transitionToStage

      public void transitionToStage(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.
      Parameters:
      stage - makes the stage, e.g. () -> new Level2()
      seconds - how long
    • 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 stage
      transition - how
      seconds - 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

      public String getLibraryVersion()
      Returns:
      the version of Scratch for Java, e.g. "7.0.0"
    • getLibraryTitle

      public String getLibraryTitle()
      Returns:
      "Scratch for Java"