Java矩形相交方法

时间:2011-12-12 09:21:45

标签: java

在我编写的代码中,我试图确定两个Rectangle2D形状何时相交。但是,当我运行代码时,即使形状显然不是,交叉方法也总是返回true。如果有人对这个问题有任何见解,我将不胜感激。

    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 个答案:

答案 0 :(得分:1)

getStringBounds方法返回逻辑边界。要获取图形边界,请改用TextLayout.getBounds

示例:

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