Home Java Example Java Swing Graphics2D Show Image Scale

Related Tutorials


 
 

Share on Google+Share on Google+

Show Image Scale

Advertisement
To show scales of Image, an image is defined inside the class folder and calls the ImageIcon class to return the image.

Show Image Scale

     

This section shows you the scales of image.

To show scales of Image, an image is defined inside the class folder and calls the ImageIcon class to return the image. The Insets class shows the border of a container which specifies the space leave at each of its edges.

The method getScaleInstance() of class Image allows to generate the scales of image. The method drawImage() must know that the scaling has been doing here. There are five hints for scaling the image. The SCALE_DEFAULT shows the default scale of image. The SCALE_FAST  gives priority to speed over smoothness. The SCALE_SMOOTH gives smoothness to speed over speed. The SCALE_REPLICATE  use ReplicateScaleFilter provided by the toolkit. The  SCALE_AREA_AVERAGING use AreaAveragingScalingFilter provided by the toolkit.

Here is the code of ImageScale.java

import java.awt.*;
import javax.swing.*;

public class ImageScale extends JFrame {
  Image image;
  Insets insets;
  public ImageScale() {
  super("Show Image Scales");
  ImageIcon imageIcon = new ImageIcon("image4.jpg");
  image = imageIcon.getImage();
  }
  public void paint(Graphics g) {
  super.paint(g);
  if (insets == null) {
  insets = getInsets();
  }
  g.drawImage(image, insets.left, insets.top, this);
  }
  public void scale() {
  reset();
  Image img = image;
  image = img.getScaledInstance(250, -1, Image.SCALE_FAST);
  repaint();
  reset();
  
  image = img.getScaledInstance(300, -1, Image.SCALE_SMOOTH);
  repaint();
  reset();
  
  image = img.getScaledInstance(450, -1, Image.SCALE_REPLICATE);
  repaint();
  reset();
  
  image = img.getScaledInstance(400, -1, Image.SCALE_DEFAULT);
  repaint();
  reset();
  image = img.getScaledInstance(350, -1, Image.SCALE_AREA_AVERAGING);
  repaint();
  reset();
  System.exit(0);
  }
  private void reset() {
  try {
  Thread.sleep(3000);
  catch (Exception e) {}
  }
  public static void main(String args[]) {
  ImageScale imgScale = new ImageScale();
  imgScale.setSize(400200);
  imgScale.show();
  imgScale.scale();
  }
}

Output will be displayed as:

We have defined the sleep(3000) method, therefore the scales which are defined in the code will get displayed with the image automatically after the specified interval of time.

Download Source Code

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 15, 2008

Related Tutorials

Discuss: Show Image Scale  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com