Load Image and scale it : Image « 2D Graphics « Java Tutorial






Load Image and scale it
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class ImageFileFilterImageScale extends JFrame implements ActionListener {
  Image img;

  JButton getPictureButton  = new JButton("Get Picture");

  public static void main(String[] args) {
    new ImageFileFilterImageScale();
  }

  public ImageFileFilterImageScale() {
    this.setSize(300, 300);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel picPanel = new PicturePanel();
    this.add(picPanel, BorderLayout.CENTER);

    JPanel buttonPanel = new JPanel();
    getPictureButton.addActionListener(this);
    buttonPanel.add(getPictureButton);
    this.add(buttonPanel, BorderLayout.SOUTH);

    this.setVisible(true);
  }

  public void actionPerformed(ActionEvent e) {
    String file = getImageFile();
    if (file != null) {
      Toolkit kit = Toolkit.getDefaultToolkit();
      img = kit.getImage(file);
      img = img.getScaledInstance(300, -1, Image.SCALE_SMOOTH);
      this.repaint();
    }
  }

  private String getImageFile() {
    JFileChooser fc = new JFileChooser();
    fc.setFileFilter(new ImageFilter());
    int result = fc.showOpenDialog(null);
    File file = null;
    if (result == JFileChooser.APPROVE_OPTION) {
      file = fc.getSelectedFile();
      return file.getPath();
    } else
      return null;

  }

  class PicturePanel extends JPanel {
    public void paint(Graphics g) {
      g.drawImage(img, 0, 0, this);
    }
  }

}
class ImageFilter extends javax.swing.filechooser.FileFilter {
  public boolean accept(File f) {
    if (f.isDirectory())
      return true;
    String name = f.getName();
    if (name.matches(".*((.jpg)|(.gif)|(.png))"))
      return true;
    else
      return false;
  }

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








16.26.Image
16.26.1.Draw ImageDraw Image
16.26.2.Resize an imageResize an image
16.26.3.Load Image and scale itLoad Image and scale it
16.26.4.Flush an image
16.26.5.Reading an Image or Icon from a File
16.26.6.Get the dimensions of the image; these will be non-negative
16.26.7.Draw an Icon object
16.26.8.Scaling a Drawn Image
16.26.9.Shearing a Drawn Image
16.26.10.Rotating a Drawn Image
16.26.11.Translating a Drawn Image
16.26.12.Creates PNG images of the specified color that fade from fully opaque to fully transparent
16.26.13.Determining If an Image Has Transparent Pixels
16.26.14.Getting the Color Model of an Image
16.26.15.Filtering the RGB Values in an Image
16.26.16.Flip an image
16.26.17.Blur our image: Blur means an unfocused image
16.26.18.A reflected image: effect makes an illusion as if the image was reflected in water
16.26.19.Use PixelGrabber class to acquire pixel data from an Image object
16.26.20.Calculation of the mean value of an image
16.26.21.This filter removes all but the red values in an image
16.26.22.Using mediatracker to pre-load images
16.26.23.Create a grayscale image with Java 2D tools
16.26.24.Shrinking an image by skipping pixels
16.26.25.Get average of a set of images
16.26.26.Enlarging an image by pixel replication
16.26.27.A 3x3 kernel that blurs an image.
16.26.28.A 3x3 kernel that sharpens an image.
16.26.29.A 3x3 kernel that embosses an image.
16.26.30.Brighten the image by 30%
16.26.31.Darken the image by 10%
16.26.32.Create a filter that can modify any of the RGB pixel values in an image.
16.26.33.Gray scale image operation