如何在JAVA中旋转AWT矩形? [英] How to Rotate AWT rectangle in JAVA?

查看:220
本文介绍了如何在JAVA中旋转AWT矩形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个小型的Java 2D游戏,我想知道是否有任何方法可以旋转AWT矩形

I am creating a small java 2D game and i want to know if there is any way to rotate AWT rectangle

AffineTransform origXform = g2d.getTransform();
AffineTransform newXform = (AffineTransform) origXform.clone();
newXform.rotate(angle, pivotX, pivotY); // pivotX ,pivotY is the starting 
point of the hand image
g2d.setTransform(newXform);
Rectangle arm = new Rectangle(bowX + 5,  bowY + 55, 60, 5);
g2d.drawImage(playerBowReadyImg, bowX, bowY, null);  //hand image

在上面的代码中,我只是简单地绘制了根据鼠标位置旋转的手形图像,也将矩形设置在手形上,但是问题是矩形不随手形图像一起旋转.

on the above code i simply draw the hand image which rotates based on the mouse position, i also set the rectangle on the hand but the problem is rectangle is not rotating along with the hand image.

我也不将矩形用于任何绘图目的,而是用于检测碰撞.

also i am not using rectangle for any drawing purpose but to detect the collision.

但是使用g2d.draw(arm);绘制旋转的矩形,但实际上不旋转矩形,而只是绘制旋转的矩形.

however using g2d.draw(arm); draws the rotated rectangle but it not actually rotate the rectangle it just draws the rotated one.

任何建议都值得赞赏.

好吧,我的问题被标记为重复,所以我尝试了在那找到的答案,但是我得到的代码仅出于绘制目的而旋转矩形.

Ok my question is marked as duplicate so i tried the answers i find there but the code i get only rotate my rectangle for the draw purpose only.

描述问题的图像

现在更具体地说,图像中的箭头只能检测到蓝色矩形(原始位置)而不是红色矩形(旋转矩形)的碰撞.

now to be more specific the arrow in the image can only detect the collision for the blue rectangle (the original position) instead of the red one (rotated rectangle).

再次,我不想实际绘制矩形,但是想在箭头与矩形碰撞时检测到碰撞.

again i don't want to actually draw the rectangle but want to detect the collision when the arrow collide with the rectangle.

推荐答案

请参见

See AffineTransform.createTransformedShape(Shape) which:

在通过此变换将其转换后,返回由指定Shape的几何形状定义的新Shape对象.

Returns a new Shape object defined by the geometry of the specified Shape after it has been transformed by this transform.

这篇关于如何在JAVA中旋转AWT矩形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆