John
John

Reputation: 17

The Graphics2D Fill Method in Java Swing

I've just read that the fill method in Java paints one fewer pixel to the right and bottom.

For example, if you draw a new Rectangle2D.Double(0,0,10,20), then the drawing includes the pixels with x = 10 and y = 20. If you fill the same rectangle, those pixels are not painted.

Why do those methods return different result although they work on the same object and do similar task? Is there any reason, or it's just a mistake?

Upvotes: 1

Views: 376

Answers (1)

Catalina Island
Catalina Island

Reputation: 7126

The Graphics API explains the difference between drawing the outline and filling the interior:

Coordinates are infinitely thin and lie between the pixels of the output device. Operations that draw the outline of a figure operate by traversing an infinitely thin path between pixels with a pixel-sized pen that hangs down and to the right of the anchor point on the path. Operations that fill a figure operate by filling the interior of that infinitely thin path.

In particular,

If you draw a figure that covers a given rectangle, that figure occupies one extra row of pixels on the right and bottom edges as compared to filling a figure that is bounded by that same rectangle.

Upvotes: 3

Related Questions