I have written a small code for simple resizing of any image on my system in java using SWING.
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Window;
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.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.filechooser.FileNameExtensionFilter;
class T extends JPanel implements ActionListener {
/**
*
*/
private static final long serialVersionUID = -2900955352094956729L;
int x = 0;
int flag = 0;
BufferedImage b;
Image z;
JButton j, a, cl;
JTextField ht, wdth;
T() {
j = new JButton("Hi");
a = new JButton("Resizze");
a.addActionListener(this);
add(a);
j.addActionListener(this);
add(j);
setBackground(Color.WHITE);
}
class tobi implements ActionListener {// for close button of second window
public void actionPerformed(ActionEvent asd) {
// if(x==1)
{
int gadda = 100, w = 100;
gadda = Integer.parseInt(ht.getText());
w = Integer.parseInt(wdth.getText());
z = z.getScaledInstance(gadda, w, Image.SCALE_SMOOTH);
setBackground(Color.BLACK);
j.repaint();
JButton ttr = (JButton) asd.getSource();
Window qwe = SwingUtilities.windowForComponent(ttr);
qwe.setVisible(false);
}
}
}
public void meth()// method to create second window
{
JFrame win = new JFrame();
win.setTitle("resizze!!");
win.setLayout(new FlowLayout());
JLabel height = new JLabel("Height:");
ht = new JTextField(20);
JLabel width = new JLabel("Width:");
wdth = new JTextField(20);
JButton cl = new JButton("close");
cl.addActionListener(new tobi());
win.add(height);
win.add(ht);
win.add(width);
win.add(wdth);
win.add(cl);
win.pack();
win.setVisible(true);
}
public void paintComponent(Graphics g) {
Graphics2D gt = (Graphics2D) g;
super.paintComponent(g);
if (x == 1)
g.drawImage(z, 0, 0, null);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == j) {
JFileChooser q = new JFileChooser();
// q.setFileFilter(new
// FileNameExtensionFilter("Image Files",ImageIO.getReaderFileSuffixes()));
q.addChoosableFileFilter(new FileNameExtensionFilter("Image Files",
"jpg", "jpeg", "png"));
int option = q.showOpenDialog(null);
if (option == JFileChooser.APPROVE_OPTION) {
File f = q.getSelectedFile();
try {
b = ImageIO.read(f);
z = b.getScaledInstance(100, 100, Image.SCALE_SMOOTH);
} catch (IOException ae) {
}
}
x = 1;
repaint();
}
else if (e.getSource() == a) {
meth();
}
}
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable(){
public void run(){
JFrame j = new JFrame();
j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
j.setSize(500, 500);
j.add(new T());
j.setVisible(true);
j.revalidate();
j.repaint();
}
});
}
}
I am asking user to upload the image file and then taking parameters height and width for resizing. After clicking on close button in second window,nothing happens(except for first few times),while the image on first window should be repainted. (Sorry for bad variable names and formatting)