A JavaFX library that allows Java2D code (Graphics2D) to be used to draw to a Canvas node.

Overview

FXGraphics2D

Version 2.1, 3 October 2020.

Maven Central

Overview

FXGraphics2D is a free implementation of Java's Graphics2D API that targets the JavaFX Canvas. It makes it possible to reuse existing Java2D code in a JavaFX application.

FXGraphics2D sample

The code has been developed for the use of Orson Charts (http://www.object-refinery.com/orsoncharts/) and JFreeChart (http://www.jfree.org/jfreechart). Numerous demos can be found in the JFree-FXDemos project page at GitHub.

You can also read about FXGraphics2D in this Java Magazine article:

http://www.oraclejavamagazine-digital.com/javamagazine/november_december_2014#pg63

Temporary: link above is broken, please use this in the meantime:

http://www.jfree.org/fxgraphics2d/javamagazine20141112-dl.pdf

The home page for the project is:

http://www.jfree.org/fxgraphics2d/

FXGraphics2D requires JDK 11 or later and is licensed under the terms of a (three clause) BSD-style license.

Include

To include FXGraphics2D in your own project, add the following Maven dependency:

    <dependency>
        <groupId>org.jfree</groupId>
        <artifactId>org.jfree.fxgraphics2d</artifactId>
        <version>2.1</version>
    </dependency>

Build

You can build FXGraphics2D from sources using Maven:

mvn clean install

License

FXGraphics2D is licensed under a BSD-style license:

Copyright (c) 2014-2020, Object Refinery Limited.

All rights reserved.

Redistribution and use in source and binary forms, with or without modification, 
are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, 
this list of conditions and the following disclaimer in the documentation 
and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its contributors 
may be used to endorse or promote products derived from this software without 
specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Implementation Notes

(1) Initial versions of the JavaFX Canvas did not have support for dashed lines. We submitted this feature request:

https://bugs.openjdk.java.net/browse/JDK-8097787

The feature was added in Java 1.8.0_40.

(2) In performance testing we have noted that path rendering is not as fast as it could be. This is confirmed by this bug report:

https://bugs.openjdk.java.net/browse/JDK-8092373

Feedback

If you have feedback about the FXGraphics2D library, please visit the forum at http://www.jfree.org/fxgraphics2d.

History

3-Oct-2020 : Version 2.1
  • update JavaFX dependencies to version 15;
  • migrated tests to JUnit 5.
27-Feb-2020 : Version 2.0
  • converted to a module (org.jfree.fxgraphics2d);
  • now requires JDK 11 or later.
7-Feb-2019 : Version 1.8
  • fixed bug #8 where draw(Shape) alters the shape state for RenderingHints.VALUE_STROKE_PURE;
  • added automatic module name (org.jfree.fxgraphics2d).
5-Jul-2018 : Version 1.7
  • fixed bug #6 where color, stroke and font attributes are changed after calling setClip() twice.
5-Nov-2017 : Version 1.6
  • added support for JDK9 by removing the use of some non-public classes;
  • simplified the project set-up by removing Ant as a build option, and removing the demos (these will be provided in a separate Git project https://github.com/jfree/jfree-fxdemos).
15-Oct-2016 : Version 1.5
  • add rendering hint to provide option for measuring fonts via JavaFX API;
  • fix exception in drawImage() method with null transform;
  • updated JFreeSVG to version 3.2 (affects demo only).
27-Apr-2016 : Version 1.4
  • improve handling of setComposite() for alpha values;
  • fixed bug in rotate method, incorrect conversion to degrees.
28-Jan-2016 : Version 1.3
  • fixed bug in drawImage() method;
  • add 'maven' directory with a pom that can be copied over to load the project in NetBeans;
  • updated Ant script to copy over pom.xml to distribution.
23-Jan-2016 : Version 1.2.1
  • implemented getGraphicsDevice() method;
  • add support for dashed lines;
  • in setPaint() and setStroke(), don't update anything if the value is the same;
  • various performance fixes;
  • fixed bug in drawImage() method;
  • updated demos.

Note that version 1.2.1 corrects a Maven dependency problem that sneaked into the 1.2 release.

30-Jul-2014 : Version 1.1
  • fix clipping so that combined plots in JFreeChart work correctly;
  • observe KEY_STROKE_CONTROL rendering hint for lines and rectangles (used to sharpen gridlines and borders in JFreeChart);
  • adjust fontRenderContext so that glyph positioning is more precise for TextLayout;
  • add clearRect() call in the JFreeChart demo;
  • add Maven support (pom.xml).
19-May-2014 : Version 1.0
  • initial public release.
Comments
  • Very nice! What about the other way around?

    Very nice! What about the other way around?

    I would like to be able to draw javafx nodes to a Graphics2D so I can draw these in frameworks that use Graphics2D. Sofar I do not have a solution, perhaps overriding JFXPanel#paintComponent is a good starting point.

    opened by eduarddrenth 3
  • setPaint() inefficiency

    setPaint() inefficiency

    Copied this report which was entered against the wrong repo:

    "I'm using javafx from JFreeChart. I notice that plotting 2M points took 4m while 4M points took 83m. That is due to JFreeChart's drawFirstPassShape and drawSecondaryPass use of setPaint() for each item. setPaint calls writePaint which adds to a GrowableDataBuffer which expands incrementally. That results in n^2 performance. Maybe javafx providing a better hint or allowing clients to provide a hint... I hacked a hint into javafx's Canvas.getBuffer. That reduced the time to 30s.

    Cross posted to javafx. I expect it is more of a javafx issue, but wanted to provide a heads up...

    jfreechart 1.0.19 javafx 1.8.0_45

    Duane Tiemann"

    opened by jfree 3
  • setClip incorrectly restores prior font, paint, color, and stroke

    setClip incorrectly restores prior font, paint, color, and stroke

    The setClip() method, when invoked after font, paint, color or stroke details are altered, incorrectly restores the prior state. The following code demonstrates the issue:

    import java.awt.BasicStroke;
    import java.awt.Color;
    import java.awt.Rectangle;
    
    import org.jfree.fx.FXGraphics2D;
    
    import javafx.application.Application;
    import javafx.scene.Group;
    import javafx.scene.Scene;
    import javafx.scene.canvas.Canvas;
    import javafx.scene.canvas.GraphicsContext;
    import javafx.stage.Stage;
    
    /**
     * Simple code example demonstrating the issue with setClip()
     */
    public class SetClipIssue extends Application {
    
    	public static void main(String[] args) {
    		launch(args);
    	}
    
    	@Override
    	public void start(Stage primaryStage) throws Exception {
    		primaryStage.setTitle("SetClipIssue");
    		Group root = new Group();
    		Canvas canvas = new Canvas(640, 480);
    		GraphicsContext gc = canvas.getGraphicsContext2D();
    
    		FXGraphics2D g2d = new FXGraphics2D(gc);
    		g2d.setFont(g2d.getFont().deriveFont(20f));
    		g2d.setColor(Color.RED);
    		g2d.setStroke(new BasicStroke(2.0f));
    		g2d.setClip(new Rectangle(50, 50, 540, 380));
    		g2d.drawString("Red 20pt", 100, 100);
    		g2d.draw(new Rectangle(55, 75, 25, 25));
    
    		g2d.setFont(g2d.getFont().deriveFont(30f));
    		g2d.setColor(Color.BLUE);
    		g2d.setStroke(new BasicStroke(1.0f));
    
    		/*
    		 * This clip set incorrectly restores the previous state of color, font, and
    		 * stroke.
    		 */
    		g2d.setClip(new Rectangle(49, 49, 542, 382));
    		g2d.drawString("Blue 30pt?", 100, 200);
    		g2d.draw(new Rectangle(55, 175, 25, 25));
    
    		root.getChildren().add(canvas);
    		primaryStage.setScene(new Scene(root));
    		primaryStage.show();
    	}
    
    }
    

    This patch resolved the issue in both the sample code above and in our application where we originally discovered the problem:

    fxgraphics2d_patch.txt

    opened by turnef 2
  • Avoid duplicating buffered image

    Avoid duplicating buffered image

    Simple commit to perform instance check to avoid duplicating buffered image. Most of the calls to #drawImage are probably going to use BufferedImage, and we can avoid unnecessary copies.

    opened by trajar 2
  • draw(Shape) method may alter the state of input Line2D and Rectangle2D shapes

    draw(Shape) method may alter the state of input Line2D and Rectangle2D shapes

    The draw(Shape) method, when the RenderingHints.KEY_STROKE_CONTROL is set to something other than RenderingHints.VALUE_STROKE_PURE, alters the state of input Rectangle2D and Line2D shapes. The code below demonstrates this:

    import java.awt.RenderingHints;
    import java.awt.geom.Line2D;
    import java.awt.geom.Rectangle2D;
    
    import org.jfree.fx.FXGraphics2D;
    
    import javafx.application.Application;
    import javafx.scene.Group;
    import javafx.scene.Scene;
    import javafx.scene.canvas.Canvas;
    import javafx.scene.canvas.GraphicsContext;
    import javafx.stage.Stage;
    
    public class DrawNormalize extends Application {
    
    	public static void main(String[] args) {
    		launch(args);
    	}
    
    	@Override
    	public void start(Stage primaryStage) throws Exception {
    		primaryStage.setTitle("DrawStrokeNormalize");
    		Group root = new Group();
    		Canvas canvas = new Canvas(640, 480);
    		GraphicsContext gc = canvas.getGraphicsContext2D();
    
    		FXGraphics2D g2d = new FXGraphics2D(gc);
    
    		/*
    		 * Set the stroke control rendering hint to something other than
    		 * VALUE_STROKE_PURE to trigger the issue.
    		 */
    		g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, 
    			RenderingHints.VALUE_STROKE_NORMALIZE);
    
    		Line2D.Double line = new Line2D.Double(10.15, 10.15, 470.92, 470.92);
    		printLine(line);
    		g2d.draw(line);
    		printLine(line); // line's state has changed
    
    		Rectangle2D.Double rect = new Rectangle2D.Double(20.57, 20.57, 590.01, 430.3);
    		printRect(rect);
    		g2d.draw(rect);
    		printRect(rect); // rect's state has changed
    
    		root.getChildren().add(canvas);
    		primaryStage.setScene(new Scene(root));
    		primaryStage.show();
    	}
    
    	static void printLine(Line2D line) {
    		System.out.println(String.format("(%5.3f, %5.3f) -> (%5.3f, %5.3f)", 
    				line.getX1(), line.getY1(), line.getX2(), line.getY2()));
    	}
    
    	static void printRect(Rectangle2D rect) {
    		System.out.println(String.format("(%5.3f, %5.3f) -> (%5.3f, %5.3f)", 
    				rect.getMinX(), rect.getMinY(),
    				rect.getMaxX(), rect.getMaxY()));
    	}
    
    }
    

    It's not clear that this is exactly a problem that needs to be addressed, but we found the behavior surprising. It was straightforward enough to work around in our code, but I've attached a patch for your consideration that I believe corrects the issue while preserving the original intent of the source:

    fxgraphics2d.patch.txt

    opened by turnef 1
  • Binary download?

    Binary download?

    The latest version is currently 1.7, but the home page (http://www.jfree.org/fxgraphics2d/index.html) only has a link to download version 1.5. And on github there's only the source code.

    opened by aditsu 1
  • Demo classes no longer runnable since not included in any jar files

    Demo classes no longer runnable since not included in any jar files

    Directly on the README.md we have instructions to run demo program:

    java -cp fxgraphics2d-1.5-demo.jar org.jfree.fx.demo.FXGraphics2DDemo1

    The class does exist but is not included in any jar file by mvn package.

    opened by javadba 1
  • Incorrect range adding single series to empty XYSeriesCollection in XYPlot in CombinedDomainXYPlot

    Incorrect range adding single series to empty XYSeriesCollection in XYPlot in CombinedDomainXYPlot

    This doesn't happen if the XYPlot is added to the CombinedDomainXYPlot after it contains a series.

    Adding a second series results in a correct image.

    Duane Tiemann jfreechart-1.0.19.jar as distributed Code provided in OneSeriesProblem.txt because .java is not allowed.

    OneSeriesProblem.txt singleseries twoseries

    Code provided in OneSeriesProblem.txt since .java is not allowed.

    opened by DuaneTiemann 1
Releases(v2.1.3)
  • v2.1.3(Jan 23, 2022)

  • v2.1.2(Aug 5, 2021)

    Updates in this release:

    • set winding rule for Path2D instances;
    • set cycle method on gradient paints;
    • fix focus distance on RadialGradientPaint.
    Source code(tar.gz)
    Source code(zip)
  • v2.1.1(May 24, 2021)

    Updates in this release:

    • fix handling for null arguments in drawImage() methods;
    • update JavaFX dependency to version 16;
    • update JUnit test dependency to 5.7.2.
    Source code(tar.gz)
    Source code(zip)
  • v2.1(May 24, 2021)

  • v2.0(Sep 27, 2020)

  • v1.8(Feb 8, 2019)

    A maintenance release fixing bug #8 where drawn shapes can have their state altered. In addition, an automatic module name (org.jfree.fxgraphics2d) has been added to the jar.

    Source code(tar.gz)
    Source code(zip)
  • v1.7(Jul 5, 2018)

  • v1.6(Nov 5, 2017)

    • added support for JDK9 by removing the use of some non-public classes;
    • simplified the project set-up by removing Ant as a build option, and removing the demos (these will be provided in a separate Git project https://github.com/jfree/jfree-fxdemos).
    Source code(tar.gz)
    Source code(zip)
Owner
David Gilbert
Java client side developer, creator of JFreeChart, Orson Charts (also for Android), JFreeSVG and FXGraphics2D.
David Gilbert
A JavaFX library containing tiles that can be used for dashboards.

TilesFX A JavaFX library containing tiles for Dashboards. Donations are welcome at Paypal Intro The Tile is a simple JavaFX Control that comes with di

Gerrit Grunwald 1.3k Dec 30, 2022
A small tools to play with JavaFX Color.derive() function - allows to create custom colors and to save those in color palettes.

DeriveColorsFX This is not a serious application. Its a small tool where I just played with the method Color::deriveColor provided by JavaFX. Also its

Oliver Löffler 11 Oct 9, 2022
Tray Icon implementation for JavaFX applications. Say goodbye to using AWT's SystemTray icon, instead use a JavaFX Tray Icon.

FXTrayIcon Library intended for use in JavaFX applications that makes adding a System Tray icon easier. The FXTrayIcon class handles all the messy AWT

Dustin Redmond 248 Dec 30, 2022
Lib-Tile is a multi Maven project written in JavaFX and NetBeans IDE 8 and provides the functionalities to use and handle easily Tiles in your JavaFX application.

Lib-Tile Intention Lib-Tile is a multi Maven project written in JavaFX and NetBeans IDE and provides the functionalities to use and handle easily Tile

Peter Rogge 13 Apr 13, 2022
DataFX - is a JavaFX frameworks that provides additional features to create MVC based applications in JavaFX by providing routing and a context for CDI.

What you’ve stumbled upon here is a project that intends to make retrieving, massaging, populating, viewing, and editing data in JavaFX UI controls ea

Guigarage 110 Dec 29, 2022
A JavaFX Application mimicking the Matrix green code falling/raining effect.

Matrix Effect A JavaFX application that mimics the falling/raining green code effect. This project has been generated from Gluon at https://start.gluo

Carl Dea 10 Oct 1, 2022
An Android library that allows you to easily create applications with slide-in menus.

An Android library that allows you to easily create applications with slide-in menus. You may use it in your Android apps provided that you cite this project and include the license in your app. Thanks!

Jeremy Feinstein 11.1k Jan 4, 2023
A library of +70 ready-to-use animations for JavaFX

AnimateFX A library of ready-to-use animations for JavaFX Features: Custom animations Custom interpolators Play/Stop animation Play an animation after

Loïc Sculier 366 Jan 5, 2023
A library for JavaFX that gives you the ability to show progress on the Windows taskbar.

A clean and easy way to implement this amazing native Windows taskbar-progressbar functionality in javaFX Background Since Windows 7 there is a taskba

Daniel Gyoerffy 77 Nov 28, 2022
A JavaFX 3D Visualization and Component Library

FXyz3D FXyz3D Core: FXyz3D Client: FXyz3D Importers: A JavaFX 3D Visualization and Component Library How to build The project is managed by gradle. To

null 16 Aug 23, 2020
A library for creating and editing graph-like diagrams in JavaFX.

Graph Editor A library for creating and editing graph-like diagrams in JavaFX. This project is a fork of tesis-dynaware/graph-editor 1.3.1, which is n

Steffen 125 Jan 1, 2023
Provides a Java API to use the JavaScript library d3.js with the JavaFx WebView

javafx-d3 Provides a Java API for using the JavaScript library d3.js with JavaFx Applications. Many thanks to the authors of the projects gwt-d3 [1] a

null 98 Dec 19, 2022
Kubed - A port of the popular Javascript library D3.js to Kotlin/JavaFX.

Kubed: A Kotlin DSL for data visualization Kubed is a data visualization DSL embedded within the Kotlin programming language. Kubed facilitates the cr

Brian Hudson 71 Dec 28, 2022
A 3D chart library for Java applications (JavaFX, Swing or server-side).

Orson Charts (C)opyright 2013-2020, by Object Refinery Limited. All rights reserved. Version 2.0, 15 March 2020. Overview Orson Charts is a 3D chart l

David Gilbert 96 Sep 27, 2022
Flow Visualization Library for JavaFX and VRL-Studio

VWorkflows Interactive flow/graph visualization for building domain specific visual programming environments. Provides UI bindings for JavaFX. See htt

Michael Hoffer 274 Dec 29, 2022
A Javafx Library for building MVC Applications.

A JavaFx Library For Making MVC Type Desktop Applications Installation requires Java jdk > 7 for windows requres openjdk-7 or 8 and openjfx for linux

Obi Uchenna David 38 Apr 30, 2022
RXControls is a JavaFX custom component library.

RXControls RXControls Version 8.x.y need javafx8 RXControls Version 11.x.y need javafx11+ 一个javafx的自定义组件库, 密码可见组件, 轮播图组件, 动态按钮组件等, 音频频谱可视化组件,歌词组件 等...

null 164 Jan 1, 2023
A JavaFX library that contains different kind of charts

Charts A library for scientific charts in JavaFX. This is still a work in development, but here are some of the charts being worked on so far. The cha

Gerrit Grunwald 497 Jan 2, 2023