Posted by
vinocell on
URL: http://imagej.273.s1.nabble.com/zoom-not-working-tp3691020.html
Hello,
I just wrote a simple plugin to have my ImageCanvas embedded into a JFrame
with the ability to zoom from a square ROI.
This is why I created the HiddenImageWindow class with an empty redefined
show() and a setVisible(false) call in the constructor, to make sure this
window is not going to be displayed (while being able to grab its canvas).
After this trick I just copied the zoomToSelection() function in
http://rsb.info.nih.gov/ij/source/ij/plugin/Zoom.java to be able to run it
from a square ROI.
However it doesn't work (especially after a second zoom) and the canvas
turns to grey. Any idea why this code printed below fails ?
Thanks,
Vinocell.
import ij.*;
import ij.process.*;
import ij.gui.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import ij.io.OpenDialog;
import ij.io.Opener;
import ij.plugin.*;
import ij.plugin.PlugIn;
import java.awt.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import javax.swing.*;
public class JFrameZoom_ implements PlugIn, MouseListener {
/** GUI main window */
protected JFrame _mainWindow;
/** GUI subpanel */
protected JPanel _pan;
/** DICOM, jpeg, tiff, ... phase images. */
protected ImagePlus _impPhase;
/** Window phase images. */
protected ImageWindow _winPhase;
/** Canvas containing one of the phase images. */
protected ImageCanvas _canvasPhase;
public void run(String arg) {
// Input files
OpenDialog od = new OpenDialog("Open Image Sequence...", arg);
String directory = od.getDirectory();
String fileName = od.getFileName();
Opener op = new Opener();
// Create ImagePlus & Canvas & HiddenImageWindow
_impPhase = op.openImage(directory,fileName);
_canvasPhase = new ImageCanvas(_impPhase);
_winPhase = new HiddenImageWindow(_impPhase,_canvasPhase);
_canvasPhase.addMouseListener(this);
_pan = new JPanel();
_pan.add(_canvasPhase);
//Display the window.
_mainWindow = new JFrame("FLUX");
_mainWindow.add(_pan);
_mainWindow.pack();
_mainWindow.setVisible(true);
}
/** Implement the interface MouseListener */
public void mouseReleased(MouseEvent e) {
if (e.getSource().equals(_canvasPhase) &&
_impPhase.getRoi()!= null &&
IJ.shiftKeyDown()) {
zoomToSelection(_impPhase, _canvasPhase);
_impPhase.killRoi();
}
}
/** Implement the interface MouseListener */
public void mousePressed(MouseEvent e) {}
/** Implement the interface MouseListener */
public void mouseEntered(MouseEvent e) {}
/** Implement the interface MouseListener */
public void mouseExited(MouseEvent e) {}
/** Implement the interface MouseListener */
public void mouseClicked(MouseEvent e) {}
// Copy of the zoom plugin as defined in ImageJ
void zoomToSelection(ImagePlus imp, ImageCanvas ic) {
Roi roi = imp.getRoi();
ic.unzoom();
if (roi==null) return;
Rectangle w = imp.getWindow().getBounds();
Rectangle r = roi.getBounds();
double mag = ic.getMagnification();
int marginw = (int)((w.width - mag * imp.getWidth()));
int marginh = (int)((w.height - mag * imp.getHeight()));
int x = r.x+r.width/2;
int y = r.y+r.height/2;
mag = ic.getHigherZoomLevel(mag);
while(r.width*mag<w.width - marginw && r.height*mag<w.height -
marginh) {
ic.zoomIn(ic.screenX(x), ic.screenY(y));
double cmag = ic.getMagnification();
if (cmag==32.0) break;
mag = ic.getHigherZoomLevel(cmag);
w = imp.getWindow().getBounds();
}
}
/**
* The class HiddenImageWindow hides the frame that IJ uses to display
the image.
*/
private class HiddenImageWindow extends ImageWindow {
/** For the serialization */
private static final long serialVersionUID = 0L;
public HiddenImageWindow(ImagePlus imp, ImageCanvas ic) {
super(imp, ic);
setVisible(false);
}
public void show() {}
}
}