Class Window

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

public class Window extends Object
The Window class represents a singleton window for the application. It provides various constructors to create a window with different configurations such as dimensions, full screen mode, and asset paths. The class ensures that only one instance of the window can be created at any time.

The class also provides methods to interact with the window and the application, such as retrieving the window dimensions, setting the debug mode, managing stages, and exiting the application.

Example usage:


 Window window = new Window();
 window.setDebug(true);
 
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int[]
    The default color used for debugging purposes.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs a new Window with default dimensions.
    Window(int width, int height)
    Constructs a new Window with the specified width and height.
    Window(int width, int height, String assets)
    Constructs a new Window with the specified width, height, and assets path.
    Window(String assets)
    Constructs a new Window with the specified assets.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    debug(Object... values)
    Prints a debug message to stdout when debug mode is enabled.
    void
    Exits the application by invoking the `whenExits` method and then calling the `exit` method on the `Applet` instance.
    double
    Gets the seconds passed since the last frame.
    int
    Retrieves the height of the current window.
    static Window
    Returns the singleton instance of the Window class.
    Returns the title of the library.
    Returns the version of the library.
    static String
    Returns the picture the loading screen shows.
    Retrieves the current stage from the Applet instance.
    Returns how pictures are being smoothed.
    int
    Retrieves the width of the current window.
    boolean
    Checks if the application is in debug mode.
    void
    setDebug(boolean debug)
    Enables or disables the debug mode for the application.
    void
    setStage(Stage stage)
    Sets the current stage of the application.
    void
    transitionToStage(Stage stage, int duration)
    Transitions to a new stage with a specified duration.
    static void
    Makes the window fill the whole screen.
    static void
    Puts your own picture on the loading screen in place of the Scratch for Java logo.
    static void
    Chooses how pictures are smoothed when they are drawn larger or smaller than they really are.
    void
    This method is called when the window exits.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • DEBUG_COLOR

      public static final int[] DEBUG_COLOR
      The default color used for debugging purposes. You can modify this array to change the debug color.

      Debug mode can be toggled at runtime by pressing F12.

      For example, to set the debug color to green, you can do:

      
       Window.DEBUG_COLOR = new int[] { 0, 255, 0 };
       
  • Constructor Details

    • Window

      public Window()
      Constructs a new Window with default dimensions. The default width is 480 pixels and the default height is 360 pixels.
    • Window

      public Window(String assets)
      Constructs a new Window with the specified assets. The window will have a default width of 480 pixels and a height of 360 pixels.
      Parameters:
      assets - the path to the assets to be used in the window
    • Window

      public Window(int width, int height)
      Constructs a new Window with the specified width and height.
      Parameters:
      width - the width of the window
      height - the height of the window
    • Window

      public Window(int width, int height, String assets)
      Constructs a new Window with the specified width, height, and assets path. Ensures that only one instance of Window can be created.

      The single exception is the window Java builds by itself: a class that extends Window and has a main method which is not static is instantiated by the JVM before main is called, so the new MyWindow() in that main would be the second window. That one reuses the first instead of failing.

      Parameters:
      width - the width of the window
      height - the height of the window
      assets - the path to the assets
      Throws:
      Error - if an instance of Window already exists
  • Method Details

    • useFullScreen

      public static void useFullScreen()
      Makes the window fill the whole screen.

      Call it before the first stage is created, because the size of the window is settled the moment it opens:

      
       public static void main(String[] args) {
         Window.useFullScreen();
         new MyStage();
       }
       
    • useTextureSampling

      public static void useTextureSampling(TextureSampling sampling)
      Chooses how pictures are smoothed when they are drawn larger or smaller than they really are. Pixel art usually wants TextureSampling.POINT.

      Call it before the first stage is created.

      
       Window.useTextureSampling(TextureSampling.POINT);
       new MyStage();
       
      Parameters:
      sampling - how to smooth pictures
    • getTextureSampling

      public static TextureSampling getTextureSampling()
      Returns how pictures are being smoothed.
      Returns:
      the current setting
    • useSplashLogo

      public static void useSplashLogo(String path)
      Puts your own picture on the loading screen in place of the Scratch for Java logo. The picture fades in while your project starts and fades out again once it is ready.

      It is shrunk to fit if it is large, and never enlarged, so a picture of roughly 300 by 300 looks right in a window of the usual size.

      Call it before the first stage is created, because the loading screen is the first thing the window draws:

      
       public static void main(String[] args) {
         Window.useSplashLogo("assets/logo.png");
         new MyStage();
       }
       
      Parameters:
      path - a path to an image file or the name of a built-in sprite, or null for the Scratch for Java logo
    • getSplashLogo

      public static String getSplashLogo()
      Returns the picture the loading screen shows.
      Returns:
      the path set with useSplashLogo(java.lang.String), or null for the built-in logo
    • getInstance

      public static Window getInstance()
      Returns the singleton instance of the Window class.
      Returns:
      the singleton instance of Window
    • getDeltaTime

      public double getDeltaTime()
      Gets the seconds passed since the last frame.
      Returns:
      seconds since last frame
    • isDebug

      public boolean isDebug()
      Checks if the application is in debug mode.

      Debug mode can be toggled at runtime by pressing F12.

      Returns:
      true if the application is in debug mode, false otherwise.
    • setDebug

      public void setDebug(boolean debug)
      Enables or disables the debug mode for the application.

      Debug mode shows sprite positions, hitboxes, and other debug information. It can also be toggled at runtime by pressing F12.

      Parameters:
      debug - a boolean value where true enables debug mode and false disables it.
    • debug

      public void debug(Object... values)
      Prints a debug message to stdout when debug mode is enabled. The message is prefixed with the window's class name.

      Example:

      
       this.debug("fps =", getFps());
       // prints: [MyWindow] fps = 60
       
      Parameters:
      values - one or more values to print
    • getWidth

      public int getWidth()
      Retrieves the width of the current window.
      Returns:
      the width of the window in pixels
    • getHeight

      public int getHeight()
      Retrieves the height of the current window.
      Returns:
      the height of the window in pixels
    • setStage

      public void setStage(Stage stage)
      Sets the current stage of the application.
      Parameters:
      stage - the new stage to be set
    • transitionToStage

      public void transitionToStage(Stage stage, int duration)
      Transitions to a new stage with a specified duration.
      Parameters:
      stage - the new stage to transition to
      duration - the duration of the transition in milliseconds
    • getStage

      public Stage getStage()
      Retrieves the current stage from the Applet instance.
      Returns:
      the current Stage object
    • exit

      public void exit()
      Exits the application by invoking the `whenExits` method and then calling the `exit` method on the `Applet` instance.
    • whenExits

      public void whenExits()
      This method is called when the window exits. Override this method to define custom behavior when the window is closed.
    • getLibraryVersion

      public String getLibraryVersion()
      Returns the version of the library.
      Returns:
      the version of the library as a String
    • getLibraryTitle

      public String getLibraryTitle()
      Returns the title of the library.
      Returns:
      the title of the library as a String