gpt4 book ai didi

java - 如何使图像相对于其框架缩放(ImageJ)

转载 作者:行者123 更新时间:2023-12-01 12:58:32 26 4
gpt4 key购买 nike

所以我想我问了一个新问题,这是我的旧问题的延伸。因此,我能够将 ImageJ 窗口中的内容放入桌面 Pane 内的 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();

}

}

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

    /** 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);
}

最佳答案

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

标准 ImageJ GUI 允许使用 ij.plugin.Zoom 类进行缩放。在 Fiji's script editor 中运行以下 JavaScript 片段在正常缩放以适应模式之间切换:

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

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

抱歉,我没有花时间摆弄您的代码:它并不是真正的 minimal example .

关于java - 如何使图像相对于其框架缩放(ImageJ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23699949/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com