简体   繁体   中英

OpenImaj - Using a MouseListener with JFrame

I'm attempting to add mouse listening capabilities to a JFrame that displays an MBFImage and the mouse events do absolutely nothing. I'm not sure if the events are not firing or if they are and not being caught because I am doing something wrong...

The image shows up in the JFrame just fine, however moving the mouse around over the image, clicking, moving, dragging, etc. does not result in any activity.

NOTE 1 I've discovered that if I add the mouselistener to a JPanel and then (in this specific order) draw the image and then add the the JPanel to the JFrame, that the mouse listener catches the events, but ONLY listens outside of the image. It draws a minimum size window which I need to resize. Any mouse movement over the image does not seem to fire / catch any events.

NOTE 2 If I add the panel to the JFrame and then draw the image, the window size is just fine, however the mouse listener no longer work.

Can anyone shed any light?

Here is the relevant portion of my code:

private JFrame displayImage(final MyAppImage image, final MyAppImage.DetectLevel level, String title) {
    MBFImage mbfImg = image.drawDetections(level);  //draws face detection boxes
    JFrame imgFrame = new JFrame(title);
    DisplayUtilities.display(mbfImg, imgFrame);

    imgFrame.addMouseListener(new MouseListener() {

        @Override
        public void mouseClicked(MouseEvent e) {

        }

        @Override
        public void mousePressed(MouseEvent e) {

        }

        @Override
        public void mouseReleased(MouseEvent e) {

        }

        @Override
        public void mouseEntered(MouseEvent e) {
            System.out.println("here");
            if (level == MyAppImage.DetectLevel.filtered) {
                if (image.checkPoints(e.getX(), e.getY(), level) != null) {
                    System.out.println("YES");
                }
                else {
                    System.out.println("NO!");
                }
            }
            else {
                System.out.println("Huh?");
            }
        }

        @Override
        public void mouseExited(MouseEvent e) {

        }
    });

    imgFrame.addMouseMotionListener(new MouseMotionListener() {

        @Override
        public void mouseDragged(MouseEvent e) {
            System.out.println("hello");
        }

        @Override
        public void mouseMoved(MouseEvent e) {
            System.out.println("here");
            if (level == MyAppImage.DetectLevel.filtered) {
                if (image.checkPoints(e.getX(), e.getY(), level) != null) {
                    System.out.println("YES");
                }
                else {
                    System.out.println("NO!");
                }
            }
            else {
                System.out.println("Huh?");
            }
        }
    });
    return imgFrame;

}

Exactly what @MadProgrammer commented - when you call DisplayUtilities.display(mbfImg, imgFrame); it creates an ImageComponent inside your JFrame that is itself a MouseListener .

You should be able to add a MouseListener to the ImageComponent directly however.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM