Coder Social home page Coder Social logo

jfree / fxgraphics2d Goto Github PK

View Code? Open in Web Editor NEW
204.0 21.0 23.0 5.96 MB

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

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

License: Other

Java 100.00%
javafx javafx-library java2d

fxgraphics2d's Issues

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

JFXCentral Badge

Would be great if you could add this badge to your readme:

[![JFXCentral](https://img.shields.io/badge/Find_me_on-JFXCentral-blue?logo=googlechrome&logoColor=white)](https://www.jfx-central.com/libraries/fxgraphics2d)

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

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.

Exception in Graphics2DTester

When running the Graphics2DTester, after some iterations the following exception is triggered:

Elapsed: 150.929
FPS: 3.364
java.lang.ClassCastException: class com.sun.prism.paint.Color cannot be cast to class java.lang.String (com.sun.prism.paint.Color is in module javafx.graphics@20-ea of loader 'app'; java.lang.String is in module java.base of loader 'bootstrap')
        at javafx.graphics@20-ea/com.sun.javafx.sg.prism.NGCanvas.handleRenderOp(NGCanvas.java:1375)
        at javafx.graphics@20-ea/com.sun.javafx.sg.prism.NGCanvas.renderStream(NGCanvas.java:1104)
        at javafx.graphics@20-ea/com.sun.javafx.sg.prism.NGCanvas.renderContent(NGCanvas.java:610)
        at javafx.graphics@20-ea/com.sun.javafx.sg.prism.NGNode.doRender(NGNode.java:2072)
        at javafx.graphics@20-ea/com.sun.javafx.sg.prism.NGNode.render(NGNode.java:1964)
        at javafx.graphics@20-ea/com.sun.javafx.sg.prism.NGGroup.renderContent(NGGroup.java:270)
        at javafx.graphics@20-ea/com.sun.javafx.sg.prism.NGRegion.renderContent(NGRegion.java:579)
        at javafx.graphics@20-ea/com.sun.javafx.sg.prism.NGNode.doRender(NGNode.java:2072)
        at javafx.graphics@20-ea/com.sun.javafx.sg.prism.NGNode.render(NGNode.java:1964)
        at javafx.graphics@20-ea/com.sun.javafx.sg.prism.NGGroup.renderContent(NGGroup.java:270)
        at javafx.graphics@20-ea/com.sun.javafx.sg.prism.NGRegion.renderContent(NGRegion.java:579)
        at javafx.graphics@20-ea/com.sun.javafx.sg.prism.NGNode.renderForClip(NGNode.java:2313)
        at javafx.graphics@20-ea/com.sun.javafx.sg.prism.NGNode.renderRectClip(NGNode.java:2207)
        at javafx.graphics@20-ea/com.sun.javafx.sg.prism.NGNode.renderClip(NGNode.java:2233)
        at javafx.graphics@20-ea/com.sun.javafx.sg.prism.CacheFilter.renderNodeToCache(CacheFilter.java:683)
        at javafx.graphics@20-ea/com.sun.javafx.sg.prism.CacheFilter.render(CacheFilter.java:587)
        at javafx.graphics@20-ea/com.sun.javafx.sg.prism.NGNode.renderCached(NGNode.java:2377)
        at javafx.graphics@20-ea/com.sun.javafx.sg.prism.NGNode.doRender(NGNode.java:2063)
        at javafx.graphics@20-ea/com.sun.javafx.sg.prism.NGNode.render(NGNode.java:1964)
        at javafx.graphics@20-ea/com.sun.javafx.sg.prism.NGGroup.renderContent(NGGroup.java:270)
        at javafx.graphics@20-ea/com.sun.javafx.sg.prism.NGRegion.renderContent(NGRegion.java:579)
        at javafx.graphics@20-ea/com.sun.javafx.sg.prism.NGNode.doRender(NGNode.java:2072)
        at javafx.graphics@20-ea/com.sun.javafx.sg.prism.NGNode.render(NGNode.java:1964)
        at javafx.graphics@20-ea/com.sun.javafx.sg.prism.NGGroup.renderContent(NGGroup.java:270)
        at javafx.graphics@20-ea/com.sun.javafx.sg.prism.NGRegion.renderContent(NGRegion.java:579)
        at javafx.graphics@20-ea/com.sun.javafx.sg.prism.NGNode.doRender(NGNode.java:2072)
        at javafx.graphics@20-ea/com.sun.javafx.sg.prism.NGNode.render(NGNode.java:1964)
        at javafx.graphics@20-ea/com.sun.javafx.tk.quantum.ViewPainter.doPaint(ViewPainter.java:480)
        at javafx.graphics@20-ea/com.sun.javafx.tk.quantum.ViewPainter.paintImpl(ViewPainter.java:321)
        at javafx.graphics@20-ea/com.sun.javafx.tk.quantum.PresentingPainter.run(PresentingPainter.java:92)
        at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:577)
        at java.base/java.util.concurrent.FutureTask.runAndReset(FutureTask.java:358)
        at javafx.graphics@20-ea/com.sun.javafx.tk.RenderJob.run(RenderJob.java:58)
        at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1144)
        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:642)
        at javafx.graphics@20-ea/com.sun.javafx.tk.quantum.QuantumRenderer$PipelineRunnable.run(QuantumRenderer.java:126)
        at java.base/java.lang.Thread.run(Thread.java:1589)

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"

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.