The following code below shows when an image is opened, the user should be able to zoom in and out of the image using the arrow keys. But when I zoom I get the following picture:
I hope someone can see where I am going wrong cause when I move the frame (kind of like refreshing), then the picture is okay but if you don't move it and then this is what you see. I want to fix this so even if you don't move the frame the picture is still okay. Thanks in advance.
Here is the main class where it contains a call to the imageJ zoom class:
zoom = new JMenu("Zoom");
zoom.setEnabled(false);
JMenuItem in = new JMenuItem("Zoom In");
in.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_UP, ActionEvent.ALT_MASK));
in.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
MyInternalFrame selectedFrame = (MyInternalFrame) desktop.getSelectedFrame();
Zoom z = new Zoom(selectedFrame.getImage());
String args = "in";
z.run(args);
}
});
JMenuItem out = new JMenuItem("Zoom Out");
out.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, ActionEvent.ALT_MASK));
out.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
MyInternalFrame selectedFrame = (MyInternalFrame) desktop.getSelectedFrame();
Zoom z = new Zoom(selectedFrame.getImage());
String args = "out";
z.run(args);
}
});
zoom.add(in);
zoom.add(out);
I also had to add a constructor to the zoom so it would take the image my customized GUI opens (this is in the zoom class):
ImagePlus imp;
public Zoom (ImagePlus img){
imp = img;
}
In the custom window class, where the image is opened, I have a component listener which resizes the image to fit the frame size and works very well (I don't know if this is the problem):
@Override
public void componentResized(ComponentEvent arg0) {
// TODO Auto-generated method stub
Rectangle rect = desktop.getSelectedFrame().getBounds();
MyInternalFrame f = (MyInternalFrame) desktop.getSelectedFrame();
f.getImage().getCanvas().fitToWindow(rect);
System.out.println("resized- the real one");
}
In the same class here is where the image is added to the frame:
JPanel panel = new JPanel();
ImageCanvas c = new ImageCanvas(image);
c.getImage();
//panel2.add(new JLabel(new ImageIcon(c.getImage())));
m = new ImageWindow(image);
Image n = new Image();
//frame = new MyInternalFrame(title, img, save,m);
//ImageCanvas c = m.getCanvas();
ImagePlus im = new ImagePlus();
im.setImage(image);
String [] array = title.split("/");
//System.out.println("in manager: "+array[5]);
//image.setTitle(path);
frame = new MyInternalFrame(image.getTitle(), image, save, title);
m.centerNextImage();
//image.getCanvas().setScaleToFit(true);
panel.add(m.getCanvas());
panel.setBackground(Color.white);
frame.add(panel);
frames.add(frame);
frame.setVisible(true);
frame.setAutoscrolls(true);
frame.setAutoscrolls(true);
//frame.setOpaque(true);
frame.setResizable(true);
desktop.add(frame);
I don't get the background of how you define the class MyInternalFrame and the ImageCanvas.
I suggest you to check the code :
f.getImage().getCanvas().fitToWindow(rect);
To resize a image on a panel or other component:
step1: you can override the paintComponent() method,and use the graphics object's scale() and translate() method to resize the image.
step2: call the panel's repaint() method to update the panel.
This will really work.