Java - 悬停图像事件

Java - Hover Over Image event

我正在为我目前正在设计的游戏创建一个菜单,我正在使用我在 photoshop 中设计的图像用作菜单按钮,但我想知道是否有任何方法可以检测鼠标是否悬停在图像上。

我以前对常规矩形做过此操作,但这个形状不是基本矩形(单击 HERE 查看形状)。

正在使用 Graphics2D 将形状绘制为 BufferedImage。

提前感谢您的帮助!gya

您可以向对象添加 MouseListener,并为 MouseEntered 事件编写代码。这将在鼠标首次进入组件边界时触发,但在单击时不会触发。 Oracle 对此有很好的文档。

https://docs.oracle.com/javase/tutorial/uiswing/events/mouselistener.html

您可以绘制出您的多边形,并通过添加 MouseListener 来确定选择了哪个多边形。下次重绘时,可以显示选区。

您可以使用以下方法检查事件的点是否包含在多边形内:

[Java Docs] :: java.awt.Polygon.contains(Point)

这是一个例子:

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.*;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.*;

public class PolygonDetectorPanel extends JPanel implements MouseListener {
    private static final long serialVersionUID = -8929816396666863908L;
    private BufferedImage backgroundImage;
    private List<Polygon> shapes;
    private Polygon selectedPoly;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame frame = new JFrame("Polygon Detector");
                PolygonDetectorPanel panel = new PolygonDetectorPanel();

                frame.setContentPane(panel);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public PolygonDetectorPanel() {
        super();
        initialize();

        this.addMouseListener(this);
    }

    protected void initialize() {
        try {
            InputStream is = PolygonDetectorPanel.class.getClassLoader().getResourceAsStream("resources/buttons.png");
            this.backgroundImage = ImageIO.read(is);
        } catch (IOException e) {
            e.printStackTrace();
        }

        int width = this.backgroundImage.getWidth();
        int height = this.backgroundImage.getHeight();

        this.setPreferredSize(new Dimension(width, height));
        this.shapes = new ArrayList<Polygon>();

        int buttonStartX = 13;
        int buttonStartY = 54;
        int buttonOffset = 100;

        for (int i = 0; i < 4; i++) {
            int[] w  = { buttonStartX       , buttonStartY + 33 + (i * buttonOffset) };
            int[] nw = { buttonStartX +  50 , buttonStartY      + (i * buttonOffset) };
            int[] ne = { buttonStartX + 300 , buttonStartY      + (i * buttonOffset) };
            int[] e  = { buttonStartX + 350 , buttonStartY + 33 + (i * buttonOffset) };
            int[] se = { buttonStartX + 300 , buttonStartY + 66 + (i * buttonOffset) };
            int[] sw = { buttonStartX +  50 , buttonStartY + 66 + (i * buttonOffset) };

            int[] xPoints = { w[0], nw[0], ne[0], e[0], se[0], sw[0], w[0]};
            int[] yPoints = { w[1], nw[1], ne[1], e[1], se[1], sw[1], w[1]};

            this.shapes.add(new Polygon(xPoints, yPoints, 6));
        }
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        if (this.backgroundImage != null) {
            g.drawImage(this.backgroundImage, 0, 0, null);
        }

        Graphics2D g2 = (Graphics2D) g;
        g2.setStroke(new BasicStroke(5, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 10.0f));

        for (Polygon poly : this.shapes) {
            if (poly == this.selectedPoly) {
                g.setColor(Color.GREEN);
            } else {
                g.setColor(Color.RED);
            }

            g.drawPolygon(poly);
        }
    }

    protected Polygon getClickedShape(Point point) {
        for (Polygon poly : this.shapes) {
            if (poly.contains(point)) {
                return poly;
            }
        }

        return null;
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        Polygon poly = getClickedShape(new Point(e.getX(), e.getY()));

        if (poly != null) {
            this.selectedPoly = poly;
        } else {
            this.selectedPoly = null;
        }

        repaint();
    }

    @Override
    public void mousePressed(MouseEvent e) { }

    @Override
    public void mouseReleased(MouseEvent e) { }

    @Override
    public void mouseEntered(MouseEvent e) { }

    @Override
    public void mouseExited(MouseEvent e) { }
}

这是截图。