Login  Register

Re: ImagePlus Window inside JFrame

Posted by Juanjo Vega on Aug 09, 2011; 12:15pm
URL: http://imagej.273.s1.nabble.com/ImagePlus-Window-inside-JFrame-tp3683566p3683570.html

Ahsan,

I have implemented user interfaces like those, and my recommendation is to use/extend the ImageWindow class. So you can add stuff to the ImagePlus window while keeping the normal use of it.

As ImageWindow is AWT, you might experiment some problems if you use SWING components. I try to stay in AWT whenever it's possible, but if not (once I needed to use a JTabbedPane), try to place your swing components in a Panel, and then add that panel to the imagewindow.

I have adapted the following code from one of my recent plugins (where users had to select an area, and after that, a point inside the sub-image). Try it, and feel free to use it if you like this "style".


public class CropTest {

    public static void main(String args[]) {
        new ImageJ();
        IJ.run("Clown (14K)");

        JFrame frame = new JFrame("Crop TEST");

        JButton button = new JButton("Crop");
        button.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ae) {
                crop();
            }
        });

        frame.add(button);

        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    private static void crop() {
        final ImagePlus imp = WindowManager.getCurrentImage();

        if (imp != null) {
            final Roi roi = imp.getRoi();
            if (roi != null) {
                if (roi.isArea()) {
                    // Gets CenterOfMass from image ROI
                    final ImagePlus subImp = new ImagePlus("selected region", imp.getProcessor().crop());
                    subImp.copyScale(imp);  // Copies calibration info: pixels size, etc.

                    // Adds a button to store points.
                    Button bOk = new Button("Close");
                    bOk.addActionListener(new ActionListener() {

                        @Override
                        public void actionPerformed(ActionEvent ae) {
                            subImp.close();
                        }
                    });

                    // Creates a new window as it will be null until "show()",
                    ImageWindow window = new ImageWindow(subImp);
                    // Adds button.
                    window.add(bOk);
                    window.pack();

                    subImp.show();
                } else {
                    IJ.error("ROI is not an area.");
                }
            } else {
                IJ.error("No ROI selected.");
            }
        } else {
            IJ.error("There are no images open.");
        }
    }
}

On Aug 9, 2011, at 12:45 PM, ashamim wrote:

> Juanjo
>
> Thanks for the reply. I am implementing user interface for one plugin. One
> requirement is that the user might have to crop some part of the image
> before analysing it. I was wondering it would be better if  I could display
> the image within the frame of UI and the user be able to crop it in there
> rather than in a seperate window.
>
>
> Regards
>
> Ahsan
>
> -----
> Ahsan
> --
> View this message in context: http://imagej.588099.n2.nabble.com/ImagePlus-Window-inside-JFrame-tp6667443p6667646.html
> Sent from the ImageJ mailing list archive at Nabble.com.

------------------------------------------------------------
Juanjo Vega ([hidden email])

Unidad de Biocomputación. Laboratorio B-13.
Centro Nacional de Biotecnología. CNB-CSIC.
C\ Darwin, 3. Campus de Cantoblanco.
Universidad Autónoma de Madrid.
28049, Madrid, Spain.

http://www.cnb.csic.es
http://www.biocomp.cnb.csic.es

+34 91 585 4510

"Las mejores almas son capaces de los mayores vicios como de las mayores
virtudes, y aquellos que caminan despacio por el camino recto pueden
llegar más lejos que los que corren pero se apartan de él." - Discurso
del Método, René Descartes.