具有Double值和Int值的Rectangle

时间:2017-01-13 02:51:03

标签: java swing graphics2d affinetransform

我正在使用java制作游戏,我的播放器可以旋转360度。我必须制作一个随播放器旋转和移动的hitbox。我使用Rectangle来做到这一点,但是,它使用int值而不是double值,因此,hitbox的定位稍微偏离(因为我向整数添加了double值)。我该如何解决?

我已经尝试过Rectangle2D.Double,但我无法修改x和y值。

以下是相关代码:

宣言:

private Rectangle hitbox;
private Shape transformed;

初​​始化

hitbox = new Rectangle((int) x, (int) y + 20, currentImg.getWidth(), currentImg.getHeight()/2 + 20);
transformed = hitbox;

运动

hitbox.x += xM * Math.sin(Math.toRadians(angle));
hitbox.y += yM * Math.cos(Math.toRadians(angle));

轮换

AffineTransform transform = new AffineTransform();
transform.rotate(Math.toRadians(this.angle), hitbox.x + hitbox.getWidth()/2, hitbox.y + hitbox.getHeight()/2);
transformed = transform.createTransformedShape(hitbox);

0 个答案:

没有答案
相关问题