วิธีรับพิกัด x ของ Shape Object ใน Java

ฉันอยากทำเกมยิงปืนแบบง่ายๆ ฉันมีคลาสเครื่องบินที่มีแอตทริบิวต์ IMG และรูปร่าง (นี่คือเส้นขอบสำหรับ IMG นั้น ดังนั้นฉันจึงสามารถใช้วิธี isIntersect ได้)

จากนั้นฉันหมุนและแปลวัตถุ (โดยใช้ AffineTransform ) หลังจากนั้นฉันต้องการรับพิกัดของมัน ใครสามารถช่วยฉันได้บ้าง?

ป.ล.: ฉันพยายามใช้สี่เหลี่ยมผืนผ้ามากกว่ารูปร่าง แต่ดูเหมือนว่าฉันไม่สามารถสร้างวัตถุสี่เหลี่ยมผืนผ้าด้วย 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++;
}

person Randy Raharjo    schedule 02.12.2013    source แหล่งที่มา


คำตอบ (2)


คลาส Shape ของ Java มีเมธอด getBounds() ที่สามารถใช้เพื่อรับวัตถุสี่เหลี่ยมผืนผ้าที่มีพิกัด X และ Y โปรดทราบว่าสำหรับกราฟิก Java พิกัดเหล่านั้นแสดงถึงมุมซ้ายบนของสี่เหลี่ยมผืนผ้า ซึ่งตรงข้ามกับด้านซ้ายล่างที่คุณคุ้นเคยในระบบคาร์ทีเซียนทั่วไป

สี่เหลี่ยมผืนผ้านั้นยังสามารถใช้เป็นกล่องขอบเขตเพื่อวัตถุประสงค์ในการตรวจจับการชนในการใช้งานกราฟิกขีปนาวุธของผู้เล่นได้ แต่ Shape มีวิธีการ intersects อยู่แล้วที่สามารถใช้เป็นกล่องขอบเขตได้

person MarsAtomic    schedule 02.12.2013

กรอบล้อมรอบรูปร่างที่แปลงแล้วจะมีขนาดใหญ่ขึ้นและอาจคลาดเคลื่อนได้ในบางจุด คุณสามารถแปลงรูปร่างเป็นพื้นที่และตรวจสอบจุดตัดได้

นี่คือสิ่งที่ฉันพบในคำตอบอื่น การตรวจจับการชนกันของ Java ระหว่างวัตถุ Shape สองวัตถุ

person user3357745    schedule 24.05.2019