Water
Water

Reputation: 3655

JavaFX slow performance on canvas when drawing freestyle with pen

I was trying out the code here for making a JavaFX app which allows my stylus pen to draw on a canvas: Canvas does not draw smooth lines

The performance is extremely painful when trying to draw on a canvas. It will freeze for half a second, and then start drawing. Only after it starts drawing is it fine. Beforehand though when you first press down with the mouse/pen, the delay is pretty brutal and makes it unusable.

Here is the code I used:

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.effect.BoxBlur;
import javafx.scene.shape.StrokeLineCap;
import javafx.scene.shape.StrokeLineJoin;
import javafx.stage.Stage;

public class Test extends Application {

    private GraphicsContext gc;

    @Override
    public void start(Stage stage) {
        Canvas canvas = new Canvas(500, 500);
        canvas.setOnMouseDragged(e -> {
            gc.lineTo(e.getX(), e.getY());
            gc.stroke();
        });
        canvas.setOnMousePressed(e -> gc.moveTo(e.getX(), e.getY()));

        gc = canvas.getGraphicsContext2D();
        gc.setLineCap(StrokeLineCap.ROUND);
        gc.setLineJoin(StrokeLineJoin.ROUND);
        gc.setLineWidth(1);

        BoxBlur blur = new BoxBlur();
        blur.setWidth(1);
        blur.setHeight(1);
        blur.setIterations(1);
        gc.setEffect(blur);

        Group root = new Group(canvas);
        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.show();
        stage.setFullScreen(true);
    }

    public static void main(String[] args) {
        launch(args);
    }
}

Is there any way to fix the performance issue? My desktop computer is pretty solid and can run high-end games, so its not the performance on my computer.

NOTE: I should also say that the delay that occurs when you first press the mouse on the canvas is bad enough to cause mouse clicks to drop.

EDIT: To confirm it wasnt the OnMousePressed, I commented it out and it did not help.

Upvotes: 2

Views: 1131

Answers (1)

Water
Water

Reputation: 3655

Apparently my driver was conflicting somehow with Java, which means JavaFX is fine. After getting help from the tablet company and some fixes, this doesn't happen with the latest drivers.

Upvotes: 1

Related Questions