Menu

Graphics and UI Modules

Relevant source files

This document covers the graphics and UI capabilities provided by OwnLang's visual modules, primarily the canvasfx module which integrates JavaFX for 2D graphics rendering, canvas operations, and visual effects. This module enables creation of graphical applications with drawing capabilities, event handling, and sophisticated visual effects.

For network-based UI applications, see Server Module. For basic I/O and system interaction, see Standard Library Modules.

Module Architecture

The graphics and UI system in OwnLang is built around JavaFX integration through the canvasfx module, which provides a comprehensive graphics API accessible from OwnLang scripts.

Sources: modules/canvasfx/build.gradle1-24 modules/canvasfx/src/main/java/com/annimon/ownlang/modules/canvasfx/canvasfx.java1-48 settings.gradle9-14

Module Configuration and Dependencies

The canvasfx module is configured as a separate Gradle subproject with specific JavaFX dependencies and shadow JAR packaging for distribution.

ConfigurationValuePurpose
Module IDcanvasfxModule identifier in OwnLang
Version1.1.0Current module version
JavaFX Version21Required JavaFX runtime
JavaFX Modulesjavafx.controls, javafx.swingCore and Swing bridge components
Build Pluginorg.openjfx.javafxpluginJavaFX Gradle plugin
DistributionShadow JARSelf-contained executable JAR

The module follows the standard OwnLang module interface pattern, implementing both constants() and functions() methods to expose functionality to OwnLang scripts.

Sources: modules/canvasfx/build.gradle1-24 modules/canvasfx/src/main/java/com/annimon/ownlang/modules/canvasfx/canvasfx.java88-195

Core Graphics Components

The canvasfx module provides several key components that work together to enable graphics programming in OwnLang.

Sources: modules/canvasfx/src/main/java/com/annimon/ownlang/modules/canvasfx/canvasfx.java48-195 modules/canvasfx/src/main/java/com/annimon/ownlang/modules/canvasfx/canvasfx.java632-705

Graphics Context API

The GraphicsFXValue class provides a comprehensive wrapper around JavaFX's GraphicsContext, exposing over 60 drawing and rendering methods to OwnLang scripts.

Drawing Operations

CategoryMethodsDescription
Path OperationsbeginPath, closePath, moveTo, lineToPath construction and management
Shape DrawingfillRect, strokeRect, fillOval, strokeOvalBasic shape rendering
Complex ShapesfillPolygon, strokePolygon, fillArc, strokeArcAdvanced geometric shapes
Text RenderingfillText, strokeText, setTextAlign, setTextBaselineText drawing with alignment
Image OperationsdrawImage, createImage, getPixelsImage rendering and manipulation
Transformationstranslate, rotate, scale, transformCoordinate system transformations

State Management

OperationMethodsPurpose
Graphics Statesave, restoreState stack management
Fill/StrokesetFill, setStroke, getFill, getStrokeColor and pattern settings
Line PropertiessetLineWidth, setLineCap, setLineJoinStroke appearance
BlendingsetGlobalBlendMode, setGlobalAlphaCompositing controls
EffectssetEffect, applyEffectVisual effect application

Sources: modules/canvasfx/src/main/java/com/annimon/ownlang/modules/canvasfx/canvasfx.java632-950

Color System

The module provides a comprehensive color system with multiple color space support and predefined color constants.

The color system supports:

  • ARGB Constructor: Direct 32-bit integer color values
  • RGB Components: Separate red, green, blue, alpha values (0.0-1.0)
  • HSB Color Space: Hue, saturation, brightness representation
  • Web Colors: CSS-style color strings (#FF0000, red, etc.)
  • Named Constants: All JavaFX predefined colors (BLACK, WHITE, RED, etc.)

Sources: modules/canvasfx/src/main/java/com/annimon/ownlang/modules/canvasfx/canvasfx.java92-105 modules/canvasfx/src/main/java/com/annimon/ownlang/modules/canvasfx/canvasfx.java246-298

Visual Effects System

The module provides access to JavaFX's comprehensive effects engine through wrapper functions that create EffectValue objects.

Available Effects

Effect TypeConstructor FunctionParameters
Blur EffectsBloomEffect, BoxBlurEffect, GaussianBlurEffect, MotionBlurEffectThreshold, radius, iterations, angle
Shadow EffectsDropShadowEffect, InnerShadowEffect, ShadowEffectBlur type, color, radius, offsets
Color EffectsColorAdjustEffect, ColorInputEffect, SepiaToneEffectHue, saturation, brightness, contrast
Composite EffectsBlendEffect, ReflectionEffectBlend mode, opacity, reflection parameters
Lighting EffectsLightingEffect, GlowEffectLight sources, surface properties, glow level
Transform EffectsPerspectiveTransformEffectCorner point coordinates

Effect Application

Sources: modules/canvasfx/src/main/java/com/annimon/ownlang/modules/canvasfx/canvasfx.java175-190 modules/canvasfx/src/main/java/com/annimon/ownlang/modules/canvasfx/canvasfx.java346-558

Event Handling System

The module provides comprehensive event handling for user interaction through the JavaFX event system.

Event Categories

Event CategoryEvent TypesHandler Registration
Mouse EventsMOUSE_CLICKED, MOUSE_PRESSED, MOUSE_RELEASED, MOUSE_MOVED, MOUSE_DRAGGEDaddEventHandler, addEventFilter
Keyboard EventsKEY_PRESSED, KEY_RELEASED, KEY_TYPEDEvent type constants with KeyCode mapping
Touch EventsSWIPE_UP, SWIPE_DOWN, SWIPE_LEFT, SWIPE_RIGHTGesture recognition support
Drag EventsDRAG_DETECTED, MOUSE_ENTERED_TARGET, MOUSE_EXITED_TARGETDrag and drop operations

Event Constants and Enums

The module exposes several enumeration constants for event handling:

  • Events: Mapped to Events enum with ordinal values
  • MouseButton: PRIMARY, SECONDARY, MIDDLE button constants
  • KeyCode: Complete JavaFX KeyCode enumeration for keyboard handling

Sources: modules/canvasfx/src/main/java/com/annimon/ownlang/modules/canvasfx/canvasfx.java56-86 modules/canvasfx/src/main/java/com/annimon/ownlang/modules/canvasfx/canvasfx.java149-166 modules/canvasfx/src/main/java/com/annimon/ownlang/modules/canvasfx/canvasfx.java191-194

Image Processing

The ImageFXValue class provides image creation, loading, and pixel-level manipulation capabilities.

Image Creation Methods

MethodSignaturePurpose
Load from URLcreateImage(url)Load image from file or web URL
Create BlankcreateImage(width, height)Create empty WritableImage
From Pixel DatacreateImage(width, height, pixels)Create image from ARGB pixel array

Image Properties and Operations

  • Properties: width, height, preserveRatio, smooth (read-only)
  • Pixel Access: getPixels() method returns ARGB integer array
  • Pixel Format: Uses WritablePixelFormat.getIntArgbInstance() for consistent ARGB handling
  • Integration: Seamless use with GraphicsContext.drawImage() for rendering

Sources: modules/canvasfx/src/main/java/com/annimon/ownlang/modules/canvasfx/canvasfx.java601-630 modules/canvasfx/src/main/java/com/annimon/ownlang/modules/canvasfx/canvasfx.java561-599

Window Management

The module integrates with Java Swing through JFXPanel to provide windowing capabilities that bridge Swing and JavaFX.

Core Window Functions

FunctionPurposeImplementation
window(width, height, title)Create main application windowUses JFrame with embedded JFXPanel
repaint()Force canvas redrawTriggers JavaFX Platform update

Threading Model

The module handles JavaFX threading requirements through Platform.runLater() calls, ensuring proper thread safety when integrating with Swing applications and the OwnLang runtime.

Sources: modules/canvasfx/src/main/java/com/annimon/ownlang/modules/canvasfx/canvasfx.java53-55 modules/canvasfx/src/main/java/com/annimon/ownlang/modules/canvasfx/canvasfx.java172-173