Sometimes we need to show images in our Java program and sometimes the images
need to be re-sized or scaled; the image may be stored in a file in the storage.
Here we will discuss how we can show an image on a label (javax.swing.JLabel)
from an image file. We will also discuss how we can scale or resize the image
before showing on the label.
Firstly, we have to create an Image
object which refers the image file (ImageToBeResized.jpg, in this example). As
Image is an interface we cannot instantiate it directly, however getImage(…)
method of Toolkit class can help us here to create a reference of
java.awt.Image. Static method getDefaultToolkit of Toolkit class can be used to
create a reference of Toolkit.
See the following code:
Image image = Toolkit.getDefaultToolkit().getImage("ImageToBeResized.jpg.jpg");
Here it is assumed that the image file and the Java program are in
same directory. If the image is in different directory, the full location should
be stated in the parameter of getIamge(…) method.
Now, if we need
image scaling we may use getScaledInstance(…) of Image. Three arguments need to
be passed to the method. First two are new width and height of the image and the
last one is the resizing option (any of Image.SCALE_SMOOTH, Image.SCALE_FAST or
Image.SCALE_DEFAULT).
See the code below:
Image scaledImage = image.getScaledInstance(200,300,Image.SCALE_DEFAULT);
Here we will get an object of 200 x 300 sized image.
A
complete code is given here to demonstrate how we can show image on a
label(javax.swing.JLabel) with and without scaling.
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.Container;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
public class ImageResize {
JFrame frame = new JFrame();
JTextField txtFilePath = new JTextField();
JButton btnBrowse = new JButton("Browse");
JLabel lblOriginalImage = new JLabel();// original image without scaling will be shown
JLabel lblResizedImage1 = new JLabel();// scaled image will be shown
JLabel lblResizedImage2 = new JLabel();
public ImageResize() {
Container c = frame.getContentPane();
c.setLayout(null);
//set up components size and location
txtFilePath.setBounds(10, 10, 280, 30);
btnBrowse.setBounds(300, 10, 80, 30);
lblOriginalImage.setBounds(10, 60, 280, 250);
lblResizedImage1.setBounds(300, 110, 200, 200);
lblResizedImage2.setBounds(510, 160, 150, 150);
//add the components on the container
c.add(txtFilePath);
c.add(btnBrowse);
c.add(lblOriginalImage);
c.add(lblResizedImage1);
c.add(lblResizedImage2);
txtFilePath.setEditable(false); // the field will not be editable
//set border for the labels
lblOriginalImage.setBorder(BorderFactory.createEtchedBorder());
lblResizedImage1.setBorder(BorderFactory.createEtchedBorder());
lblResizedImage2.setBorder(BorderFactory.createEtchedBorder());
btnBrowse.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
JFileChooser fileChooser = new JFileChooser(".");
FileNameExtensionFilter filter = new FileNameExtensionFilter("Images", "jpg", "gif", "png");
fileChooser.addChoosableFileFilter(filter);
int choice = fileChooser.showOpenDialog(null);
if (choice == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
txtFilePath.setText(file.getPath());
//create Image object from an image file
Image originalImage = Toolkit.getDefaultToolkit().getImage(file.getPath());
//resize the image according to the label size
Image resizedImage1 = originalImage.getScaledInstance(lblResizedImage1.getWidth(),
lblResizedImage1.getHeight(), Image.SCALE_DEFAULT);
Image resizedImage2 = originalImage.getScaledInstance(lblResizedImage2.getWidth(),
lblResizedImage2.getHeight(), Image.SCALE_DEFAULT);
//show images on the labels
lblOriginalImage.setIcon(new ImageIcon(originalImage));
lblResizedImage1.setIcon(new ImageIcon(resizedImage1));
lblResizedImage2.setIcon(new ImageIcon(resizedImage2));
}
}
});
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(80, 100, 680, 360);
frame.setVisible(true);
}
public static void main(String args[]) {
ImageResize ir = new ImageResize();
}
}