简体   繁体   English

如何使用Java Swing创建图像映射?

[英]How to create an image map using Java Swing?

I need to make an image map using Swing that displays a background image, and then when the mouse hovers over (or clicks) specific hotspots, I need to pop up a 'zoomed-in' image and have it display. 我需要使用Swing制作一张显示背景图像的图像地图,然后当鼠标悬停(或单击)特定的热点时,我需要弹出一个“放大”图像并显示它。

I was thinking of extending JPanel to include an image reference and have that drawn thru the paintComponent(g) method. 我正在考虑扩展JPanel以包括图像参考,并通过paintComponent(g)方法绘制该参考。 This part I have done so far, and here's the code: 到目前为止,我已经完成了这一部分,下面是代码:

public class ImagePanel extends JPanel
{
    private static final long serialVersionUID = 1L;

    private Image image;

    public ImagePanel(Image image)
    {
        setImage(image);
    }

    public void setImage(Image newImage)
    {
        image = newImage;
    }

    @Override
    public void paintComponent(Graphics g)
    {
        Dimension size = getSize();
        g.drawImage(image, 0, 0, size.width, size.height, this);
    }

Could anyone recommend how I might listen for / respond to mouse clicks over defined hot-spots? 谁能推荐我如何在定义的热点上侦听/响应鼠标单击? Could someone additionally recommend a method for displaying the pop-ups? 有人可以另外推荐一种显示弹出窗口的方法吗? My gut reaction was to extend JPopupMenu to have it display an image, similar to the above code. 我的直觉反应是扩展JPopupMenu使其显示图像,类似于上面的代码。

Thanks for any help! 谢谢你的帮助!

To listen to the mouse clicks implement the MouseListener interface, and add it to your panel. 要收听鼠标单击,请实现MouseListener界面,并将其添加到面板中。 Then when the click is recieved you can use a JPopupMenu as you suggested, or you could even use a glass pane to show the zoomed in image. 然后,当收到点击时,您可以按照建议的方式使用JPopupMenu,或者甚至可以使用玻璃窗格显示放大的图像。

I'm guessing you want to achieve something similar to this post by Joshua Marinacci, he has also posted the source here , I would take a look at that. 我猜您想实现与Joshua Marinacci的帖子类似的功能,他也在此处发布了消息源,我将对此进行研究。

I would probably: 我可能会:

  • create some instance of Shape that represents each of your hotspots (could be a plain boring old Rectangle, or see GeneralPath if you need to create fancy shapes) 创建一些表示每个热点的Shape实例(可以是一个无聊的旧Rectangle,如果需要创建奇特的形状,请参见GeneralPath)
  • register a MouseListener which iterates through each of the Shapes and calls its contains() method to see if the clicked coordinate is inside the hotspot in question 注册一个MouseListener,该鼠标迭代每个Shapes并调用其contains()方法,以查看单击的坐标是否在所讨论的热点内

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM