Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
turnef opened this issue Jan 30, 2019 · 1 comment

Comments

@turnef
Copy link

turnef commented Jan 30, 2019

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

jfree added a commit that referenced this issue Jan 30, 2019
@jfree
Copy link
Owner

jfree commented Jan 30, 2019

Thanks for the report. I fixed the issue in the same way that you suggested, but I made use of the existing 'recyclable' line and rect objects that are used elsewhere in the code (this should minimise garbage generation in cases where many shapes are drawn).

@jfree jfree closed this as completed Jan 30, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants