用户加载新图像时,是否可以更新显示窗口? [英] Is there a way to update the display window when the user loads a new image?

查看:113
本文介绍了用户加载新图像时,是否可以更新显示窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个加载图像(一个文件中的图像堆栈)并使用控件(按钮)对该图像执行一些操作的程序,并且能够在不更改以前的控件的情况下将该图像加载到框架上.

I am writing a program that loads an image(stack of images in one file) and do some operations on the image with the controls (buttons) and being able to load another image on the frame without having to change previous controls.

该框架在程序开始时具有默认的窗口图像. 有一个按钮可以从所需目录中加载新图像.

The frame has a default window image at the start of the program. There is a button that loads a new image from the wanted directory.

但是,当按下loadImage按钮时,图像永远不会更新.

However the image never gets updated when the loadImage button is pressed.

当加载新图像时,如何使框架更新图像窗口?

How do I get my frame to update the image window when a new image is loaded?

我的JScrollbar也没有出现在我想要的框架中.它没有滚动槽,并且与我的图像窗口没有相同的宽度. 如何更改滚动条的参数以反映这一点?

Also my JScrollbar does not appear in the frame as I want it. It does not scroll trough and does not have the same width as my image window. How do I change the parameters of the scroll bar to reflect that?

这是我的下面的代码:

我尝试从loadImage方法内部的框架中使用重绘功能,但是它不起作用.我还尝试了一些其他方法来在调用加载新图像"按钮时更新框架,但是它不起作用.

I have tried using the repaint function from the frame inside the loadImage method but it would not work. I have also tried some other methods to update the frame when the load new image button is being called but it would not work.

    import org.scijava.command.Command;
    import org.scijava.plugin.Plugin;

    import ij.ImagePlus;
    import ij.gui.NewImage;
    import net.imagej.ImageJ;


    @Plugin(name = "counting_2", type = Command.class, menuPath = "Plugins 
    > counting_2", headless = true)
    public class counting_2 implements Command {

static  ImagePlus img;


public void run(){

    img = null;
    ImagePlus tmpImg = NewImage.createImage("", 521, 609, 100, 8, NewImage.FILL_BLACK);

    img = tmpImg;
    tmpImg.close();
    new MySuperFrameUI();


}

public static void main(String[] args){
    ImageJ ij = new ImageJ();
    ij.launch();

    ij.command().run(counting_2.class, true);


}

}

    import java.awt.Adjustable;
    import java.awt.Dimension;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.AdjustmentEvent;
    import java.awt.event.AdjustmentListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseWheelEvent;
    import java.awt.event.MouseWheelListener;

    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JScrollBar;
    import javax.swing.border.EmptyBorder;

    import ij.ImagePlus;
    import ij.WindowManager;
    import ij.gui.ImageCanvas;
    import ij.gui.ImageWindow;
    import ij.io.OpenDialog;
    import ij.io.Opener;
    import ij.plugin.frame.ContrastAdjuster;


    public class MySuperFrameUI extends JFrame {

private JPanel contentPane;
ImagePlus img;
ImageWindow win;
ImageCanvas can;

int currSlice;

// some variables 

int x;
int y;



public MySuperFrameUI() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Dimension screenSize =  new Dimension(1280 ,720);

    // Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    x=  screenSize.width;
    y= screenSize.height;
    setBounds(0,0,x, y);
    setVisible(true);

    // frame title 
    setTitle("Counting");


    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    GridBagLayout gbl_contentPane = new GridBagLayout();
    gbl_contentPane.columnWidths = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -7, 0, 0, 0, 0, 0, 0, 0, 53, 102, 0, 0, 0, 0, 129, 0, -86, 0};
    gbl_contentPane.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 88, 0};
    gbl_contentPane.columnWeights = new double[]{0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, Double.MIN_VALUE};
    gbl_contentPane.rowWeights = new double[]{0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
    contentPane.setLayout(gbl_contentPane);




    Dimension dim = new Dimension(x, y);

    img = counting_2.img;
    win = new ImageWindow(img);
    img.setWindow(win);
    win.setVisible(false);
    win.setMinimumSize(dim);
    win.setSize(dim);
    can = img.getWindow().getCanvas();
    can.fitToWindow();

    JPanel ViewPane = new JPanel();
    GridBagConstraints gbc_ViewPane = new GridBagConstraints();
    gbc_ViewPane.anchor = GridBagConstraints.SOUTH;
    gbc_ViewPane.gridwidth = 15;
    gbc_ViewPane.gridheight = 23;
    gbc_ViewPane.insets = new Insets(0, 0, 5, 5);
    gbc_ViewPane.gridx = 1;
    gbc_ViewPane.gridy = 8;
    contentPane.add(ViewPane, gbc_ViewPane);
    GridBagLayout gbl_ViewPane = new GridBagLayout();
    gbl_ViewPane.columnWidths = new int[]{0, 0, 0, 0, 0, 222, 0};
    gbl_ViewPane.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    gbl_ViewPane.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
    gbl_ViewPane.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
    ViewPane.setLayout(gbl_ViewPane);




    GridBagConstraints gbc_can = new GridBagConstraints();
    gbc_can.gridwidth = 6;
    gbc_can.insets = new Insets(0, 0, 5, 0);
    gbc_can.gridx = 0;
    gbc_can.gridy = 21;
    ViewPane.add(can, gbc_can);




    JScrollBar SB = new JScrollBar();
    SB.setOrientation(Adjustable.HORIZONTAL);
    SB.setMinimum(1);
    SB.setMaximum(counting_2.img.getNSlices() + 9);

    SB.setVisibleAmount(win.getWidth());
    SB.setValue(img.getCurrentSlice());
    GridBagConstraints gbc_SB = new GridBagConstraints();
    gbc_SB.insets = new Insets(0, 0, 5, 0);
    gbc_SB.gridwidth = 6;
    gbc_SB.gridx = 0;
    gbc_SB.gridy = 22;
    ViewPane.add(SB, gbc_SB);



    JLabel sliceNum = new JLabel(String.valueOf(img.getCurrentSlice()));
    GridBagConstraints gbc_sliceNum = new GridBagConstraints();
    gbc_sliceNum.gridx = 5;
    gbc_sliceNum.gridy = 23;
    ViewPane.add(sliceNum, gbc_sliceNum);



    JPanel controlsPane = new JPanel();
    GridBagConstraints gbc_controlsPane = new GridBagConstraints();
    gbc_controlsPane.anchor = GridBagConstraints.NORTH;
    gbc_controlsPane.gridwidth = 7;
    gbc_controlsPane.gridheight = 23;
    gbc_controlsPane.insets = new Insets(0, 0, 0, 5);
    gbc_controlsPane.gridx = 16;
    gbc_controlsPane.gridy = 8;


    contentPane.add(controlsPane, gbc_controlsPane); 
    GridBagLayout
    gbl_controlsPane = new GridBagLayout(); 
    gbl_controlsPane.columnWidths = new
            int[]{0, 0, 0, 0, 0, 0, 0, 0, 0}; 
    gbl_controlsPane.rowHeights = new int[]{0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; 
    gbl_controlsPane.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0,
            0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE}; 
    gbl_controlsPane.rowWeights = new
            double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
    controlsPane.setLayout(gbl_controlsPane);


    JButton loadImageB = new JButton("Load an Image");
    loadImageB.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {

            loadImageF();



        }
    });

    GridBagConstraints gbc_loadImageB = new GridBagConstraints();
    gbc_loadImageB.insets = new Insets(0, 0, 5, 0);
    gbc_loadImageB.gridx = 7;
    gbc_loadImageB.gridy = 1;
    controlsPane.add(loadImageB, gbc_loadImageB);




    SB.addAdjustmentListener(new AdjustmentListener() {
        @Override
        public void adjustmentValueChanged(AdjustmentEvent e) {
            img.setSlice(e.getValue());
            currSlice = e.getValue();
            sliceNum.setText(currSlice + " / " + img.getNSlices());

        }
    });
    ViewPane.addMouseWheelListener(new MouseWheelListener() {
        @Override
        public void mouseWheelMoved(MouseWheelEvent e) {
            WindowManager.setCurrentWindow(win);
            int action = e.getWheelRotation();
            boolean zoom = e.isControlDown();
            int ox = can.offScreenX(e.getX());
            int oy = can.offScreenY(e.getY());
            if (zoom) {
                if (action < 0)
                    can.zoomIn(ox, oy);
                else can.zoomOut(ox, oy);
                can.setPreferredSize(dim);
                can.repaint();

            } 

            else {
                if (action < 0) {
                    if (currSlice > 1) {
                        currSlice--;
                        img.setSlice(currSlice);
                    }
                }
                if (action > 0) {
                    if (currSlice < img.getNSlices()) {
                        currSlice++;
                        img.setSlice(currSlice);
                    }
                }
                SB.setValue(img.getCurrentSlice());
                sliceNum.setText(currSlice + " / " + img.getNSlices());
            }

        }
    });



    can.addMouseListener(new MouseListener() {
        @Override
        public void mouseClicked(MouseEvent e) {

        }


        public void mouseReleased(MouseEvent e) {


        }

        @Override
        public void mouseEntered(MouseEvent e) {

        }

        @Override
        public void mouseExited(MouseEvent e) {

        }


        @Override
        public void mousePressed(MouseEvent e) {
            // TODO Auto-generated method stub

        }
    });


}


private void loadImageF() {

    counting_2.img.close();;
    OpenDialog od = new OpenDialog(getName());
    counting_2.img =Opener.openUsingBioFormats(od.getPath());
    can.repaint();
    can.revalidate();


    win.repaint();
    win.revalidate();

    if (counting_2.img == null)
        return;
}

}

在此处输入图片描述

推荐答案

您正在使用我们无法访问的第三方/自定义组件,因此无法提供任何真实"的解决方案. ..

You're using 3rd party/custom components, to which we don't have access to, so it's impossible to provide any "real" solutions, however...

但是,当按下loadImage按钮时,图像永远不会更新

However the image never gets updated when the loadImage button is pressed

这可能有多种原因,假设图像已正确加载,则可能是:

This could be for any number of reasons, assuming that the image was loaded correctly, its possible that:

  • 尚未将图像添加到任何可以在屏幕上渲染的图像和/或尚未将渲染添加到附着在屏幕上的任何图像
  • 通常,您可以在要更新的组件上调用repaint,它应该"触发一个新的绘制过程(不是图像,而是用来渲染它的东西)
  • 您可能需要在用于显示图像的组件上调用revalidate,以触发新的布局遍历,如果正确构建了UI,则会更新组件层次结构.
  • The image hasn't been added to anything which can render it on the screen and/or the render hasn't been added to anything attached to the screen
  • Generally, you can call repaint on the component you want updated, which "should" trigger a new paint pass (not the image, but what ever is been used to render it)
  • You may need to call revalidate on the component used to display the image to trigger a new layout pass, which, if you've built your UI correctly, will update the component hierarchy.

我的JScrollbar也没有出现在我想要的框架中.它没有滚动槽,并且与我的图像窗口没有相同的宽度.如何更改JScrollbar的参数以反映这一点?

Also my JScrollbar does not appear in the frame as I want it. It does not scroll trough and does not have the same width as my image window. How do I change the parameters of my JScrollbar to reflect that?

这是一个更为复杂的问题.首先,您应该在建立UI后在框架上调用setVisible,否则它可能无法布局/绘制内容(除非您手动调用revalidate/repaint).

That's a much more complicated issue. First, you should call setVisible on your frame AFTER you've established the UI, otherwise it may not layout/paint the contents (unless you call revalidate/repaint manually).

您不需要使用JScrollBar,而是应该使用JScrollPane.

You shouldn't need to use JScrollBar, but instead, should be using JScrollPane.

强烈建议您先通读以下内容:

I would, strongly, recommend you start by reading through:

  • Creating a GUI With JFC/Swing
  • How to Use Scroll Panes
  • Laying Out Components Within a Container

以更好地基本了解UI的API和组件

to get a better basic understand of the UI its API and components

这篇关于用户加载新图像时,是否可以更新显示窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆