Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

How do I detect a collision of two imageicons in java? import java.awt.*; import

ID: 3896636 • Letter: H

Question

How do I detect a collision of two imageicons in java?

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class DieRollPanel extends JPanel implements ActionListener,KeyListener {
public static ImageIcon image,image2;
public static Timer timer;
public static int x,y,movex,movey;
public static int x2,y2,movex2,movey2,movey2again,width,height;
public Image im;
public static int move=0;
  
  
  
public DieRollPanel(){
image = new ImageIcon("die14.jpg");
image2 = new ImageIcon("die51.jpg");
timer = new Timer(150,this);
x=0;
y=40;
// movex=15;
//movey=5;
x2=230;
y2=10;
movey2=10;
movey2again=-10;
setPreferredSize (new Dimension(300, 100));
setBackground (Color.black);
setOpaque(false);
timer.start();
addKeyListener(this);
setFocusable(true);
  
  
  
}
public void paintComponent(Graphics page){
super.paintComponent(page);
  
image.paintIcon(this,page,x,y);
image2.paintIcon(this, page, x2, y2);
  

  
  
  
  
}
  
  
public void actionPerformed (ActionEvent event){
repaint();
  
x+=movex;

//y+=movey;
y2+=movey2;
if (y2==90){
//movey2=0;
movey2=-10;
  
}
if (y2==10){
movey2=10;
}
  
}

  

  

  
/* public void stop(){
movex=0;

}*/
public void move(){
movex=10;
}

  
  
  
public void keyPressed(KeyEvent e){
int code = e.getKeyCode();

if (code == KeyEvent.VK_ENTER){
move();
  
  
}
/*if (code == KeyEvent.VK_DOWN){
stop();
}*/
/* if (code == KeyEvent.VK_LEFT){
left();
}
if (code == KeyEvent.VK_RIGHT){
right();
}

Sound.PlaySound(go);

*/
}
public void keyTyped(KeyEvent e){}
public void keyReleased(KeyEvent e){}
}

Explanation / Answer

Answer : The functionality of collision detection can be implemented with the use of Rectangles in Java

First

Import Java.awt.Rectangle

In the begining define a boolean variable

boolean collision =false

The rectangle class in Java has a constructor to fetch the X,Y coordinates and the width and height of the objects.

Create a method to get the coordinates, width and height of the objects getting collided

public Rectangle encompass()

{ return ( new Rectangle(x,y,300,100)

}

The set maximum width and height of the images are 300and 100 respectively

We create another method to detect collision of the imageicons

public void collisiondetection()

{

Rectangle a= image.encompass(); //store x,ycoordinates,width ,height of image into the rectangle a

   Rectangle b= image2.encompass() ; //store x,y coordinates,width ,height of image2 into rectangle b

if(a.intersects(b))

collision=true; //set boolean value to true if there is a collision

system.out.println(" Collision Detected");

else

collision =false

Call the function collisiondetection() in the method to move the imageicon so that collisions if any can be detected.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote