检查GUI图像是否重叠另一个

时间:2014-10-10 01:19:38

标签: java user-interface

我有一个简单的问题。我试图通过获取两者的x和y坐标以及大小来查看GUI Graphics中的图像是否与另一个图像重叠。如何使用if语句检查? (或者有更好的方法吗?)

图像是我从抽象Sprite类扩展的对象的一部分。我应该检查当前的 Sprite图像是否与s中包含的Sprite图像重叠。

像往常一样,非常感谢你的时间和帮助。 (对不起,如果我提供的信息令人困惑。)

    import java.awt.Graphics;
import java.awt.Image;

import javax.imageio.ImageIO;

import java.io.File;
import java.io.IOException;
import java.util.Random;

abstract class Sprite
{
    private int x;
    private int y;
    private int size;
    private int w;
    private int h;
    private int xSlope;
    private int ySlope;
    private Image image;
    private static Random rand;

    public Sprite(int xIn, int yIn, int width, int height, String imagePath, int imageSize) {
        if (rand == null) {
            rand = new Random();
        }
        size = imageSize;
        setImage(imagePath);
        x = xIn;
        y = yIn;
        w = width;
        h = height;
        xSlope = rand.nextInt(11) - 5;
        ySlope = rand.nextInt(11) - 5;
    }

    public int getX() { return x; }
    public int getY() { return y; }
    public int getSize() { return size; }
    public void setSize(int s) { size = s; }
    public void setX(int xIn) { x = xIn; }
    public void setY(int yIn) { y = yIn; }

    public void setImage(String imagePath) {
        try {
            image = ImageIO.read(new File(imagePath));
        } catch (IOException ioe) {
            System.out.println("Unable to load image file.");
        }
    }
    public Image getImage() { return image; }

    public boolean overlaps(Sprite s) {
        int locX = this.getX();
        int locY = this.getY();
        int sz = getSize();

        int overX = s.getX();
        int overY = s.getY();
        int overSz = getSize();

        if 
        return false;
    }

    public void update(Graphics g) {
        g.drawImage(getImage(), x, y, getSize(), getSize(), null);
    }

    public void move() {
        // Move the Sprite
        int x = getX() + xSlope;
        int y = getY() + ySlope;
        if (x < 0) x = w;
        if (x > w) x = 0;
        if (y < 0) y = h;
        if (y > h) y = 0;
        setX(x);
        setY(y);
    }

}

0 个答案:

没有答案