如何使用 Java Swing 创建图像映射? [英] How to create an image map using Java Swing?

查看:26
本文介绍了如何使用 Java Swing 创建图像映射?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用显示背景图像的 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.

我正在考虑扩展 JPanel 以包含图像引用,并通过 paintComponent(g) 方法进行绘制.这部分我已经完成了,代码如下:

I was thinking of extending JPanel to include an image reference and have that drawn thru the paintComponent(g) method. 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);
    }

谁能推荐我如何在定义的热点上监听/响应鼠标点击?有人可以另外推荐一种显示弹出窗口的方法吗?我的直觉是扩展 JPopupMenu 让它显示图像,类似于上面的代码.

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.

感谢您的帮助!

推荐答案

要监听鼠标点击,实现 MouseListener 接口,并将其添加到您的面板中.然后,当收到点击时,您可以按照您的建议使用 JPopupMenu,或者您甚至可以使用玻璃窗格来显示放大的图像.

To listen to the mouse clicks implement the MouseListener interface, and add it to your panel. 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.

我猜你想实现类似于这个 post 由 Joshua Marinacci 发布,他还发布了源 这里,我去看看.

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.

这篇关于如何使用 Java Swing 创建图像映射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆