[an error occurred while processing this directive]

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:

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.

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.

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.

This method returns the width of displaying images in pixels.

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) {
      new ImageSize();
    catch(InterruptedException e){}
  public ImageSize() throws InterruptedException{
    Toolkit tool = Toolkit.getDefaultToolkit();
    image = tool.getImage("1.gif");
    MediaTracker mTracker = new MediaTracker(this);
    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){
  public void paint(Graphics g){

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


[an error occurred while processing this directive]