by David Wood

Up until now, you've seen a lot of Java demonstrations and explanations regarding the more (to the layman) obscure features of the language. To be sure, understanding the event mechanisms and interface objects of Java is important; however, ultimately, most people writing Java are writing it for the Web--applet style. And on the Web, well, these days flash and presentation are of key importance to a lot of designers.

The best way to present yourself is to draw them a picture, followed closely by playing them a sound. One of the primary reasons the Web took off the way it has is that it could do both of these things, after a fashion, which wasn't lost on the creators of Java.

Thankfully, it's quite simple to paint an image into your frame of influence in the browser. Java's Applet API has routines for loading images (by URL) and displaying them. And wonder of wonders, rather than coining a new and obscure graphics format for its images, it supports GIFs and JPEGs.

The easiest way to incorporate a predrawn image into your applet is to use the getImage() method from right out of java.applet.Applet. There are several ways that you can call it; we're going to focus on two of them. The simplest is simply this:

    Image myimage = getImage(new URL("http://myhost.org/images/myimage.jpg"));

Here we've instantiated a new URL object containing the URL we've chosen and passed that to getImage(). Now an important functional note: getImage returns an Image object right away--but it does not actually load the image until it is required. The other call takes two parameters: a URL and a String. This String is appended to the URL in some intelligent fashion to form the final location of your image. Why would we need this? Look at this example:

    Image myimage = getImage(getCodeBase(),"myimage.gif");

You can call getImage this way due to the fact that it was, in all likelihood, created explicitly for the getCodeBase() and getDocumentBase() methods. Their purpose is to return the "Code Base" and "Document Base" where the applet itself originated from, in URL form. This allows the programmer to avoid "hard coding" URLs into his or her applet (always a bad idea). One can instead reference a file by its relative location to where the applet itself is stored on the Web server. In this case, you'd put the "myimage.gif" file right next to your applet class file. If you used getDocumentBase instead, you'd put the GIF file next to the document that your applet was imbedded in.

Once you have an Image object associated with a real image, you can draw it inside of your frame with drawImage(). It's important to remember that this method is part of an instantiated Graphics object; in other words, you can't just call it from anywhere. It's best to follow my next example, where the Graphics object drawImage that it's called through is the one passed to a paint() method you've created:


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

public class showimage extends Applet {

  Image myimage;
  public void init () {

    myimage = getImage(getDocumentBase(),"javaimage.gif");
  }

  public void paint (Graphics g) {

     // Draw the image, upper left corner starts at pixel coordinates
     // (0,0), ImageObserver (ignored for the moment) referenced as "this".

     g.drawImage(myimage,0,0,this);
  }
}


Again, this is barely scratching the surface. By using different calls to this simple method alone, it's possible to scale your image and change the background color. Future columns will touch on ImageObservers, ImageProducers, ImageFilters, and ImageConsumers. Not to mention that your images can also be integrated with elements of your user interface. As usual, Java allows for simple things to be done simply, while leaving the more complex, and configurable, options beneath the surface.

Coming up, we'll tackle sounds, gloss quickly over animation, and begin to talk about how the simple elements of Java we've discussed so far are integrated into some "first-generation" clever applets. Until next week . . .

Past installments of Java Jolt

http://www.internet.com/