Scratch for Java

Documentation

The org.openpatch.scratch package contains classes that provide an API for creating Scratch-like projects in Java.

The documentation with many examples can be found at https://scratch4j.openpatch.org.

To create a scratch4j project, you will need to create a class that extends the Window class.

The Window class is the main class that represents the window of the project.

*
 public class Game extends Window {
      public Game() {
          super(800, 600);
          this.setStage(new MyStage());
      }
 }

The Stage class represents the stage of the project.

*
 class MyStage extends Stage {
      public MyStage() {
          this.add(new MySprite());
      }
 }

The Sprite class represents a sprite in the project.

*
 class MySprite extends Sprite {
      public MySprite() {
          this.setCostume("cat_sitting", "cat.png");
      }
 
      public void run() {
          this.ifOnEdgeBounce();
          this.move(10);
      }
 }

This will create a window with a stage containing a sprite that moves 10 pixels every frame and bounces off the edges of the window.

Running the examples here

Nearly every method on the following pages comes with an example you can start and change without leaving the page. Those run in your browser rather than on your own computer, and there is one difference worth knowing about: a whole number that is a double prints here without its decimal part, so println("x: " + mySprite.getX()) says x: 0 in the browser and x: 0.0 when you run the same program yourself. The value is the same; only the way it is written out differs.

Classes

Documentation

Create Shareable URL

Select Sections