1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
| public class TestImage extends JFrame implements Runnable {
private static final long serialVersionUID = 4983727800375272097L;
int g=0;
int id=0;
BufferedImage img = null;
public TestImage(int id) {
this.id=id;
System.out.println(id);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(300, 300);
setLocationRelativeTo(null);
}
public void run()
{
try {
img = ImageIO.read(new File("image.jpeg"));
} catch (IOException e) {
e.printStackTrace();
}
setContentPane(new MyCanvas(img));
setVisible(true);
}
public static void main(String[] args) {
new TestImage(id);
}
private class MyCanvas extends JComponent implements MouseListener {
private static final long serialVersionUID = 8845913940083986438L;
private BufferedImage buff = null;
private Raster data = null;
public MyCanvas(BufferedImage img) {
this.addMouseListener(this);
this.buff = img;
this.data = img.getData();
}
public void paintComponent(Graphics g) {
g.drawImage(buff, 0, 0, buff.getWidth(), buff.getHeight(), this);
}
private void testLocation(Point mouse, String text) {
if(data.getBounds().contains(mouse))
{
System.out.println("image"+id);
}
}
public void mouseClicked(MouseEvent e) {
Point p = e.getPoint();
testLocation(p, "mouseClicked");
}
public void mousePressed(MouseEvent e) {
Point p = e.getPoint();
testLocation(p, "mousePressed");
}
public void mouseReleased(MouseEvent e) {
Point p = e.getPoint();
testLocation(p, "mouseReleased");
}
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
}
} |
Partager