Friday, 1 July 2016

java - problem in setting scrollpane for canvas

In the below program I want to zoomin and zoomout the image which is selected by the user.the problem is that when I zoomin or zoomout the image by clicking on zoomin zoomout button then scrollabars are not working. please help what is hte problem




import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Container;
import java.awt.Cursor;
import java.awt.Dimension;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class ImageProcess extends JFrame implements ActionListener {

private JMenuItem newimage;
private JMenuItem open;
private Image image;
private File file;
private JFrame jf;
private JPanel panel;
private Cursor zoomCursor;

public ImageProcess() {
super("Image Demo");

this.setBounds(200, 200, 100, 100);
this.setSize(800, 800);
this.setResizable(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container con = this.getContentPane();
this.setVisible(true);
JMenuBar menubar = new JMenuBar();
this.setJMenuBar(menubar);
JMenu file = new JMenu("File");
newimage = new JMenuItem("New");

open = new JMenuItem("Open");
menubar.add(file);
file.add(newimage);
file.addSeparator();
file.add(open);
newimage.addActionListener(this);
open.addActionListener(this);
}

public void actionPerformed(ActionEvent e) {

if (e.getSource() == newimage) {
ReadImage ri = new ReadImage();

}
if (e.getSource() == open) {
OpenImage oi = new OpenImage();

}
}


public static void main(String args[]) {
ImageProcess imagepro = new ImageProcess();
}
}

class ImageFileFilter extends javax.swing.filechooser.FileFilter {
public boolean accept(File file) {
if (file.isDirectory())
return false;
String name = file.getName().toLowerCase();

return (name.endsWith(".jpg") || name.endsWith(".png") || name
.endsWith(".gif"));
}

public String getDescription() {
return "Images (*.gif,*.bmp, *.jpg, *.png )";
}
}

class ReadImage extends JFrame {

File file;
JFileChooser chooser;

public ReadImage() {
chooser = new JFileChooser();
chooser.setMultiSelectionEnabled(false);

chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
int returnVal = chooser.showOpenDialog(ReadImage.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {

file = chooser.getSelectedFile();
try {
final BufferedImage bi = ImageIO.read(file);
Canvas can = new Canvas() {
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(bi, 0, 0, this);
}
};
this.getContentPane().add(can);

can.setPreferredSize(new Dimension(bi.getWidth(), bi
.getHeight()));
this.pack();
this.setVisible(true);
} catch (IOException ie) {
ie.printStackTrace();
}
}
}


}

class OpenImage extends JFrame implements ActionListener {
private JFileChooser chooser;
private File file;
private JFrame jf;
Canvas can;
JPanel pan;
public JScrollPane sp;
Dimension imgSize, iniSize;

private JButton butIn = new JButton("ZoomIN");
private JButton butOut = new JButton("ZoomOUT");
private JButton butReset = new JButton("Reset");

public OpenImage() {
chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));
chooser.addChoosableFileFilter(new ImageFileFilter());
int returnVal = chooser.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {

file = chooser.getSelectedFile();
String filename = file.getName();
try {
final BufferedImage bi = ImageIO.read(file);
imgSize = iniSize = new Dimension(bi.getWidth(), bi.getHeight());
jf = new JFrame();
this.setResizable(false);
this.setTitle(filename);
pan = new JPanel();
pan.add(butIn);

pan.add(butOut);
// pan.add(butReset);
butIn.addActionListener(this);
butOut.addActionListener(this);
butReset.addActionListener(this);

can = new Canvas() {
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
if (imgSize.width = iniSize.width) {

can.setSize(imgSize);
}
can.repaint();
can.validate();
}

public void zoomOUT() {
if (!(getWidth() > imgSize.width)) {
int x = 10 * imgSize.width / 100;
int y = 10 * imgSize.height / 100;


imgSize = new Dimension(imgSize.width - x, imgSize.height - y);
if (getWidth() >= iniSize.width + 50) {
can.setSize(imgSize);
}
can.repaint();
can.validate();
} else {
repaint();
}

}

}

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...