如何将超链接插入BuffereImage(java)的指定区域?

如何将超链接插入BuffereImage(java)的指定区域?,java,jbutton,bufferedimage,Java,Jbutton,Bufferedimage,我在绘图面板上显示了一个java BuffereImage。可以说,我如何“激活”该图像的特定区域,使其成为一个可点击区域,并附加一个特定的超链接?谢谢。您有很多选择 使用“地图图像”定义图像的可单击位置。这将需要第二个图像的大小与第一个图像的大小相同,并带有可在运行时识别的绘制区域,以确定它们是否可单击 定义一系列表示可单击位置的“形状”。这些区域可以命名并保存到文件中,从而能够独立于代码定义这些区域 下面的示例使用这两种方法 “大师”形象 “地图”图像 将鼠标/运动监听器添加到绘图窗格中

我在绘图面板上显示了一个java BuffereImage。可以说,我如何“激活”该图像的特定区域,使其成为一个可点击区域,并附加一个特定的超链接?谢谢。

您有很多选择

  • 使用“地图图像”定义图像的可单击位置。这将需要第二个图像的大小与第一个图像的大小相同,并带有可在运行时识别的绘制区域,以确定它们是否可单击
  • 定义一系列表示可单击位置的“形状”。这些区域可以命名并保存到文件中,从而能够独立于代码定义这些区域
  • 下面的示例使用这两种方法

    “大师”形象

    “地图”图像


    将鼠标/运动监听器添加到
    绘图窗格中
    ,当鼠标移过/单击相关区域时,执行一些任务…首先,感谢您花费大量精力编写这些内容。不幸的是,那个代码对我来说有点太多了。如果您能帮助理解,我们将不胜感激。你的代码不仅仅是建立一个可点击的区域吗?有没有办法把它归结为一些简单的东西,比如识别像素位置,画框,用特定的超链接使框可以点击?谢谢
    public class TestImageMap {
    
        private BufferedImage master;
        private BufferedImage masterMap;
    
        public static void main(String[] args) {
            new TestImageMap();
        }
    
        public TestImageMap() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    }
    
                    try {
    
                        master = ImageIO.read(getClass().getResource("/Master.png"));
                        masterMap = ImageIO.read(getClass().getResource("/MasterMap.png"));
    
                        JFrame frame = new JFrame();
                        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                        frame.setLayout(new GridLayout(2, 1));
                        frame.add(new MapPane());
                        frame.add(new CoordPane());
                        frame.pack();
                        frame.setLocationRelativeTo(null);
                        frame.setVisible(true);
    
                    } catch (Exception exp) {
                        exp.printStackTrace();
                        System.exit(0);
                    }
                }
            });
        }
    
        public void sendMoney() {
            JOptionPane.showMessageDialog(null, "Sending money :D");
        }
    
        public void sendMoreMoney() {
            JOptionPane.showMessageDialog(null, "Sending ALL your money 8D");
        }
    
        public abstract class AbstractImagePane extends JPanel {
    
            public AbstractImagePane() {
    
                MouseAdapter handler = new MouseAdapter() {
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        doMouseClicked(e);
                    }
    
                    @Override
                    public void mouseMoved(MouseEvent e) {
                        doMouseMoved(e);
                    }
                };
    
                addMouseMotionListener(handler);
                addMouseListener(handler);
            }
    
            @Override
            public Dimension getPreferredSize() {
                return master == null ? super.getPreferredSize() : new Dimension(master.getWidth(), master.getHeight());
            }
    
            protected void doMouseClicked(MouseEvent evt) {
                if (evt.getButton() == MouseEvent.BUTTON1) {
                    if (evt.getClickCount() == 1) {
                        Point p = evt.getPoint();
                        if (containsMoney(p)) {
                            sendMoney();
                        } else if (containsMoreMoney(p)) {
                            sendMoreMoney();
                        }
                    }
                }
            }
    
            protected void doMouseMoved(MouseEvent evt) {
                Cursor cursor = Cursor.getDefaultCursor();
                Point p = evt.getPoint();
                if (containsMoney(p) || containsMoreMoney(p)) {
                    cursor = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
                }
                setCursor(cursor);
            }
    
            protected abstract boolean containsMoney(Point p);
            protected abstract boolean containsMoreMoney(Point p);
    
            protected Point normalize(Point p) {
                Point offset = getImageOffset();
                Point norm = new Point();
                norm.x = p.x - offset.x;
                norm.y = p.y - offset.y;
                return norm;
            }
    
            protected Point getImageOffset() {
                int width = getWidth() - 1;
                int height = getHeight() - 1;
                int x = (width - master.getWidth()) / 2;
                int y = (height - master.getHeight()) / 2;
    
                return new Point(x, y);
    
            }
    
            @Override
            public void paint(Graphics g) {
                super.paint(g);
                if (master != null) {
                    Point offset = getImageOffset();
                    g.drawImage(master, offset.x, offset.y, this);
                }
            }
        }
    
        public class MapPane extends AbstractImagePane {
    
            private Rectangle moneyBounds = new Rectangle(16, 24, 139, 36);
            private Rectangle moreMoneyBounds = new Rectangle(16, 70, 139, 34);
    
            @Override
            protected boolean containsMoney(Point p) {
                return moneyBounds.contains(normalize(p));
            }
    
            @Override
            protected boolean containsMoreMoney(Point p) {
                return moreMoneyBounds.contains(normalize(p));
            }
        }
    
        public class CoordPane extends AbstractImagePane {
    
            protected boolean contains(Point p, int rgb) {
                Point norm = normalize(p);
                return masterMap.getRGB(norm.x, norm.y) == rgb;
            }
    
            @Override
            protected boolean containsMoney(Point p) {
                int white = new Color(255, 255, 255).getRGB();
                return contains(p, white);
            }
    
            @Override
            protected boolean containsMoreMoney(Point p) {
                int red = new Color(255, 0, 0).getRGB();
                return contains(p, red);
            }
        }
    }