Scratch for Java is a Java library that replicates the functionality and concepts of Scratch, helping learners transition from block-based programming to text-based coding in Java. The library provides an approachable API inspired by Scratch blocks, making it easier for beginners to understand programming concepts while gaining experience with real Java syntax and tools.
- Main Package:
org.openpatch.scratch - Current Version: 4.24.1
- Java Version: 17
- Build Tool: Maven
- Documentation Site: https://scratch4j.openpatch.org
- Repository Size: ~5,500 lines of core Java code, 146 reference examples, 59 demo projects
The library is structured around three core concepts:
- Window: The main application window (extends
Windowclass) - Stage: The game/application stage where sprites and elements are displayed
- Sprite: Interactive objects that can move, detect collisions, play sounds, etc.
- Processing 4.4.6: Core graphics and windowing framework
- Jackson 2.19.2: JSON/XML processing
- JOGL 2.5.0: OpenGL bindings (from jogamp repository)
- Gluegen 2.5.0: Native library loading
- Java 17+ (verified with OpenJDK 17.0.16)
- Maven 3.9.11+
- Node.js + npm (for documentation)
Standard Build Commands:
mvn clean compile # Basic compilation
mvn clean package # Create standard JAR
mvn clean test # Run tests (if any)Build Profiles:
# For Maven Central release (includes native dependencies)
mvn clean compile -Pcentral
# For standalone JAR with all dependencies
mvn clean package -Pall # Creates target/*-all.jarNote: The library uses JOGL for OpenGL graphics rendering, which requires access to the jogamp.org repository. This dependency is now working correctly in the CI environment.
The documentation uses Hyperbook (Node.js-based static site generator):
cd docs
npx hyperbook dev # Start development server
npx hyperbook build # Build static documentationDocumentation includes:
- English and German versions (
docs/en/anddocs/de/) - Reference examples with generated GIFs
- API documentation generated from Java source
Use the s4j script to check documentation coverage:
./s4j # Check documentation coverage
./s4j --create # Create missing documentation filesThis script validates that all public classes and methods have corresponding documentation files in both English and German.
src/
├── main/java/org/openpatch/scratch/
│ ├── Stage.java # Core stage class (1,907 lines)
│ ├── Sprite.java # Core sprite class (2,173 lines)
│ ├── Window.java # Main window class (304 lines)
│ ├── extensions/ # Extension modules
│ │ ├── animation/ # Sprite animation support
│ │ ├── camera/ # Camera/viewport functionality
│ │ ├── color/ # Color manipulation
│ │ ├── fs/ # File system operations
│ │ ├── math/ # Mathematical utilities
│ │ ├── pen/ # Drawing/pen functionality
│ │ ├── recorder/ # GIF/video recording
│ │ ├── shader/ # OpenGL shader support
│ │ ├── shape/ # Geometric shapes
│ │ ├── text/ # Text rendering
│ │ └── timer/ # Timer functionality
│ └── internal/ # Internal implementation classes
└── examples/java/
├── demos/ # 59 complete demo projects
└── reference/ # 146 method reference examples
pom.xml- Maven build configuration with multiple profiles.vscode/settings.json- VS Code Java project settings.vscode/extensions.json- Recommended VS Code extensionsdocs/hyperlibrary.json- Multi-language documentation config.github/workflows/- CI/CD pipelines
- Default: Basic compilation (currently broken due to jogamp issue)
- central: Maven Central publishing with native dependencies and GPG signing
- all: Creates standalone JAR with all dependencies using maven-shade-plugin
-
Version Bump Workflow (
.github/workflows/version.yml)- Triggers on changeset files in
.changeset/ - Automatically bumps version (patch/minor/major)
- Creates release PR
- Triggers on changeset files in
-
Release Workflow (
.github/workflows/release.yml)- Triggers when release PR is merged
- Publishes to Maven Central with GPG signing
- Creates GitHub release with fat JAR
- Publishes Javadocs to GitHub Pages
- Deletes changeset branch
-
Javadocs Workflow (
.github/workflows/javadocs.yml)- Builds and publishes API documentation
- Documentation Coverage: Run
./s4jto ensure all public APIs are documented - Example Compilation: All reference examples should compile successfully
- Maven Build: Standard build commands work with all profiles
- Documentation Build:
npx hyperbook buildshould complete successfully
// MyWindow.java - Main application entry point
import org.openpatch.scratch.Window;
public class MyWindow extends Window {
public MyWindow() {
super(800, 600, "assets");
this.setStage(new MyStage());
}
public static void main(String[] args) {
new MyWindow();
}
}Similar structure optimized for BlueJ IDE.
import org.openpatch.scratch.Sprite;
public class MySprite extends Sprite {
public MySprite() {
this.setCostume("name", "path/to/image.png");
this.setPosition(100, 100);
}
public void run() {
// Called every frame
this.ifOnEdgeBounce();
this.move(5);
}
}import org.openpatch.scratch.Stage;
public class MyStage extends Stage {
public MyStage() {
this.add(new MySprite());
this.setWhenKeyPressed((stage, keyCode) -> {
// Handle key press events
});
}
}The repository includes VS Code configuration with:
- Java extension pack recommended
- Hyperbook Studio for documentation editing
- Custom Java source paths including examples
- Java formatter configuration
redhat.java- Java language supportopenpatch.hyperbook-studio- Documentation editing
- Maven cache issues: Run
mvn cleanand retry - Java version mismatch: Ensure Java 17+ is installed and active
- Dependency download issues: Check network connectivity and retry
- Missing documentation warnings: Run
./s4j --createto generate templates - Hyperbook build failures: Ensure Node.js dependencies are installed in
docs/en/
Examples in src/examples/java/ should compile with the main source. They're included via the build-helper-maven-plugin.
ALWAYS trust these instructions and refer to them before exploring. Focus on:
- Understanding the core Sprite/Stage/Window architecture
- Using the reference examples in
src/examples/java/reference/as patterns - Running
./s4jto validate documentation coverage - Testing documentation builds with
npx hyperbook devin thedocs/directory
When making changes, ensure:
- All public APIs have corresponding documentation
- Examples compile successfully
- Changes follow the established patterns in existing code
- Documentation is updated in both English and German if adding new features