【问题标题】:Is there a way to update the display window when the user loads a new image?当用户加载新图像时,有没有办法更新显示窗口?
【发布时间】:2019-06-17 15:49:56
【问题描述】:

我正在编写一个程序来加载图像(一个文件中的图像堆栈)并使用控件(按钮)对图像进行一些操作,并且能够在框架上加载另一个图像而无需更改以前的控件。

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

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

如何让我的框架在加载新图像时更新图像窗口?

我的JScrollbar 也没有按照我的意愿出现在框架中。它不会滚动,也没有与我的图像窗口相同的宽度。 如何更改滚动条的参数以反映这一点?

下面是我的代码:

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

    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;
}

}

enter image description here

【问题讨论】:

标签: java swing jframe jscrollpane imagej


【解决方案1】:

您正在使用我们无法访问的第 3 方/自定义组件,因此不可能提供任何“真正的”解决方案,但是...

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

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

  • 图像尚未添加到任何可以在屏幕上呈现它的东西和/或渲染尚未添加到任何附加到屏幕上的东西
  • 通常,您可以在要更新的组件上调用 repaint,这“应该”触发新的绘制通道(不是图像,而是用于渲染它的任何内容)
  • 您可能需要在用于显示图像的组件上调用 revalidate 以触发新的布局传递,如果您正确构建了 UI,它将更新组件层次结构。

我的 JScrollbar 也没有按照我的意愿出现在框架中。它不会滚动,也没有与我的图像窗口相同的宽度。如何更改 JScrollbar 的参数以反映这一点?

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

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

我强烈建议您先阅读:

更好地了解 UI 及其 API 和组件

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-12
    • 2019-09-05
    • 1970-01-01
    相关资源
    最近更新 更多