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.
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?
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?
Here is my code below:
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;
}
}