Java - 悬停在图像事件上

时间:2016-07-05 17:33:37

标签: java graphics2d

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

我以前为常规矩形做过这个,但这个形状不是一个基本的矩形(点击HERE查看形状)。

使用Graphics2D将形状绘制为BufferedImage。

提前感谢您的帮助!gya

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

您可以通过添加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) { }
}

这是截图。

Screenshot