Changing the slice in an ImageCanvas

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Changing the slice in an ImageCanvas

simon andrews (BI)
I'm writing an application in which I'm embedding ImageCanvas objects
which are then controlled via my application.

I'm having a problem adding a slider to allow me to browse through the
slices in an image stack.  I can make the canvas and can register the
slider being changed, but none of the methods I've tried calling have
made the ImageCanvas update its contents.  It always shows the first
slice.

I've made up a minimal example which I'll paste below.  If anyone can
tell me what I need to do to the ImagePlus or ImageCanvas objects to get
them to update I'd really appreciate it.

Many thanks

Simon.

import ij.*;
import ij.gui.*;
import java.awt.*;
import java.io.File;
import javax.swing.*;
import javax.swing.event.*;

public class ProblemCanvas extends JPanel implements ChangeListener {

        private ImageCanvas canvas = null;
        private ImagePlus ip = null;
        private JSlider slicesSlider = null;
       
        public ProblemCanvas (File file) {
                setLayout(new BorderLayout());
                ImagePlus ip = IJ.openImage(file.getAbsolutePath());
               
                this.ip = ip;
               
                canvas = new ImageCanvas(ip);
                add(canvas,BorderLayout.CENTER);

                slicesSlider = new
JSlider(JSlider.HORIZONTAL,0,ip.getStackSize()-1,0);
                slicesSlider.addChangeListener(this);
                add(slicesSlider,BorderLayout.SOUTH);
        }

        public void stateChanged(ChangeEvent ce) {
                // Someone has dragged the image slice slider.  We need
                // to update the frame being shown
               
                int frame = ((JSlider)ce.getSource()).getValue();
               
                System.out.println("Udating to show frame "+frame);
               
                ip.setSlice(frame+1);
                System.out.println("Slice is set to
"+ip.getCurrentSlice());
                ip.updateAndDraw();
                canvas.repaint();
        }
       
        public static void main (String [] args) {
                JFrame frame = new JFrame();
                File file = new File ("C:/test_stack.tif");
                frame.add(new ProblemCanvas(file));
                frame.setSize(200,200);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
        }
}