package com.mycodefu.javacv.fun.display;

import com.mycodefu.javacv.fun.converters.MatrixToBufferedImage;
import org.opencv.core.Mat;

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;

import static java.lang.Math.max;

/**
 * Create a Java Swing window in the matrix dimensions and draw images on it.
 *
 * Assumes that the image dimensions will not vary. Reuses the buffers to improve the efficiency based on this assumption.
 *
 * Created by lthompson on 2/05/15.
 */
public class DisplayImages {
    protected JFrame frame;
    private final MatrixToBufferedImage converter;

    public DisplayImages() {
        frame = null;
        converter = new MatrixToBufferedImage();
    }

    public void draw(Mat imageMatrix) {
        initializeWindow(imageMatrix.width(), imageMatrix.height());

        final BufferedImage image = converter.convert(imageMatrix);

        frame.getGraphics().drawImage(image, 0, 0, frame.getWidth(), frame.getHeight(), (img, infoflags, x, y, width, height) -> false);
    }

    protected void initializeWindow(int width, int height) {
        if (frame == null) {
            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

            frame = new JFrame();
            frame.setBounds(0, 0, max(width, screenSize.width), max(height, screenSize.height));
            frame.setVisible(true);
        }
    }

}

@@ -190,27 +190,172 @@
 awt.
-image.BufferedImage
+event.MouseEvent;%0Aimport java.awt.event.MouseListener;%0Aimport java.awt.image.BufferedImage;%0Aimport java.util.stream.Collectors;%0Aimport java.util.stream.DoubleStream
 ;%0A%0Ai
@@ -746,16 +746,46 @@
 nverter;
+%0A    private Mat currentImage;
 %0A%0A    pu
@@ -991,24 +991,61 @@
 height());%0A%0A
+        currentImage = imageMatrix;%0A%0A
         fina
@@ -1547,16 +1547,16 @@
 ight));%0A
-
         
@@ -1582,16 +1582,900 @@
 e(true);
+%0A%0A            frame.addMouseListener(new MouseListener() %7B%0A                @Override%0A                public void mouseClicked(MouseEvent e) %7B%0A                    final double%5B%5D pixel = currentImage.get(e.getY(), e.getX());%0A                    final String pixelString = DoubleStream.of(pixel).mapToObj(Double::toString).collect(Collectors.joining(%22,%22));%0A%0A                    System.out.println(%22Mouse clicked! %22 + e + %22, %22 + pixelString);%0A                %7D%0A%0A                @Override%0A                public void mousePressed(MouseEvent e) %7B%0A%0A                %7D%0A%0A                @Override%0A                public void mouseReleased(MouseEvent e) %7B%0A%0A                %7D%0A%0A                @Override%0A                public void mouseEntered(MouseEvent e) %7B%0A%0A                %7D%0A%0A                @Override%0A                public void mouseExited(MouseEvent e) %7B%0A%0A                %7D%0A            %7D);
 %0A       

Add mouse click handler to the jFrame