Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8872651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:16:16+00:00 2026-06-14T18:16:16+00:00

I’ve got a java BufferedImage displayed on a DrawingPanel. How do I ‘activate’, so

  • 0

I’ve got a java BufferedImage displayed on a DrawingPanel. How do I ‘activate’, so to speak, a specific area of that image so that it is a clickable region with a specific hyperlink attached to it? Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T18:16:18+00:00Added an answer on June 14, 2026 at 6:16 pm

    You have a number of choices…

    1. Use map image to define the clickable location of the image. This will require to have a second image the same size as the first with painted regions you can identify at run time to determine if they clickable or not.
    2. Define a series of “shapes” which represent the clickable locations. These could be named and saved to a file, allowing the ability to define these regions independently of the code.

    The following example uses both;

    The “Master” image

    enter image description here

    The “Map” image

    enter image description here

    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);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I have a text area in my form which accepts all possible characters from
I know there's a lot of other questions out there that deal with this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.