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

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

问题描述

我需要使用显示背景图像的Swing制作图像地图,然后当鼠标悬停(或点击)特定热点时,我需要弹出放大图像并显示它。 / p>

我正在考虑将JPanel扩展为包含图像引用,并通过paintComponent(g)方法绘制。这部分我到目前为止,这里是代码:

 公共类ImagePanel扩展JPanel 
{
private static final long serialVersionUID = 1L;

私人图片图片;

public ImagePanel(图片图片)
{
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);
}

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



谢谢你任何帮助!

解决方案

要听鼠标点击,请执行 MouseListener 界面,并将其添加到您的面板。然后,当收到点击时,您可以按照建议使用JPopupMenu,或者甚至可以使用玻璃窗格显示放大的图像。



我在猜你希望通过Joshua实现与此帖子类似的内容Marinacci,他还发布了这里的来源,我会看一下。


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.

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);
    }

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.

Thanks for any help!

解决方案

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.

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天全站免登陆