【问题标题】:How to make an image zoom with respect to its frame (ImageJ)如何使图像相对于其框架进行缩放(ImageJ)
【发布时间】:2014-05-17 03:23:14
【问题描述】:

所以我想我问一个新问题,这是我旧问题的延伸。因此,我能够将 ImageJ 窗口中的内容放入桌面窗格内的 JInternal 框架中。但图像不会随着帧大小的放大而缩放。我发现了几种主要使用 ImageJ 中的缩放类的方法,但它不会缩放以适应框架。所以我想知道是否有人知道我做错了什么。放大工作正常,缩小工作也有效,但缩放和缩放设置不起作用,我不知道为什么。预先感谢。

这是我的代码的一部分:

public class CustomGui extends ImageWindow implements InternalFrameListener, ComponentListener, ActionListener{

    public CustomGui(ImagePlus imp, String title, JDesktopPane desktop, final JMenuItem save, JWindow win, JMenuItem fft) {
        super(imp);
        // TODO Auto-generated constructor stub
        setCall();
        img = imp;
        save.setEnabled(true);
        fft.setEnabled(true);
        //this.title = title;
        this.win = win;
        this.fft = fft;
        this.save = save;


        JPanel panel = new JPanel();

        ImageCanvas c = new ImageCanvas(imp);
        c.getImage();

        //panel2.add(new JLabel(new ImageIcon(c.getImage())));
         m = new ImageWindow(img);

        Image n = new Image();
        frame = new MyInternalFrame(title, img, save,m);
        //ImageCanvas c = m.getCanvas();
        ImagePlus im = new ImagePlus();
        im.setImage(img);


        //ImageRoi roi = ImageRoi();
        //panel.add(c);
       //   frame.add(m.getContentPane());
        m.centerNextImage();

      //This is where I try to use the zoom class.
        img.getCanvas().setScaleToFit(true);

        //ImageCanvas nu = new ImageCanvas(img);
        //nu.setScaleToFit(true);

     //I set arg to "scale" but it doesn't work, even when I put it in component resized listener.
        Zoom z = new Zoom(img);
        String arg = "orig";
        z.run(arg);

        //m.setLocationRelativeTo(frame);
        //m.setLocationAndSize(true);
        //m.setLocationAndSize(frame.getWidth(), frame.getHeight(), frame.getWidth(), frame.getHeight());
        panel.add(m.getCanvas());
        panel.setBackground(Color.white);

        frame.add(panel);
        frame.setVisible(true); 
        frame.setAutoscrolls(true);
        frame.setAutoscrolls(true);
        desktop.add(frame);
        try {
                frame.setSelected(true);
        } catch (java.beans.PropertyVetoException e) {

        }
       frame.addInternalFrameListener(this);  
       frame.addComponentListener(this);
       //win.add(desktop);

    }

    public void actionPerformed(ActionEvent e){
        if(e.getActionCommand().equals(SHOW)){
            save.setEnabled(true);
            frame.addInternalFrameListener(this);
        }

        System.out.println("beingg called");
    }

      public void setCall(){
            called = true;
        }

        public void setCallF(){
        called = false;
        }

        public boolean getCall(){
        return called;
        }



      //This is where the internal frame is resized.
    @Override
    public void componentResized(ComponentEvent arg0) {
        // TODO Auto-generated method stub
        Rectangle r = frame.getBounds();
    //      int h = (int) r.getHeight();
    //      int w = (int) r.getWidth();
    //      m.resize(w, h);
        //m.getCanvas().unzoom();
        //m.setLocationAndSize((int)r.getWidth(), (int)r.getHeight(), (int)r.getWidth(), (int)r.getHeight());
        m.getCanvas().fitToWindow(r);
        System.out.println("resized- the real one");

    }

    @Override
    public void componentShown(ComponentEvent arg0) {
        // TODO Auto-generated method stub
        frame.show();

    }

    }

我还为缩放类添加了一个构造函数(这是缩放类的一部分):

    /** This plugin implements the commands in the Image/Zoom submenu. */
   public class Zoom implements PlugIn{

ImagePlus imp;
public Zoom (ImagePlus img){
    imp = img;
}

public void run(String arg) {
    //ImagePlus imp = WindowManager.getCurrentImage();
    //ImagePlus imp = img;
    if (imp==null)
        {IJ.noImage(); return;}
    ImageCanvas ic = imp.getCanvas();
    if (ic==null) return;
    Point loc = ic.getCursorLoc();
    if (!ic.cursorOverImage()) {
        Rectangle srcRect = ic.getSrcRect();
        loc.x = srcRect.x + srcRect.width/2;
        loc.y = srcRect.y + srcRect.height/2;
    }
    int x = ic.screenX(loc.x);
    int y = ic.screenY(loc.y);
    if (arg.equals("in")) {
        ic.zoomIn(x, y);
        if (ic.getMagnification()<=1.0) imp.repaintWindow();
    } else if (arg.equals("out")) {
        ic.zoomOut(x, y);
        if (ic.getMagnification()<1.0) imp.repaintWindow();
    } else if (arg.equals("orig"))
        ic.unzoom();
    else if (arg.equals("100%"))
        ic.zoom100Percent();
    else if (arg.equals("to"))
        zoomToSelection(imp, ic);
    else if (arg.equals("set"))
        setZoom(imp, ic);
    else if (arg.equals("max")) {
        ImageWindow win = imp.getWindow();
        win.setBounds(win.getMaximumBounds());
        win.maximize();
    } if (arg.equals("scale"))
        scaleToFit(imp);
}

【问题讨论】:

  • What have you tried? 如果您提供 minimal code example 来说明您的问题,那么其他人可以更轻松地帮助您。
  • @JanEglinger 我添加了代码,我尝试了几件事。我希望它不要乱七八糟。

标签: java user-interface imagej


【解决方案1】:

在Zoom.java 中,我没有看到任何构造函数接受参数。您确定要导入和使用ij.plugin.Zoom?

标准 ImageJ GUI 允许使用 ij.plugin.Zoom 类进行缩放。在Fiji's script editor 中运行以下 JavaScript sn-p 以在 normal 和 scale to fit 模式之间切换:

zm = new Zoom();
zm.run("scale");

ImageJ 主窗口的状态栏(以及Zoom.java 的来源)也提供了提示,即在调整窗口大小时按alt 将导致图像缩放到窗口大小。

对不起,我没有花时间摆弄你的代码:它不是真正的minimal example。

【讨论】:

  • 是的,我忘了说这个,但我为它创建了一个构造函数,因为我需要传递在我的自定义 gui 中打开的图像。我也尝试了alt,但它没有做任何事情。我不知道这是否是问题,但我不使用图像窗口我有自己的内部框架。
  • 嗯,我想我必须把它留给其他人来帮助你。如果您创建了自己的自定义 GUI,那对我来说有点过于复杂了。但是,为了避免浪费那些愿意提供帮助的人的时间,我建议您也提供 customized Zoom 类,以便其他人可以实际复制您正在做的事情。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-26
  • 2022-01-06
  • 2014-04-25
  • 1970-01-01
相关资源
最近更新 更多