rmp2150
rmp2150

Reputation: 797

Java Rectangle Intersect Method

In the code I have written, I am trying to determine when two Rectangle2D shapes have intersected. However when I run the code, the intersect method always returns true even when the shapes are clearly not. If anyone has any insight into this issue I would greatly appreciate it.

    Graphics2D g2 = (Graphics2D) getGraphics();
    FontRenderContext context = g2.getFontRenderContext();

    Rectangle2D rectangleOne = fontOne.getStringBounds(blockOne, context);
    Rectangle2D rectangleTwo= fontTwo.getStringBounds(blockTwo, context);

    if(rectangleOne.intersects(rectangleTwo)){ ...

Upvotes: 0

Views: 2211

Answers (1)

Mister Smith
Mister Smith

Reputation: 28158

The getStringBounds method returns a logical bound. To obtain the Graphical bound, use TextLayout.getBounds instead.

Example:

Font font = Font.getFont("Helvetica-bold-italic");
FontRenderContext frc = g.getFontRenderContext();
TextLayout layout = new TextLayout("This is a string", font, frc);

Upvotes: 1

Related Questions