Skip to content

andrestubbe/FastGraphics

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

FastGraphics — High-Performance GPU-Accelerated Graphics2D (600% Faster than Java2D) [ALPHA]

🎨 MAJOR UPDATE - Alpha Transparency & Rounded Rectangles now fully implemented! See TODO.md for remaining features.

⚡ Ultra-fast GPU-accelerated Graphics2D replacement for Java — 600% faster than java.awt.Graphics2D / Java2D

Build Java Platform License: MIT

// Quick Start — Ultra-fast 2D rendering
FastGraphics2D g = new FastGraphics2D(hwnd);
g.setColor(Color.RED);
g.fillRect(10, 10, 100, 50);
g.setColor(Color.BLUE);
g.fillOval(200, 100, 30, 30);
g.present();  // 1 Draw Call für alles!

// NEW: Alpha transparency support!
g.setColor(new Color(255, 0, 0, 128)); // 50% transparent red
g.fillOval(100, 100, 200, 200);

// NEW: Rounded rectangles!
g.setColor(new Color(0, 200, 255));
g.fillRoundRect(300, 200, 150, 100, 20, 20);

FastGraphics is a high-performance GPU-accelerated 2D rendering library that replaces java.awt.Graphics2D with a native DirectX 11 backend. Built for real-time games, data visualization, scientific applications, and high-frequency UI rendering where Java2D performance bottlenecks.

Keywords: java graphics2d alternative, gpu accelerated 2d rendering, directx java rendering, fast fillRect, java game engine 2d, hardware accelerated graphics, batch rendering java, instanced rendering 2d, 600fps java graphics

If you need thousands of shapes at 60fps+, batch rendering, or GPU-accelerated 2D, FastGraphics delivers native-level DirectX 11 performance with Java simplicity.


Table of Contents


Why FastGraphics?

java.awt.Graphics2D is convenient but slow. Its immediate-mode API creates CPU bottlenecks, and Java2D's software rasterizer limits performance to ~100-200 simple shapes per frame.

FastGraphics solves this with:

  • Batch Rendering — hundreds/thousands of shapes in a single GPU draw call
  • Instanced Rendering — 76% less data transfer than vertex expansion
  • DirectX 11 backend — native GPU performance, zero Java2D overhead
  • Zero GC pressure — direct ByteBuffers, pooled resources
  • Drop-in API — familiar Graphics2D-style methods

Performance Benchmarks

Test Java2D (AWT) FastGraphics (DX11) Speedup
1,000 Rectangles ~120 FPS 5,335 FPS 44×
5,000 Rectangles ~60 FPS 6,056 FPS 100×
10,000 Rectangles ~40 FPS 4,585 FPS 114×
50,000 Rectangles ~5 FPS 1,060 FPS 212×
Batch Overhead High Minimal 600%+

Measured on Windows 11, RTX 3070, Java 17, 360Hz display. Tests use fillRect() with varying counts.


FastGraphics vs java.awt.Graphics2D

Feature java.awt.Graphics2D FastGraphics
Rendering Backend Java2D (CPU) DirectX 11 (GPU)
Max Shapes @ 60fps ~1,000 50,000+
Batch Rendering ❌ No ✅ Yes (Automatic)
Instanced Rendering ❌ No ✅ Yes (76% less bandwidth)
Memory Pressure High (GC) Zero (Direct Buffers)
Cross-Platform ✅ All platforms Windows (DX11)

Quick Start

import fastgraphics.FastGraphics2D;
import javax.swing.JFrame;
import java.awt.Color;

public class QuickStart {
    public static void main(String[] args) {
        // Create window
        JFrame frame = new JFrame("FastGraphics Demo");
        frame.setSize(800, 600);
        frame.setVisible(true);
        
        // Get native window handle
        long hwnd = FastGraphics2D.findWindow("FastGraphics Demo");
        
        // Create FastGraphics context
        FastGraphics2D g = new FastGraphics2D(hwnd);
        
        // Render loop
        while (frame.isVisible()) {
            g.setColor(Color.BLACK);
            g.clear();
            
            g.setColor(Color.RED);
            g.fillRect(10, 10, 100, 50);
            
            g.setColor(Color.BLUE);
            g.fillRect(200, 100, 80, 80);
            
            g.present();  // Single GPU draw call!
        }
    }
}

TV Test Pattern Demo

FastGraphics includes a classic 80s TV Test Pattern demo for visual validation:

The test renders identical patterns in both FastGraphics and Java2D for pixel-perfect comparison. This validates:

  • ✅ Color accuracy (Color Bars)
  • ✅ Geometric precision (Convergence Circles)
  • ✅ Gradient rendering (Shade Bars)
  • ✅ Text rendering (Station ID)

Run the test:

java -cp out demo.Comparator

API Reference

Core Methods

Method Description Status
new FastGraphics2D(hwnd) Create rendering context ✅ Implemented
setColor(Color c) Set current drawing color ✅ Implemented
fillRect(x, y, w, h) Fill rectangle (batched) ✅ Implemented
fillOval(x, y, w, h) Fill oval/circle ✅ Implemented
drawRect(x, y, w, h) Draw rectangle outline ✅ Implemented
drawOval(x, y, w, h) Draw oval/circle outline ✅ Implemented
drawLine(x1, y1, x2, y2) Draw line ✅ Implemented
drawPolygon(xPoints, yPoints) Draw polygon outline ✅ Implemented
fillPolygon(xPoints, yPoints) Fill polygon (convex) ✅ Implemented
drawArc(x, y, w, h, startAngle, arcAngle) Draw arc ✅ Implemented
fillArc(x, y, w, h, startAngle, arcAngle) Fill arc ✅ Implemented
drawRoundRect(x, y, w, h, arcWidth, arcHeight) Draw rounded rectangle ✅ Implemented
fillRoundRect(x, y, w, h, arcWidth, arcHeight) Fill rounded rectangle ✅ Implemented
translate(tx, ty) Translate coordinate system ✅ Implemented
scale(sx, sy) Scale coordinate system ✅ Implemented
rotate(angle) Rotate coordinate system ✅ Implemented
resetTransform() Reset transformations ✅ Implemented
setStroke(float lineWidth) Set line width for drawing ✅ Implemented
setRenderingHint(key, value) Set rendering hint ⚠️ Stub (stores only)
getRenderingHint(key) Get rendering hint ✅ Implemented
setClip(x, y, w, h) Set clipping rectangle ✅ Implemented (Scissor Rects)
resetClip() Reset clipping ✅ Implemented
drawString(str, x, y) Draw text string ⚠️ Stub (no effect)
drawImage(img, x, y, w, h) Draw image (GPU-accelerated with caching) ✅ Implemented
clear() / clear(Color c) Clear background ✅ Implemented
present() Display rendered frame ✅ Implemented

Known Limitations

  • AntiAliasing: RenderingHints.KEY_ANTIALIASING is supported via API, but DirectX 11 MSAA requires Swap Chain configuration (not runtime-switchable)
  • Clipping: ✅ Fully implemented via DirectX 11 Scissor Rects
  • Line Width: ✅ Now implemented! Use setStroke(width) for thick lines
  • Text Rendering: drawString() is a stub (requires textured shaders and font rendering)
  • Image Rendering: drawImage() is fully implemented with GPU texture caching
  • Alpha Transparency: Now fully supported! Use new Color(r, g, b, alpha) for transparent shapes

State Management

FastGraphics automatically batches operations. No manual beginBatch() / endBatch() needed!

g.setColor(Color.RED);
g.fillRect(0, 0, 50, 50);   // Queued

// No color change = same batch
g.fillRect(60, 0, 50, 50);  // Same batch

// Color change = auto-flush, new batch
g.setColor(Color.BLUE);
g.fillRect(120, 0, 50, 50); // New batch

g.present();  // All batches rendered in 1-2 draw calls

Examples

All runnable demos are in examples/ folder:

cd examples/00-basic-usage && mvn compile exec:java        # Main demo
cd examples/10-clipping && mvn compile exec:java           # Clipping tests  
cd examples/20-benchmark && mvn compile exec:java          # Performance test
cd examples/30-shapes && mvn compile exec:java             # Shape rendering
cd examples/40-advanced && mvn compile exec:java           # Transforms, transparency
cd examples/50-image && mvn compile exec:java              # Image rendering
cd examples/60-comparison && mvn compile exec:java         # FastGraphics vs Java2D
cd examples/70-simple && mvn compile exec:java             # Simple tests

Build from Source

Prerequisites

  • Windows 10/11
  • Java JDK 17+
  • Visual Studio 2022 (C++ workload)

Quick Start (Maven)

<dependency>
    <groupId>com.github.andrestubbe</groupId>
    <artifactId>FastGraphics</artifactId>
    <version>v1.0.0-beta</version>
</dependency>

Or download JAR: https://jitpack.io/com/github/andrestubbe/FastGraphics/v1.0.0-beta/FastGraphics-v1.0.0-beta.jar

Build from Source

# Build library
mvn clean package

# Run examples
cd examples/00-basic-usage
mvn compile exec:java

Build Native DLL (Windows)

# Requires Visual Studio 2022 with C++ workload
cl /O2 /EHsc /LD /Fe:out\FastGraphics.dll native\FastGraphics.cpp `
    /I"%JAVA_HOME%\include" /I"%JAVA_HOME%\include\win32" `
    d3d11.lib d3dcompiler.lib user32.lib

Platform Support

Platform Status Notes
Windows 10/11 ✅ Full Support DirectX 11 native
Linux ❌ Not Supported Would need OpenGL/Vulkan port
macOS ❌ Not Supported Would need Metal port

License

MIT License — Free for commercial and personal use.

See LICENSE for details.


Built with ❤️ — Making Java fast again!

About

GPU-accelerated Graphics2D replacement for Java. 600% faster than Java2D. DirectX-powered rendering.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors