I've got a JavaFX Canvas on which the user can draw (somewhat like MS Paint).
Now I'm trying to add the option to draw with a fixed level of opacity, meaning that no matter how often the same area is drawn on, the transparency of that area should remain the same. Right now it gets more opaque every time a line intersects itself as you would expect.
I've tried different blending modes and effects but I can't get it to work.
Is there a way to disable aplha blending altogether?
edit:
import java.io.IOException;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.StrokeLineCap;
import javafx.stage.Stage;
public class DrawingTest extends Application{
public static void main(String[] args) throws IOException{
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
Pane root = new Pane();
Scene scene = new Scene(root, 1000, 1000);
Canvas canvas = new Canvas(1000,1000);
GraphicsContext gc = canvas.getGraphicsContext2D();
root.getChildren().add(canvas);
canvas.setOnMousePressed(event -> {
gc.setStroke(Color.rgb(0,0,0,0.2));
gc.setLineCap(StrokeLineCap.ROUND);
gc.setLineWidth(10);
gc.beginPath();
gc.moveTo(event.getX(), event.getY());
gc.stroke();
});
canvas.setOnMouseDragged(event -> {
gc.lineTo(event.getX(), event.getY());
gc.stroke();
gc.closePath();
gc.beginPath();
gc.moveTo(event.getX(), event.getY());
});
primaryStage.setScene(scene);
primaryStage.show();
}
}