Draw Images Example Java

March 10, 2011 | Image drawing

Java comes with many built-in multimedia capabilities. Java includes many classes and methods specially designed for drawing images and animation in both applications and applets. Drawing an image on applets, quiet varies, from drawing on applications. Two programs are given showing the way of drawing images on applets and applications.

1. Draw Images Example Java in Applets

While browsing, you come across, very often, beautiful and attractive images and animations. They can be done with applets. To draw the images, the java.awt.Graphics class comes with a method drawImage(). With the help of this method and ImageObserver at the background, images can be drawn on the applet window. Following program explains.

import java.awt.Image;
import java.awt.Graphics;
import java.applet.Applet;              
import java.net.URL;              	
public class ImageWithApplet extends Applet  
  ublic void paint( Graphics g )  
    URL url1 = getCodeBase();
    Image img = getImage(url1, "bird2.gif");
    g.drawImage(img, 60, 120, this);  

HTML file to run the above applet - ImageWithApplet.html

Draw Images Example Java Output screenshot on Draw Images Example Java

Let us see the coding.

URL url1 = getCodeBase(); Image img = getImage(url1, "bird2.gif");

Any image to be drawn should be converted into an object of Image class (like ImageIcon in case of Swing). The getImage() method of Applet class returns an object of Image, img. This method takes two parameters – the address of the image (bird2.gif) as an object of URL and the image itself as a string. Place the image, bird2.gif, in the same folder where the applet exists. getCodeBase() method of Applet class returns an object of URL (here, url1) that contains the address of the image (as image and applet exist in the same folder).

g.drawImage(img, 60, 120, this);

Graphics is an abstract class from java.awt package and contains many methods of drawing. One of such methods is drawImage() that takes four parameters. The first parameter is the image to be drawn as an object of Image class. The second and third parameters are x and y coordinates where the image is to be drawn on the applet window. The last parameter is an object of ImageObserver. The "this" argument in the above statement refers an object of ImageObserver.

Now, where from the JVM gets the reference of ImageObserver? The super class of Applet, java.awt.Component class implements the ImageObserver (observe, the hierarchy of Applet in Applets topic). From its super class, Applet gets the reference of ImageObserver.

Observe how drawImage() method is defined in Graphics class. boolean drawImage(Image img, int x, int y, ImageObserver observer);

For the execution of this applet, run the HTML file, ImageWithApplet.html, either from appletviewer or a browser; for more information, refer Applet topic.

Programming Tip: java.awt.Image is an abstract class. Do not try to create an object. Get the Image object by calling getImage() method of Applet. getImage() method creates and returns an Image object in specific format known to the underlying OS.

2. Draw Images Example Java in Applications (Frames)

After seeing how to draw images on applets, let us see how to draw on frames. Both come with different code. getImage() method works in applets as getImage() is a method of Applet class. To work with frames, getImage() method also exist with Toolkit class. Following program illustrates.

import java.awt.*;
public class ImageWithApplication extends Frame    
  public ImageWithApplication( )    
    setTitle("Image on Frame");
    setSize(250, 250);    
  public void paint(Graphics g)  
    Toolkit tkit = Toolkit.getDefaultToolkit( );
    Image img = tkit.getImage("bird4.gif");
    g.drawImage(img, 60, 80, this);
  public static void main( String args[])   
    new ImageWithApplication();   // anonymous object to access the constructor 

Draw Images Example Java Output screen on Draw Images Example Java

Toolkit tkit = Toolkit.getDefaultToolkit(); Image img = tkit.getImage("bird4.gif");

getDefaultToolkit() is a static method of abstract class, java.awt.Toolkit. This method returns an object of Toolkit. getImage() method of Toolkit class returns an object of Image class, here it is, img. img object represents bird4.gif as an object of Image class. drawImage() method is discussed in the earlier program. class MediaTracker

In animation, multiple images are to be loaded sequentially. java.awt.MediaTracker keeps a track of image loading without loading partial images. It maintains the track with states – LOADING, ERRORED, COMPLETE and ABORTED. These states are defined as static, final and integer variables in MediaTracker class. MediaTracker does not avoid flickering. To eliminate flickering, use double-buffering or update() method (we get later).