A
A
arramaga2015-11-04 17:22:30
Java
arramaga, 2015-11-04 17:22:30

How to detect hover event on image?

Started learning Java. I was doing checkers and ran into a problem.
f7b70361dcce4466aeb560776b8ab899
Now all the fields and checkers are added with regular images: Image img1 = new ImageIcon("res/pole1.jpg").getImage();
How can I check if the cursor is hovered over a certain picture and if a key is pressed?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
arramaga, 2015-11-04
@arramaga

Now this is what my super code looks like. What needs to be fixed?
package checkers;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
public class Pole extends JPanel {
Image img1 = new ImageIcon("res/pole1.jpg").getImage();
Image img2 = new ImageIcon("res/pole2.jpg").getImage();
Image img3 = new ImageIcon("res/s1.png").getImage();
Image img4 = new ImageIcon("res/s2.png").getImage();
Image img5 = new ImageIcon("res/select.png").getImage();
public void paint(Graphics g){
g = (Graphics2D) g;
int x=0, y=0;
int x2 = 105, y2=705;
for(int z=0; z<4; z++){
g.drawImage(img1, x, y, null);
x=x+100;
g.drawImage(img2, x, y, null);
x=x+100;
}
y=y+100;
x = 0;
for(int z=0; z<4; z++){
g.drawImage(img2, x, y, null);
x=x+100;
g.drawImage(img1, x, y, null);
x=x+100;
}
y=y+100;
x = 0;
for(int z=0; z<4; z++){
g.drawImage(img1, x, y, null);
x=x+100;
g.drawImage(img2, x, y, null);
x=x+100;
}
y=y+100;
x = 0;
for(int z=0; z<4; z++){
g.drawImage(img2, x, y, null);
x=x+100;
g.drawImage(img1, x, y, null);
x=x+100;
}
y=y+100;
x = 0;
for(int z=0; z<4; z++){
g.drawImage(img1, x, y, null);
x=x+100;
g.drawImage(img2, x, y, null);
x=x+100;
}
y=y+100;
x = 0;
for(int z=0; z<4; z++){
g.drawImage(img2, x, y, null);
x=x+100;
g.drawImage(img1, x, y, null);
x=x+100;
}
y=y+100;
x = 0;
for(int z=0; z<4; z++){
g.drawImage(img1, x, y, null);
x=x+100;
g.drawImage(img2, x, y, null);
x=x+100;
}
y=y+100;
x = 0;
for(int z=0; z<4; z++){
g.drawImage(img2, x, y, null);
x=x+100;
g.drawImage(img1, x, y, null);
x=x+100;
}
for(int z=0; z<4; z++){
g.drawImage(img3, x1, y1, null);
x1=x1+200;
}
x1=105;
y1=105;
for(int z=0; z<4; z++){
g.drawImage(img3, x1, y1, null);
x1=x1+200;
}
x1=5;
y1=205;
for(int z=0; z<4; z++){
g.drawImage(img3, x1, y1, null);
x1=x1+200;
}
for(int z=0; z<4; z++){
g.drawImage(img4, x2, y2, null);
x2=x2+200;
}
x2=5;
y2=605;
for(int z=0; z<4; z++){
g.drawImage(img4, x2, y2, null);
x2=x2+200;
}
x2=105;
y2=505;
for(int z=0; z<4; z++){
g.drawImage(img4, x2, y2, null);
x2=x2+200;
}
}
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question