java - 如何判断一个形状是否已经接触到另一个形状?

标签 java graphics keylistener 2d-games boundary

我有一个工作类,我可以使用键盘在屏幕上移动一个圆圈,同时还有其他圆圈以不同的速度在屏幕上移动。

我到达了这一点,我正在尝试弄清楚如何制作它,以便我可以查明我的形状是否接触了另一个形状,但这就是我所坚持的部分,因为我对如何使其发挥作用没有太多想法。我尝试过各种方法,但大多数都不起作用。

public class Test extends Panel implements Runnable, KeyListener, MouseMotionListener {

static final int left = 37;
static final int right = 39;
static final int down = 40;
static final int up = 38;

static boolean leftPress = false;
static boolean rightPress = false;
static boolean upPress = false;
static boolean downPress = false;

/*****/
static int upperLeftX;
static int upperLeftY;
static int lowerLeftX;
static int lowerLeftY;
static int upperRightX;
static int upperRightY;
static int lowerRightX;
static int lowerRightY;

int eUpperLeftX;
int eUpperLeftY;
int eLowerLeftX;
int eLowerLeftY;
int eUpperRightX;
int eUpperRightY;
int eLowerRightX;
int eLowerRightY;
/*****/

static boolean eat = false;
static boolean eaten = false;
static int mouseLocationX;
static int mouseLocationY;
static String mouseLocation = "";

/**/
int x1_;
int y1_;
int eSize;
int eRandSpawn;
int sleep;
Color eColor;
/**/

static int x1 = 250;
static int y1 = 250;
static int size = 30;

static JFrame frame = new JFrame("Fishy");
static Test panel = new Test(500, 500, 9);

// static Test time1 = new Test(1000);
// static Test time2 = new Test(500);
// static Test time3 = new Test(250);
// static Test time4 = new Test(Methods.randNum(6,20),Methods.randNum(0, 450));
// static Test time5 = new Test(Methods.randNum(6,20),Methods.randNum(0, 450));

static Test eFish[] = new Test[20];

/** CONSTRUCTOR **/
public Test() {

    sleep = Methods.randNum(6, 50);
    y1_ = Methods.randNum(0, 450);
    eRandSpawn = Methods.randNum(1, 2);
    eColor = Methods.randColor();
    eSize = Methods.randNum(5, 120);

    if (eRandSpawn == 1) {
        x1_ = -100;
    } else {
        x1_ = 600;
    }

    eUpperLeftX = x1_;
    eUpperLeftY = y1_;
    eLowerLeftX = x1_;
    eLowerLeftY = y1_ + eSize;
    eUpperRightX = x1_ + eSize;
    eUpperRightY = y1_;
    eLowerRightX = x1_ + eSize;
    eLowerRightY = x1_ + eSize;

}

/** CONSTRUCTOR **/
public Test(int width, int length, int minusBy) {
    super(width, length, minusBy);
}

/** MAIN **/
public static void main(String args[]) throws InterruptedException {
    for (int i = 0; i < 20; i++) {
        eFish[i] = new Test();
    }
    Thread time[] = new Thread[20];
    for (int i = 0; i < 20; i++) {
        time[i] = new Thread(eFish[i]);
    }

    // Thread time1_ = new Thread(time1);
    // Thread time2_ = new Thread(time2);
    // Thread time3_ = new Thread(time3);

    // Thread time4_ = new Thread(time4);
    // Thread time5_ = new Thread(time5);

    frame.add(panel);
    frame.addKeyListener(panel);
    panel.addMouseMotionListener(panel);
    Frame.showFrame(frame);

    // time1_.start();
    // time2_.start();
    // time3_.start();

    // time4_.start();
    // time5_.start();

    for (int i = 0; i < 20; i++) {
        time[i].start();
    }

    while (true) {

        if (upPress) {
            y1--;
        }
        if (downPress) {
            y1++;
        }
        if (leftPress) {
            x1--;
        }
        if (rightPress) {
            x1++;
        }

        if (upPress || downPress || leftPress || rightPress) {
            upperLeftX = x1;
            upperLeftY = y1;
            lowerLeftX = x1;
            lowerLeftY = y1 + size;
            upperRightX = x1 + size;
            upperRightY = y1;
            lowerRightX = x1 + size;
            lowerRightY = x1 + size;

            Thread.sleep(6);
            frame.repaint();
        }

        for (int i = 0; i < 20; i++) {

            if (eat) {

                size++;
                eFish[i].eRandSpawn = Methods.randNum(1, 2);

                if (eFish[i].eRandSpawn == 1) {
                    eFish[i].y1_ = Methods.randNum(0, 450);
                    eFish[i].x1_ = -100;
                    eFish[i].sleep = Methods.randNum(6, 50);
                    eFish[i].eSize = Methods.randNum(5, 120);
                    eFish[i].eColor = Methods.randColor();
                } else {
                    eFish[i].y1_ = Methods.randNum(0, 450);
                    eFish[i].x1_ = 600;
                    eFish[i].sleep = Methods.randNum(6, 50);
                    eFish[i].eSize = Methods.randNum(5, 120);
                    eFish[i].eColor = Methods.randColor();
                }

                eFish[i].eUpperLeftX = eFish[i].x1_;
                eFish[i].eUpperLeftY = eFish[i].y1_;
                eFish[i].eLowerLeftX = eFish[i].x1_;
                eFish[i].eLowerLeftY = eFish[i].y1_ + eFish[i].eSize;
                eFish[i].eUpperRightX = eFish[i].x1_ + eFish[i].eSize;
                eFish[i].eUpperRightY = eFish[i].y1_;
                eFish[i].eLowerRightX = eFish[i].x1_ + eFish[i].eSize;
                eFish[i].eLowerRightY = eFish[i].x1_ + eFish[i].eSize;

            }
            eat = false;
        }

    }
}

/** PAINT **/
public void paintComponent(Graphics g) {
    super.paintComponent(g);

    g.setColor(Color.CYAN);
    g.fillRect(0, 0, 501, 501);

    g.setColor(Color.BLACK);
    mouseLocation = String.format("%s,%s,%s", x1, y1, size);
    g.drawString(mouseLocation, 100, 100);

    g.setColor(Color.GREEN);
    g.fillOval(x1, y1, size, size);

    // g.drawOval(time4.x1_,time4.y1_,50,50);
    // g.drawOval(time5.x1_,time5.y1_,50,50);

    for (int i = 0; i < 20; i++) {
        g.setColor(eFish[i].eColor);
        g.fillOval(eFish[i].x1_, eFish[i].y1_, eFish[i].eSize, eFish[i].eSize);
    }

    /*
     * for (int i = 0; i < 10; i++) { g.setColor(Methods.randColor()); g.drawRect(x1 + i, y1 + i, width - i * 2, height - i * 2);
     * 
     * }
     */

    // g.drawString(time1.time, 20, 50);
    // g.drawString(time2.time, 20, 100);
    // g.drawString(time3.time, 20, 150);

}

@Override
public void run() {
    while (true) {
        try {
            Thread.sleep(sleep);

            eUpperLeftX = x1_;
            eUpperLeftY = y1_;
            eLowerLeftX = x1_;
            eLowerLeftY = y1_ + eSize;
            eUpperRightX = x1_ + eSize;
            eUpperRightY = y1_;
            eLowerRightX = x1_ + eSize;
            eLowerRightY = x1_ + eSize;

            if (x1_ == -150) {
                y1_ = Methods.randNum(0, 450);
                x1_ = 600;
                sleep = Methods.randNum(6, 50);
                eSize = Methods.randNum(5, 120);
                eColor = Methods.randColor();
            }
            if (x1_ == 650) {
                y1_ = Methods.randNum(0, 450);
                x1_ = -100;
                sleep = Methods.randNum(6, 50);
                eSize = Methods.randNum(5, 120);
                eColor = Methods.randColor();
            }
            if (eRandSpawn == 1) {
                x1_++;
            } else {
                x1_--;
            }
            frame.repaint();

        } catch (InterruptedException e) {
        }
    }

}

@Override
public void keyPressed(KeyEvent e) {

    if (up == e.getKeyCode()) {

        upPress = true;

    }
    if (down == e.getKeyCode()) {

        downPress = true;

    }
    if (left == e.getKeyCode()) {

        leftPress = true;

    }
    if (right == e.getKeyCode()) {

        rightPress = true;

    }
}

@Override
public void keyReleased(KeyEvent e) {

    if (up == e.getKeyCode()) {
        upPress = false;

    }
    if (down == e.getKeyCode()) {
        downPress = false;

    }
    if (left == e.getKeyCode()) {
        leftPress = false;

    }
    if (right == e.getKeyCode()) {
        rightPress = false;

    }
}

@Override
public void keyTyped(KeyEvent arg0) {
    // TODO Auto-generated method stub

}

@Override
public void mouseDragged(MouseEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void mouseMoved(MouseEvent e) {

    mouseLocationX = e.getX();
    mouseLocationY = e.getY();
    mouseLocation = String.format("%s,%s", mouseLocationX, mouseLocationY);
    frame.repaint();

}
}

最佳答案

public boolean madeContact(Ellipse a, Ellipse b) {
    //Center of ellipse
    int ax = a.getX(), ay = a.getY(), bx = b.getX(), by = b.getY();
    //Radii
    int arx = a.getRadiusX(), ary = a.getRadiusY();
    int brx = b.getRadiusX(), bry = b.getRadiusY();
    //Has hit x, y
    boolean hitx = Math.abs(ax - bx) < arx+brx;
    boolean hity = Math.abs(ay - by) < ary+bry;

    return hitx && hity;
}

关于java - 如何判断一个形状是否已经接触到另一个形状?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29783200/

相关文章:

java - 一个数学函数可以让我们得到特定类型 k 元的叶子数量?

java - 如何从Java中的多个方法中进行绘制?

JavaFX 线程问题

java - 将 KeyListener 添加到 TitleAreaDialog

java - 如何在事件处理程序中触发内部类事件处理程序(JAVA)

java - 无法使用maven构建项目

java - 所有不带参数的方法都可以访问变量吗?

java - 如何解决 Spring IoC 中 bean 之间的循环依赖?

c# - XNA 4.0 应用程序在遇到方法时突然关闭而没有抛出任何异常

java - addKeyListener 未按预期工作