tixopi
tixopi

Reputation: 476

JavaFX GraphicsContext clearRect doesn't work with clip mask

The documentation for the clearRect method of GraphicsContext states that it uses the current clip, but this isn't currently working for me. Consider:

GraphicsContext context = canvas.getGraphicsContext2D();
context.beginPath();
context.rect(0,0,100,100); //Set the current path to a rectangle
context.stroke(); //Highlights where the current path is
context.clip();   //Intersect current clip with rectangle
context.fillOval(80, 80, 40, 40); //This correctly draws the oval clipped
context.clearRect(0,0,100,100); //This does nothing at all

The above code sets the clip mask correctly, as evidenced by the fact that fillOval works correctly, however clearRect does nothing (although it works normally without the context.clip()). Why is this?

(Note that I specifically need the clip mask to be working, as later I plan on setting it to specific shapes to erase in non-rectangular shapes.)

-- Edit --

To be clear, clearRect does literally nothing, not even erase the oval. I realise that it won't erase the stroked rectangle but that's not what I'm concerned about.

-- Edit 2 --

Updating to the latest JDK has partially fixed the issue. The above code now works correctly. However, using a non-rectangular clip mask still has the same problem. e.g.

GraphicsContext context = canvas.getGraphicsContext2D();
context.beginPath();
context.arc(50, 50, 40, 40, 0, 360); // Make a circular clip mask
context.closePath();
context.clip();
context.fillRect(0, 0, 200, 200); //Draw a circle clipped correctly, shows clip mask is working
context.clearRect(0, 0, 200, 200); //Does nothing

I realise I could use save and restore to get a rectangular clip mask back, and then clearRect would work. However I want to be able to erase in non-rectangular shapes.

Full code for reproducing this is (created by making a new JavaFX project in eclipse and adding the above lines):

public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            BorderPane root = new BorderPane();
            Scene scene = new Scene(root, 400, 400);
            primaryStage.setScene(scene);
            primaryStage.show();

            Canvas canvas = new Canvas(500, 500);
            root.getChildren().add(canvas);

            GraphicsContext context = canvas.getGraphicsContext2D();
            context.beginPath();
            context.arc(50, 50, 40, 40, 0, 360);
            context.closePath();
            context.clip();
            context.fillRect(0, 0, 200, 200);
            context.clearRect(0, 0, 200, 200);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

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

This should show a blank screen, but the circle is not being cleared.

Upvotes: 1

Views: 2050

Answers (3)

berg3390
berg3390

Reputation: 11

While i agree that this should be fixed, but am facing the fact that it just is not, i have come up with a solution that meets my specific needs at least.

It is a method that clears an area based on an SVGPath using the GraphicsContext.

private void clearPath(GraphicsContext gc, SVGPath path) {
        int xstart = (int) path.getLayoutX();
        int xend = (int) (xstart + path.getLayoutBounds().getMaxX());
        int ystart = (int) path.getLayoutY();
        int yend = (int) (ystart + path.getLayoutBounds().getMaxY());

        PixelWriter pw = gc.getPixelWriter();
        for (int x = xstart; x <= xend; x++) {
            for (int y = ystart; y <= yend; y++) {
                if(path.contains(new Point2D(x, y))) {
                    pw.setColor(x, y, Color.TRANSPARENT);
                }
            }
        }
    }

Upvotes: 1

tixopi
tixopi

Reputation: 476

As per Edit 2, this behaviour seems to be a bug. In summary, clearRect has no effect when a non-rectangular clip is set. (If it isn't working for you even with a rectangular clip, update to the latest JDK.) I've filed a bug report for this.

Upvotes: 1

Roland
Roland

Reputation: 18415

The code works just fine on Windows 7 with JavaFX 8u40.

What you should do is to provide a MCVE. Nobody can possibly guess what else you've done with the GraphicsContext. There may be a save and restore missing and what not. By the way, your code lacks a closePath().


Answer before your edit:

The problem you are facing is the way JavaFX draws the lines. Check out the documentation of the Node class:

At the device pixel level, integer coordinates map onto the corners and cracks between the pixels and the centers of the pixels appear at the midpoints between integer pixel locations. Because all coordinate values are specified with floating point numbers, coordinates can precisely point to these corners (when the floating point values have exact integer values) or to any location on the pixel. For example, a coordinate of (0.5, 0.5) would point to the center of the upper left pixel on the Stage. Similarly, a rectangle at (0, 0) with dimensions of 10 by 10 would span from the upper left corner of the upper left pixel on the Stage to the lower right corner of the 10th pixel on the 10th scanline. The pixel center of the last pixel inside that rectangle would be at the coordinates (9.5, 9.5).

That's why you have a thin line on the right and at the bottom. The lines aren't crisp.

I suggest you move the rectangle to the center and also use the fill method to make this more visible for you.

Your code without clearRect:

enter image description here

Your code with clearRect:

enter image description here

Upvotes: 0

Related Questions