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 9123481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:21:02+00:00 2026-06-17T06:21:02+00:00

So I’m trying to find a way to modify an image in Java. In

  • 0

So I’m trying to find a way to modify an image in Java. In other words, if user clicks on the image, a mark will be put at the point where the user just clicked.
I have an ImageIcon which I put in a JLabel.
So far, the approach I took was to use JLayeredPanel to put another JPanel on top of the JLabel and draw on this JPanel:

//...
ImageIcon icon = new ImageIcon("foo.jpg");
JLabel lb = new JLabel(icon);
JPanel glass = new JPanel();
lb.setBounds(0, 0, 100, 100);
glass.setBounds(0, 0, 100, 100);
glass.setOpaque(false);
LayeredPane container = new LayeredPane();
container.add(lb, 1);
container.add(glass, 2);

//...

But this way doesn’t seem to work. I never see the background image (the image in lb).
So I was wondering if I’m even on the right track at all? Or is there a cleaner way to achieve this?

  • 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-17T06:21:04+00:00Added an answer on June 17, 2026 at 6:21 am

    There’s nothing wrong with using a JLayeredPane or the glass pane for something like this, personally, I find it troublesome, because in a large application, you tend to want to use these layers for any number of things, so it becomes very complicated very fast.

    I prefer to keep it “in the family” so to speak…

    Personally, I would use a custom component. This isolates the work flow to a very particular location and makes it easier to provide the customisations that you might like…

    enter image description here

    public class MarkImage {
    
        public static void main(String[] args) {
            new MarkImage();
        }
    
        public MarkImage() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    }
    
                    JFrame frame = new JFrame("Test");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new BorderLayout());
                    frame.add(new TestPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
    
            });
        }
    
        public class TestPane extends JPanel {
    
            private BufferedImage background;
            private List<Point> clickPoints;
    
            public TestPane() {
                clickPoints = new ArrayList<>(25);
                try {
                    background = ImageIO.read(getClass().getResource("/Miho_Small.png"));
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
    
                addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        clickPoints.add(e.getPoint());
                        repaint();
                    }
                });
            }
    
            @Override
            public Dimension getPreferredSize() {
                return background == null ? super.getPreferredSize() : new Dimension(background.getWidth(), background.getHeight());
            }
    
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                if (background != null) {
                    int x = (getWidth() - background.getWidth()) / 2;
                    int y = (getHeight() - background.getHeight()) / 2;
                    g.drawImage(background, x, y, this);
                }
                g.setColor(Color.RED);
                for (Point p : clickPoints) {
                    g.fillOval(p.x - 4, p.y - 4, 8, 8);
                }
            }
    
        }
    
    }
    

    I’d also consider using JXLayer (AKA JLayer in Java 7). This is best described as a glass pane for components (on steroids). Check out How to decorate components for more details…

    Updated with JLayer Example

    This is an example using Java 7’s JLayer. There are some slight differences between JLayer and JXLayer, but it wouldn’t take much to convert it…

    (Sorry, couldn’t resist the temptation of having ago)

    enter image description here

    public class MarkLayer {
    
        public static void main(String[] args) {
            new MarkLayer();
        }
    
        public MarkLayer() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    }
    
                    try {
                        JFrame frame = new JFrame("Testing");
                        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                        frame.setLayout(new GridBagLayout());
    
                        JLabel label = new JLabel(new ImageIcon(ImageIO.read(getClass().getResource("/Miho_Small.png"))));
                        LayerUI<JLabel> layerUI = new MarkLayerUI();
                        JLayer<JLabel> layer = new JLayer<>(label, layerUI);
    
                        frame.add(layer);
                        frame.pack();
                        frame.setLocationRelativeTo(null);
                        frame.setVisible(true);
                    } catch (Exception exp) {
                        exp.printStackTrace();
                    }
                }
            });
        }
    
        public class MarkLayerUI extends LayerUI<JLabel> {
    
            private Map<JLayer, List<Point>> mapPoints;
    
            public MarkLayerUI() {
                mapPoints = new WeakHashMap<>(25);
            }
    
            @Override
            public void installUI(JComponent c) {
                System.out.println("install");
                super.installUI(c);
                JLayer layer = (JLayer) c;
                layer.setLayerEventMask(AWTEvent.MOUSE_EVENT_MASK);
            }
    
            @Override
            public void uninstallUI(JComponent c) {
                super.uninstallUI(c);
                mapPoints.remove((JLayer) c);
            }
    
            @Override
            protected void processMouseEvent(MouseEvent e, JLayer<? extends JLabel> l) {
                if (e.getID() == MouseEvent.MOUSE_CLICKED) {
    
                    List<Point> points = mapPoints.get(l);
                    if (points == null) {
                        points = new ArrayList<>(25);
                        mapPoints.put(l, points);
                    }
                    Point p = e.getPoint();
                    p = SwingUtilities.convertPoint(e.getComponent(), p, l);
                    points.add(p);
                    l.repaint();
    
                }
            }
    
            @Override
            public void paint(Graphics g, JComponent c) {
                Graphics2D g2d = (Graphics2D) g.create();
                super.paint(g2d, c);
                g2d.setColor(Color.BLUE);
                g2d.drawRect(0, 0, c.getWidth() - 1, c.getHeight() - 1);
                List<Point> points = mapPoints.get((JLayer) c);
                if (points != null && points.size() > 0) {
                    g2d.setColor(Color.RED);
                    for (Point p : points) {
                        g2d.fillOval(p.x - 4, p.y - 4, 8, 8);
                    }
                }
                g2d.dispose();
            }
        }
    }
    

    The blue border is renderer as part of the layer, this gives you a guide as to where you can click – I did this for testing and demonstration purposes

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am trying to loop through a bunch of documents I have to put
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group

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.