Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML
 
 
Search All Tutorials
  

 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 
Java
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

Image on Frame in Java AWT

                         

Introduction

In this section, you will learn how to display image on the frame. This program shows you how to display image in your application.

In this program, there are three methods have been used to display the image on the frame in your application. These are explained below : 

main() : 
This is the main() method of the program from which your program starts to execute the program in sequence. This method has simply create the instance for the AwtImage class.

AwtImage() : 
This is the constructor of the AwtImage class in which, the instance of the MediaTracker class has been created to add the image on the frame using the addImage() method of the MediaTracker class. This constructor set the size, visibility and the close operation on the close button of the frame.

MediaTracker : 
MediaTracker
is the class of java.awt.*; package, has been used. MediaTracker is a utility class that tracks the status of a number of media objects. And this type of object can include images and audio clips. In this program only the explanation about the adding images has been given. MediaTracker class is used after creating the instance for that and calling the addImage() of the MediaTracker.

addImage() : 
This is the addImage() method of the MediaTracker class which is used load the image. Then the addImage() method of the MediaTracker has been used. Syntax of the addImage() function is MediaTracker.addImage(img, x, y, x1, y1). Arguments of addImage() function is explained below : 

img - image name type of Image.
x     - lower X - Coordinate type of int.
y     - lower Y - Coordinate type of int.
x1   - upper X - Coordinate type of int.
y1   - upper Y - Coordinate type of int.

Here is code of the program : 

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

public class AwtImage extends Frame{
  Image img;
  public static void main(String[] args){
    AwtImage ai = new AwtImage();
  }

  public AwtImage(){
    super("Image Frame");
    MediaTracker mt = new MediaTracker(this);
    img = Toolkit.getDefaultToolkit().getImage("icon_confused.gif");
    mt.addImage(img,0);
    setSize(400,400);
    setVisible(true);
    addWindowListener(new WindowAdapter(){
      public void windowClosing(WindowEvent we){
        dispose();
      }
    });
  }
  public void update(Graphics g){
    paint(g);
  }
  
  public void paint(Graphics g){
    if(img != null)
      g.drawImage(img, 100100this);
    else
      g.clearRect(00, getSize().width, getSize().height);
  }
}

Download this example.

                         

Facing Programming Problem?
Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

6 comments so far (post your own) View All Comments Latest 10 Comments:

thanks a lottttttttttt for posting the code for the whole day i m trying for this code

Posted by mahesh on Sunday, 02.3.08 @ 23:31pm | #47125

thank you a lot

Posted by ando on Tuesday, 10.16.07 @ 21:36pm | #34219

hi i cannot display the image i tried a jpg image and even gif image i donot get the image in frame.

Posted by rahul on Wednesday, 03.14.07 @ 14:25pm | #11675

thanks,I have a question: are methods in eclipse run in sequence automaticly? If not, in this example, how can the update(Graphic g) run?

Posted by andy on Friday, 02.23.07 @ 01:02am | #8924

Thanks but how to dl it?

Posted by Tony on Tuesday, 02.20.07 @ 23:55pm | #8559

Great example!
Thanks!

Posted by Francisco J Olvera on Friday, 12.8.06 @ 01:17am | #605

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.