Class Window
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
FieldsModifier and TypeFieldDescriptionstatic final int[]The default color used for debugging purposes. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidPrints a debug message to stdout when debug mode is enabled.voidexit()Exits the application by invoking the `whenExits` method and then calling the `exit` method on the `Applet` instance.doubleGets the seconds passed since the last frame.intRetrieves the height of the current window.static WindowReturns the singleton instance of the Window class.Returns the title of the library.Returns the version of the library.static StringReturns the picture the loading screen shows.getStage()Retrieves the current stage from the Applet instance.static TextureSamplingReturns how pictures are being smoothed.intgetWidth()Retrieves the width of the current window.booleanisDebug()Checks if the application is in debug mode.voidsetDebug(boolean debug) Enables or disables the debug mode for the application.voidSets the current stage of the application.voidtransitionToStage(Stage stage, int duration) Transitions to a new stage with a specified duration.static voidMakes the window fill the whole screen.static voiduseSplashLogo(String path) Puts your own picture on the loading screen in place of the Scratch for Java logo.static voiduseTextureSampling(TextureSampling sampling) Chooses how pictures are smoothed when they are drawn larger or smaller than they really are.voidThis method is called when the window exits.
-
Field Details
-
DEBUG_COLOR
public static final int[] DEBUG_COLORThe 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
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 windowheight- the height of the window
-
Window
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
mainmethod which is not static is instantiated by the JVM beforemainis called, so thenew MyWindow()in thatmainwould be the second window. That one reuses the first instead of failing.- Parameters:
width- the width of the windowheight- the height of the windowassets- 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
Chooses how pictures are smoothed when they are drawn larger or smaller than they really are. Pixel art usually wantsTextureSampling.POINT.Call it before the first stage is created.
Window.useTextureSampling(TextureSampling.POINT); new MyStage();- Parameters:
sampling- how to smooth pictures
-
getTextureSampling
Returns how pictures are being smoothed.- Returns:
- the current setting
-
useSplashLogo
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, ornullfor the Scratch for Java logo
-
getSplashLogo
Returns the picture the loading screen shows.- Returns:
- the path set with
useSplashLogo(java.lang.String), ornullfor the built-in logo
-
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 wheretrueenables debug mode andfalsedisables it.
-
debug
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
Sets the current stage of the application.- Parameters:
stage- the new stage to be set
-
transitionToStage
Transitions to a new stage with a specified duration.- Parameters:
stage- the new stage to transition toduration- the duration of the transition in milliseconds
-
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
Returns the version of the library.- Returns:
- the version of the library as a String
-
getLibraryTitle
Returns the title of the library.- Returns:
- the title of the library as a String
-