我想做一个简单的射击游戏。 我有一个包含 IMG 和 Shape 的 Plane 类(这是该 IMG 的边框,因此我可以使用 isIntersect 方法)属性。
然后我旋转并平移对象(使用 AffineTransform ),之后我想获取它的坐标。有人可以帮助我吗?
Ps:我尝试使用矩形而不是形状,但似乎无法使用 CreateTransformedShape 制作矩形对象。
这是我的代码:
public class EPlane {
Shape l = new Rectangle(0,0,45,55);
BufferedImage ep;
private String imgFileName = "Eplane.PNG";
ArrayList<Rectangle> EShoot = new ArrayList();
int live;
AffineTransform at;
AffineTransform at2;
AffineTransform at3;
boolean Alrdy = false;
EPlane(){
this.live = 5;
at = new AffineTransform();
at2 = new AffineTransform();
at3 = new AffineTransform();
URL imgUrl = getClass().getClassLoader().getResource(imgFileName);
if (imgUrl == null) {
System.err.println("couldn't find file: " + imgFileName);
} else {
try {
ep = ImageIO.read(imgUrl);
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
以及想要找到x坐标的类
public class EPlaneCommander {
ArrayList<ArrayList<EPlane>> EPSList = new ArrayList();
int count = 0;
int count1 = 0;
int count2 = 0;
public EPlaneCommander() {
ArrayList<EPlane> EPS = new ArrayList();
ArrayList<EPlane> EPS1 = new ArrayList();
ArrayList<EPlane> EPS2 = new ArrayList();
EPSList.add(EPS);
EPSList.add(EPS1);
EPSList.add(EPS2);
}
public void move1() {
if (count % 50 == 1 && EPSList.get(0).size() < 10) {
EPSList.get(0).add(new EPlane());
}
for (int i = 0; i < EPSList.get(1).size(); i++) {
if (!EPSList.get(0).get(i).Alrdy) {
EPSList.get(0).get(i).at.translate(400, 0);
EPSList.get(0).get(i).Alrdy = true;
EPSList.get(0).get(i).l = EPSList.get(0).get(i).at.createTransformedShape(EPSList.get(0).get(i).l);
EPSList.get(0).get(i).at2.rotate(0.004, 0, 0);
}
//EPS.get(i).at2.rotate(0.0001, 0, 0);
EPSList.get(0).get(i).l = EPSList.get(0).get(i).at2.createTransformedShape(EPSList.get(0).get(i).l);
EPSList.get(0).get(i).at.rotate(0.004, -400, 0);
Point s = (Point) EPSList.get(2).get(i).at.createTransformedShape(EPSList.get(0).get(i).l);
}
count++;
}
最佳答案
Java 的 Shape
类有一个 getBounds()
method可用于获取包含 X 和 Y 坐标的 Rectangle 对象。请记住,对于 Java 图形,这些坐标代表矩形的左上角,而不是您习惯的典型笛卡尔系统的左下角。
该矩形还可以用作边界框,以便在玩家导弹图形的实现中进行碰撞检测,但 Shape 已经有一个可以用作边界框的 intersects
方法。
关于java - 如何在Java中获取Shape对象的x坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20319694/