如何在Java中将图像或imageIcon添加到鼠标监听器?
这是我的代码。我想在鼠标点击后对imageIcon做任何事情。
public class Bubbles extends JPanel {
public static final int LINE_OUT = 440;
int x = 20;
int y = 50;
int v = 4;
Image img = new ImageIcon("res/lop.png").getImage();
BackSide back;
public Bubbles(int x, int y, int v, BackSide back) {
this.x = x;
this.y = y;
this.v = v;
this.back = back;
setFocusable(true);
}
public void move() {
if (y >= 440) {
y = 0;
} else {
y += v;
}
}
}
答案 0 :(得分:2)
您可以做的是,您可以使用JLabel
并将ImageIcon
设置为JLabel
。使用setOpaque(false)
使{{1}}透明。然后,添加监听器并执行您想做的任何事情:)
答案 1 :(得分:1)
最简单的解决方案是使用JLabel
并设置它的icon
属性。有关详细信息,请参阅How to use labels。
如果您必须自己绘制图像(即,您想要在容器中应用效果或执行动画),那么您需要将MouseListener
添加到呈现图像的容器中并进行测试以查看是否鼠标事件发生在图像的上下文中,基于它的位置和大小。
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class ImageMouseListener {
public static void main(String[] args) {
new ImageMouseListener();
}
public ImageMouseListener() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new ImagePane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class ImagePane extends JPanel {
private BufferedImage img;
private Point imgPoint;
public ImagePane() {
try {
img = ImageIO.read(...);
imgPoint = new Point(100, 100);
} catch (IOException ex) {
ex.printStackTrace();
}
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (img != null && imgPoint != null) {
Point me = e.getPoint();
Rectangle bounds = new Rectangle(imgPoint, new Dimension(img.getWidth(), img.getHeight()));
if (bounds.contains(me)) {
System.out.println("I was clicked!");
}
}
}
});
}
@Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (img != null && imgPoint != null) {
g.drawImage(img, imgPoint.x, imgPoint.y, this);
}
}
}
}
请查看Performing Custom Painting和How to use Mouse Listeners了解详情
答案 2 :(得分:0)
如果您不想使用JLabel,可以通过覆盖paintComponent方法将图像绘制到面板上。然后您可以将监听器添加到JPanel.I woud强烈建议您使用JLabel,但如果您仍然使用其他方式,请在此处可以看到一些例子如何做到这一点。 How to set background image in Java?
Icon icon = new ImageIcon("give path of the image");
JLabel lb = new JLabel(icon);
//now set listener for label.
lb.addMouseListener(new MouseAdapter()
{
@Override
public void mouseClicked(MouseEvent e)
{
//statement
}
});