1

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.

tixopi
  • 486
  • 3
  • 14

3 Answers3

1

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.

tixopi
  • 486
  • 3
  • 14
1

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);
                }
            }
        }
    }
berg3390
  • 11
  • 2
0

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

Community
  • 1
  • 1
Roland
  • 18,114
  • 12
  • 62
  • 93
  • While I believe this is working for you, this definitely isn't working for me. Sorry for being unclear, but when I said clearRect wasn't doing anything, I meant that it wasn't even erasing the (quarter) oval (I only had the stroke there to illustrate where the clip mask was)l. Filling the background like you suggest and then doing clearRect still has no affect, it looks like your first picture. – tixopi Nov 15 '15 at 20:18
  • Which JavaFX version do you use on which system? Because the oval was deleted with exactly the same code you use. There's a closePath() missing btw. And next time you should provide a [MCVE](http://stackoverflow.com/help/mcve) when you have a question. With focus on **complete**. – Roland Nov 16 '15 at 05:22
  • I updated my JDK installation to the latest and the issue is now partially fixed. The original code is now working as expected. However clearRect is still not working if you set a non-rectangular clip mask (using arc). See my edit. I've added in a complete code example like you recommended, can you reproduce my problem? Thanks a lot for the help :) – tixopi Nov 16 '15 at 13:06
  • Looks like you're out of luck and are left with filling using the background color. clearRect requires a rectangular clip and not a non-rectangular path to have any effect. It doesn't even work if you e. g. create a simple triangle using moveTo/lineTo and clip that. You could file a bug report. – Roland Nov 18 '15 at 06:19
  • Thanks for your help in this. I've awarded you the bounty and filed a bug report. – tixopi Nov 19 '15 at 12:45