How to create an image map using Java Swing?(如何使用 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 创建图像映射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用 Java Swing 创建图像映射?


- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01