Now we are back to the Applet’s World, Yay!. Let’s write some little app which just load an image and show us.

If you don’t know which’s the basic structure of a Java Applet, please read this first.

First of all, we need to import our basic libraries to work, so, the begin of your file, let’s say ImageViewer.java needs to looks like:

import java.applet.*;
import java.awt.*;

Next it’s needed to declare our class, remember that Java just allows one class per file and the class must be named exactly like file(or file like class), then you now have next thing:

import java.applet.*;
import java.awt.*;

public class ImageViewer extends Applet
{
}

The extends Applet thing it’s just a manner to say: hey!, Applet it’s my daddy and because of him I got his whole properties and habilities.

In this specific case, we gonna need three applet specific methods: init, start and paint, but before that, let’s declare the variables that we are gonna use.

import java.applet.*;
import java.awt.*;

public class ImageViewer extends Applet
{
    int maxX, maxY;
    Image image;
    MediaTracker mt;
}

The two integer variables are used to store the image width and height respectively. The Image class variable are used to store the information of the image that can be painted on the screen. And the MediaTracker variable is used to “track” some aspects of media, you’ll see it’s utility after.

Going to our first method: init().

As you can remember, init() method is executed when the applet is created, so we can look it like a “contructor”.

public void init()
{
    showStatus("Getting image...");
    mt = new MediaTracker(this);
    image = getImage(getDocumentBase(), "ImageTest.jpg");
    mt.addImage(image, 0);
}

The showStatus thing show us a message in the web browser status bar.
MediaTracker stuff creates a new object of the named class, and the only parameter to the constructor is this, if we read the java documentation says that MediaTracker contructor receives an ImageObserver variable type, but… let’s leave that for another season, in this case(and in every Applet AFAIK) just pass this.
The image line says that obtain an image with absolute path based on getDocumentBase() (this is a URL, not a String) and relative path “ImageTest.jpg”. Here it is an important thing, the getImage method returns inmediatly and it doesn’t care if the image exists, is loaded and other important things that really matters, and that’s the reason of MediaTracker to be, besides we can always use images without MediaTracker but it’s not really sane.
mt.addImage(image, 0) adds the image that just said to load into MediaTracker so we don’t loose the clue of it, the second argument it’s an integer used as a “group identifier”, normally set to zero.

Next is start() method, this is called afted init() method.

showStatus("Loading image...");
try
{
    mt.waitForAll();
}
catch(Exception e)
{
    e.printStackTrace();
}
while(!mt.checkAll(true))
{
}
imageX = image.getWidth(this);
imageY = image.getHeight(this);
showStatus("Resizing Applet image size: " + imageX + "x" + imageY);
resize(imageX, imageY);

showStatus() again.
Inside the try block we can find mt.waitForAll() which starts to load all images registered into MediaTracker.
The while loop with the mt.checkAll(true) check if all images are loaded and the parameter true said that if some are not loaded, then load!, that’s why inside the loop, to wait until every image are ready.
Next lines just store the width and height of the image then we paint in status bar the dimensions and then resize the applet to fit the image to it.

paint() method do that: paint the image on the screen, of course, first we inform to user that we’re going to paint it, and then paint the image.

That’s all!. Now you have complete code:

import java.applet.*;
import java.awt.*;

public class ImageViewer extends Applet
{
    Image image;
    int imageX, imageY;
    MediaTracker mt;
    
    public void init()
    {
	showStatus("Getting image...");
	mt = new MediaTracker(this);
	image = getImage(getDocumentBase(), "ImageTest.jpg");
	mt.addImage(image, 0);
    }
    
    public void start()
    {
	showStatus("Loading image...");
	try
	    {
		mt.waitForAll();
	    }
	catch(Exception e)
	    {
		e.printStackTrace();
	    }
	while(!mt.checkAll(true)){}
	imageX = image.getWidth(this);
	imageY = image.getHeight(this);
	showStatus("Resizing Applet image size: " + imageX + "x" + imageY);
	resize(imageX, imageY);
    }
    
    public void paint(Graphics g)
    {
	showStatus("Drawing image...");
	g.drawImage(image, 0, 0, this);
    }
      
}

It’s fun to look the applet doing nothing but painting an image, but it’s a nice start, maybe you can figure out how to make a simple game.

Greets.

One response »

  1. […] a zip format file with the extension jar. How do that?, simple!, imagine that we gonna pack the ImageViewer […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s