Java 如何使图像相对于其帧进行缩放(ImageJ)
所以我想我问了一个新问题,这是我旧问题的延伸。因此,我能够将ImageJ窗口中的内容取出,放入桌面窗格中的内部框架。但图像不会随着帧大小的放大而缩放。我在ImageJ中找到了几种主要使用zoom类的方法,但它无法缩放以适应帧。所以我想知道是否有人知道我做错了什么。放大和缩小都起作用了,但“缩放”和“缩放”的设置不起作用,我不知道为什么。先谢谢你 以下是我的部分代码:Java 如何使图像相对于其帧进行缩放(ImageJ),java,user-interface,imagej,Java,User Interface,Imagej,所以我想我问了一个新问题,这是我旧问题的延伸。因此,我能够将ImageJ窗口中的内容取出,放入桌面窗格中的内部框架。但图像不会随着帧大小的放大而缩放。我在ImageJ中找到了几种主要使用zoom类的方法,但它无法缩放以适应帧。所以我想知道是否有人知道我做错了什么。放大和缩小都起作用了,但“缩放”和“缩放”的设置不起作用,我不知道为什么。先谢谢你 以下是我的部分代码: public class CustomGui extends ImageWindow implements InternalFra
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类的一部分):
/**此插件实现图像/缩放子菜单中的命令*/
公共类Zoom实现插件{
ImagePlus imp;
公共变焦(ImagePlus img){
imp=img;
}
公共无效运行(字符串参数){
//ImagePlus imp=WindowManager.getCurrentImage();
//ImagePlus imp=img;
if(imp==null)
{IJ.noImage();return;}
ImageCanvas ic=imp.getCanvas();
如果(ic==null)返回;
点loc=ic.getCursorLoc();
如果(!ic.cursorOverImage()){
矩形srcRect=ic.getSrcRect();
loc.x=srcRect.x+srcRect.width/2;
loc.y=srcRect.y+srcRect.height/2;
}
int x=集成电路屏幕x(位置x);
int y=集成电路屏幕y(位置y);
如果(参数等于(“in”)){
放大倍数(x,y);
如果(ic.get放大()在中,我看不到任何构造函数接受参数。是否确实要导入并使用
标准的ImageJ GUI允许使用ij.plugin.Zoom
类进行缩放。在中运行以下JavaScript片段,以在正常模式和缩放模式之间切换:
ImageJ主窗口的状态栏(以及Zoom.java
的源代码)还提供提示,在调整窗口大小时按alt键将导致图像缩放到窗口大小
很抱歉,我没有花时间摆弄你的代码:这不是一个真正的问题。如果你提供一个说明你的问题的例子,你会让其他人更容易帮助你。@JanEglinger我添加了代码,我尝试了几件事。我希望它不会很混乱。是的,我忘了说这个,但我为它创建了一个构造函数,因为我需要通过我的I在我的自定义gui中打开的法师。我也尝试了alt,但它没有做任何事情。我不知道这是否是问题,但我没有使用图像窗口,我有自己的内部框架。嗯,我想我必须把它留给其他人来帮助你。如果你创建了自己的自定义gui,这对我来说有点太复杂了。但是,对于为了避免浪费那些愿意提供帮助的人的时间,我建议您也提供定制的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);
}
zm = new Zoom();
zm.run("scale");