Tony the Pony
Tony the Pony

Reputation: 41447

Actual bounds of a rectangle when drawn with Graphics2D

How can I reliably determine the exact extent of a rectangle when it is rendered using Graphics2D?

Suppose I have a rectangle that originates at (20,40) and is 200 pixels wide and 100 pixels tall:

drawRect (20,40,200,100);

The answer to my question is easy when this is a simple rectangle, drawn crudely with a 1-pixel pen. However, what are the exact bounds that are affected in the rendering process when the pen is 2 pixels, 3 pixels, or more? And will the rectangle "spill over" to adjacent pixels when anti-aliasing is used?

What is the most reliable way to calculate the exact bounds that will be manipulated?

Upvotes: 0

Views: 596

Answers (1)

rlibby
rlibby

Reputation: 6021

If you need to know the bounds of a stroked shape, you can stroke it yourself and check the bounds.

Graphics2D g;
g.getStroke().createStrokedShape(myRect).getBounds();

Upvotes: 3

Related Questions