2

我正在使用 Java Slick StateBaeedGame 并希望为我的碰撞旋转我的矩形,我知道这可以使用 Graphics 或 Graphics2D 对象来实现视觉目的,但这不会修改矩形本身,即最初与变量一起列出的矩形并在图形方法中调用不旋转,为了让事情更清楚,这里有一些代码:

    java.awt.geom.Rectangle2D.Float rectTwo = new Rectangle2D.Float(460 + buckyPositionX, 50 + buckyPositionY, 100, 100);

public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException{
      worldMap.draw(buckyPositionX,buckyPositionY); //draw the map at 0,0 to start
      bucky.draw(shiftX,shiftY); //draw bucky at 320, 160 (center of the screen)


    g.rotate(460 + buckyPositionX, 50 + buckyPositionY, 40);
    g.fillRect((float)rectTwo.getX(), (float)rectTwo.getY(), (float)rectTwo.getWidth(), (float)rectTwo.getHeight());
      }

当我加载我的 GUI 时,矩形 rectTwo 将显示为旋转,但它实际上并未旋转,如果我测试碰撞,矩形仍处于 0 度。

那么,如何让我的矩形变量改变它的角度?

4

1 回答 1

1

一般来说,你不能。

你可以做的是改变形状的路径......

PathIterator pathIterator = shape.getPathIterator(AffineTransform.getRotateInstance(Math.toRadians(33.5)));

这对您现在没有多大用处,但您可以使用 aPath2D#append将路径附加回Shape对象中......

GeneralPath path = new GeneralPath();
path.append(pathIterator, true);

哪个可以让你画...

((Graphics2D)g).fill(path);

当然,这假设您的Graphics上下文是一个Graphics2D实例。

这也意味着,您不能保持对 a 的直接引用Rectange2D,而是需要使用它Shape

于 2013-01-15T00:00:00.317 回答