0

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)){ ...

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

2 Comments

How do I use the TextLayout.getBounds method? I imported the TextLayout package but I can't seem to figure out how to invoke the method.
Thank you for your help. Your answer made me realize a major blunder I made.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.