Share on Google+Share on Google+

Get Image Size Example

This Example shows you get the image size.

Get Image Size Example


This Example shows you get the image size.

Description of the code :

Toolkit.getDefaultToolkit() : Toolkit class is used to bind the different components to specific native toolkit implementations. if we want to implementation Toolkit then firstly set System property named "java.awt.headless" is set to true.getDefaultToolkit is used for get the default toolkit.

Toolkit.getImage() : getImage method return a Image class object and this object get pixel data from the specified URL.

Graphics.drawImage() : drawImage method is used to draw a specified image as is currently available. The image is drawn with its top-left corner at (x, y) coordinate.

Image.getWidth() : getwidth method is used for get the image width.

Image.getHeight() : getwidth method is used for get the image Height.

Graphics.drawString() : drawImage method is used for draw a specified String. Its top-left corner at (x, y) coordinate.

Frame.setSize () : setSize method is used for set the frame size.

Frame.setVisible() : setVisile method is used for visible true or false.

import java.awt.*;
import java.awt.event.*;
import java.util.Locale;

public class GetImageSize extends Frame {

  Image image;
  String imageName = "photo.jpg";
  int width,height;

  public GetImageSize() {

  addWindowListener(new WindowAdapter() {

  public void windowClosing(WindowEvent we) {

  public void paint(Graphics g) {
  Toolkit tool = Toolkit.getDefaultToolkit();

  image = tool.getImage(imageName);
  this.setSize(width+20,height+80 );
  g.drawImage(image, 1030this);
  g.setColor(new Color(180,0,0));
  g.setFont(new Font("Times New Roman",140));
  g.drawString(imageName.toUpperCase(Locale.ENGLISH), width/2-5070);
  g.setFont(new Font("Times New Roman",116));
  g.drawString("Image Size : "+width+

  public static void main(String args[]) throws Exception {

  GetImageSize image = new GetImageSize();


Download code



Posted on: October 23, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

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.

Discuss: Get Image Size Example  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image