Home Java Example Java Awt Image Size
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Image Size

Advertisement
This Java awt (Abstract Windowing Toolkit) tutorial describes about the image size. This is an actual representation of object. Simply, it helps us to feel associated with an object on the frame. Some images are either 'literal' images that are unequally

Image Size

     

This Java awt (Abstract Windowing Toolkit) tutorial describes about the image size. This is an actual representation of object. Simply, it helps us to feel associated with an object on the frame. Some images are either 'literal' images that are unequally create and involve little or no extension of the various meaning of the words that are used to express them or 'figurative' images that do not follow the literal meaning of the worlds completely.

Description of program:  

This program helps you in setting an image and getting the size of its. For setting an image on the frame, you will need an image that have to be added in the MediaTracker object with the help of addImage() method. After adding you will get the size of image (width and height) through the getWidth() and getHeight() method. Here you will see the frame's size are arranged according to the size of image. You will try and get it.

Description of code:

Toolkit():
This is the constructor of Toolkit class that is the abstract super class of all exact implementation of awt. The various components are bounded by subclass of Toolkit to a particular native toolkit implementations.

getDefaultToolkit():
This method returns the default toolkit and if could not be found it then it will thrown by the AWTError.

getImage(String file_name):
This method gets pixels data from the given file name that has the extension either GIF, JPEG or PNG and returns an image in pixels. It takes string type file name that is the name of an image.

MediaTracker():
This is the constructor of MediaTracker class that is a utility class for tracking the status of a number of media objects that include an audio clips as well as images but only images are supported in current time.

waitForID(int id):
This method is used to start the loading all images with the help of media tracker to specified identifier that are given by user.

getWidth():
This method returns the width of displaying images in pixels.

getHeight():
This method returns the height of displaying images in pixels.

Here is the code of program:

import java.awt.*;
import java.awt.event.*;

public class ImageSize extends Frame{
  Image image;
  public static void main(String[] args) {
  try{
  new ImageSize();
  }
  catch(InterruptedException e){}
  }
  public ImageSize() throws InterruptedException{
  Toolkit tool = Toolkit.getDefaultToolkit();
  image = tool.getImage("1.gif");
  MediaTracker mTracker = new MediaTracker(this);
  mTracker.addImage(image,1);
  mTracker.waitForID(1);
  int width = image.getWidth(null);
  int height = image.getHeight(null);
  System.out.println("The width of image: " + width);
  System.out.println("The height of image: " + height);
  setSize(width, height);
  setTitle("Image Size Example!");
  addWindowListener(new WindowAdapter(){
  public void windowClosing(WindowEvent we){
  System.exit(0);
  }
  });
  setVisible(true);
  }
  public void paint(Graphics g){
  g.drawImage(image,0,0,null);
  }
}

Download this example.

Output of program:

Size of an image:

C:\vinod\awt1>javac ImageSize.java

C:\vinod\awt1>java ImageSize
The width of image: 300
The height of image: 224
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: March 13, 2008

Related Tutorials

Ask Questions?    Discuss: Image Size   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
Sathish
October 18, 2011
Nice example

Thanks Nice example.
DMCA.com