JFrame图像碰撞检测

时间:2014-01-24 04:56:39

标签: java image jframe collision-detection

所以我有一个图像,当玩家与剑图像发生碰撞时,库存图像会变为包含剑的库存图像。我到处都试图解决这个问题!任何帮助将不胜感激!

package main;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.geom.Ellipse2D;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

public class grow extends JPanel implements ActionListener, KeyListener {
    int x = 0, y = 0, velx = 0, vely = 0;
    Timer t = new Timer(5, this);

    public grow() {
        t.start();
        addKeyListener(this);
        setFocusable(true);
        setFocusTraversalKeysEnabled(false);
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        ImageIcon ic = new ImageIcon("C:\\Users\\Michael\\Downloads\\map.jpg");
        Image i = ic.getImage();
        g2d.drawImage(i, 0, 0, null);

        ImageIcon sw = new ImageIcon("C:\\Users\\Michael\\Downloads\\sword.jpg");
        Image s = sw.getImage();
        g2d.drawImage(s, 100, 100, null);

        ImageIcon inv = new ImageIcon("C:\\Users\\Michael\\Downloads\\inv.jpg");
        Image inv1 = inv.getImage();
        g2d.drawImage(inv1, 350, 0, null);

        ImageIcon im = new ImageIcon("C:\\Users\\Michael\\Downloads\\man.jpg");
        Image m = im.getImage();

        if(vely >= 0) {  
            g2d.drawImage(m, x, y, null);
        }
        if(vely < 0) {
            ImageIcon imb = new ImageIcon("C:\\Users\\Michael\\Downloads\\manback.jpg");
            Image mb = imb.getImage();
            g2d.drawImage(mb, x, y, null);
        }
        if(velx > 0) {
            ImageIcon imr = new ImageIcon("C:\\Users\\Michael\\Downloads\\manleft.jpg");
            Image mr = imr.getImage();
            g2d.drawImage(mr, x, y, null);
        }
        if(velx < 0) {
            ImageIcon iml = new         ImageIcon("C:\\Users\\Michael\\Downloads\\manright.jpg");
            Image ml = iml.getImage();
            g2d.drawImage(ml, x, y, null);
        }

    }

    public void actionPerformed(ActionEvent e) {
        repaint();
        x += velx;
        y += vely;

    }

    public void up() {
        vely = -2;
        velx = 0;
    }

    public void down() {
        vely = 2;
        velx = 0;
    }

    public void left() {
        vely = 0;
        velx = -2;
    }

    public void right() {
        vely = 0;
        velx = 2;
    }


    public void keyPressed(KeyEvent e) {
        int code = e.getKeyCode();
        if(code == KeyEvent.VK_UP) {
            up();
        }
        if(code == KeyEvent.VK_DOWN) {
            down();
        }

        if(code == KeyEvent.VK_RIGHT) {
            right();
        }

        if(code == KeyEvent.VK_LEFT) {
            left();
        }
    }

    public void stop(){
        velx = 0;
        vely = 0;
    }

    public void keyReleased(KeyEvent e) {
        int code = e.getKeyCode();
        if(code == KeyEvent.VK_UP){
            stop();
        }
        if(code == KeyEvent.VK_DOWN) {
            stop();
        }

        if(code == KeyEvent.VK_RIGHT) {
            stop();
        }

        if(code == KeyEvent.VK_LEFT) {
            stop(); 
        }
    }

    public void keyTyped(KeyEvent e) {

    }
}

1 个答案:

答案 0 :(得分:0)

为每个ImageIcon创建一个不可见的Rectangle对象。我建议先把每个对象(例如Man,Sword等)作为自己的类。然后执行以下操作:

public class Man{

    private int x, y;
    private Image image;
    boolean visible;
    private int width, height;



    public Man(int x, int y) {

        ImageIcon im = new ImageIcon("C:\\Users\\Michael\\Downloads\\man.jpg");
        image = im.getImage();
        visible = true;
        width = image.getWidth(null);
        height = image.getHeight(null);
        this.x = x;
        this.y = y;
    }

    //rest of your getters/setters

    //this will be used for collision detection
    public Rectangle getBounds() {
        return new Rectangle(x, y, width, height);
    }


    public void move() {
        //move code goes here
    }
}

您也希望为其他课程执行此操作。现在我们假设你也是一个Sword课程。然后您可以检测到碰撞:

    //create an instance of Man
    //create an instance of Sword


    Rectangle manRect = man.getBounds();
    Rectangle swordRect = sword.getBounds();


   if (manRect.intersects(swordRect )) {
       //perform your action code here
       //update sprites
   }

Source