1

I need to show to the user an image and led him to select with the mouse an area. So I create and show a JFrame in which I put a JLabel initizialized with an ImageIcon. After I add a and override a mouseEventListner on the JLabel. My work plan was to get the first and the second click of the mouse to get the relatives points to use as edges identify the selected area. I correctly get the points. But I am not able to correctly change the previous JLabel with the new one with the rectangle painted.

It is important to store the infomation of the selected area on the image (on its pixel) showed in the JFrame because in a second moment I need to manage this information at pixels level.

So here is were I initialize the JFrame:

 JFrame frame = new JFrame();
                ImageIcon icon = new ImageIcon(imgPath);
                JLabel label = new JLabel(icon);
                frame.add(label);
                frame.setTitle(imgPath.substring(imgPath.lastIndexOf("\\")+1));
                frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
                frame.pack();
                frame.setVisible(true);
                frame.setResizable(false); 

                //function to select area by mouse click
                addHandlerToDraw(label,frame,icon);

This is the funcion I call:

private void addHandlerToDraw(JLabel label, JFrame frame, ImageIcon img) {

    label.addMouseListener(new MouseAdapter() {

        Point start = new Point();
        Rectangle captureRect;
        int k=0;
        @Override
        public void mouseClicked(java.awt.event.MouseEvent e) {
            if(k==0) {
                start = e.getPoint();
                k=1;
            } else {
                k = 0;
                Point end = e.getPoint();
                captureRect = new Rectangle(start, new Dimension(end.x-start.x, end.y-start.y));
            }
            repaint(captureRect, frame, img, label);
            label.repaint();
        }
    });

And finally this is the function thanks to I want to switch the previous image (without the selected area) with the image with the selected area showed:

private void repaint(Rectangle rect, JFrame frame, ImageIcon img, JLabel label) {

    BufferedImage bi = new BufferedImage(
            img.getIconWidth(),
            img.getIconHeight(),
            BufferedImage.TYPE_INT_RGB);

    Graphics2D g1 = bi.createGraphics();
    g1.setColor(new Color(200,30,30,45));
    if(rect != null) {
        g1.drawRect(rect.x, rect.y, rect.width, rect.height);
        frame.remove(label);
        frame.add(new JLabel(new ImageIcon(bi)));
        frame.setVisible(true);
    }}

As I sed before even if I correctly compute the rectangle I'm not able to store and show it in the new JLabel and so in the JFrame. Where I'm wrong?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Giorgio Di Rocco
  • 119
  • 1
  • 2
  • 7
  • 1
    1. I wouldn't use a `JLabel` for this, I'd use a custom component which can paint a `BufferedImage`, see [Performing Custom Painting](https://docs.oracle.com/javase/tutorial/uiswing/painting/index.html) for more details. 2. I don't know why you're trying to create the label, when you could just replace the icon with `setIcon` – MadProgrammer May 03 '19 at 08:21
  • 1
    For [example](https://stackoverflow.com/questions/13948122/drawing-a-bounding-rectangle-to-select-what-area-to-record/13948198#13948198), [example](https://stackoverflow.com/questions/22645172/java-draws-rectangle-one-way-not-both/22645343#22645343), [example](https://stackoverflow.com/questions/26599834/select-copy-and-paste-images/26600405#26600405) – MadProgrammer May 03 '19 at 08:28

0 Answers0