FAQ 4a
Updated: 1/10/00

>>>>>> How do I draw pictures?
==============================

Drawing pictures has two parts, loading the Image then drawing the Image.
Loading Images also has two parts, loading online and loading locally.

Here is a sample program that loads and draws a picture.  The drawing will 
occur in the overwritten function paint():


// 'ImageApplet.java' code file
import java.applet.*;
import java.awt.*;
import java.net.*;

class ImageApplet extends Applet {

	Image myImage;
	boolean inBrowser;

	Applet(){
		inBrowser = true;	// see faq 3g
	}

	public void init(){
		myImage = loadImage("MyPicture.gif");	// must be GIF or JPG
	}

	public void paint(Graphics g){		// draw twice with outline
	int wide, high;

		if (myImage!=null) {

			g.drawImage(myImage,0,0,20,20,this);	// thumbnail

			wide = myImage.getWidth(this);
			high = myImage.getHeight(this);
			g.drawRect(0,20,wide,high);		// outline the image area

			g.drawImage(myImage,0,20,this);		// fullsize
		}
	}


	Image loadImage(String picName){
	URL internetPath;

		if (!inBrowser) {	// load locally
			return getToolkit().getImage("c:\ArtFolder\"+picName);
		}

		try {			// otherwise load over internet
			internetPath = new URL("http://www.mySite.com/ArtFolder/"+picName);
			return getImage(internetPath);
		}
		catch (MalformedURLException e) {return null;}
	}
}



While the above code is CORRECT it will sometimes fail to work.  When you load images in JAVA,
the task is performed asynchronously.  This means that the code will keep running at the 
same time that the image is loading.  This can be handy in some circumstances, but for making 
games it can be a disaster.

Here is a quick chunk of code you can use to wait for an image to finish loading before you 
start drawing:


class ImageApplet extends Applet {

	public void init(){
		myImage = loadImage("MyPicture.gif");
		waitForImage(myImage);
	}

	void waitForImage(Image what){
	MediaTracker tracker = new MediaTracker(this) ;
		tracker.addImage(what,0) ;
		try {tracker.waitForAll();}
        	catch (Exception e){System.out.println("waitForImage() Error: "+e);}
	}
}



:::::: 'this' parameter
=======================

PLEASE NOTE that the drawImage() and getWidth() functions all have 'this' listed as a parameter.
That's because all image related functions need an ImageObserver.  This is a special kind of 
interface that lets the image talk to it's consumer (the object drawing the image).

FORTUNATELY all Components (including Containers and Applets) are ImageObservers.  The word 'this' 
indicates that your Applet is the ImageObserver for the ImageApplet code example.


:::::: Try and Catch
====================

Some functions throw exceptions when there is a problem.  If you don't 'catch' the exceptions 
then your application will halt.  When you catch an exception, you need to decide how to handle it.
Will it cause the function to fail?  Will it lead to other code failing?  You decide what to do.

'try' has a pair of brackets {} that can encompass as much code as you like.  
'catch' will catch the type of exceptions thrown inside the 'try' code. 
It also has a pair of brackets {} so you can respond with as much code as you need.

How do you know if a function throws an exception?  Once again, open up your JDK folders 
(see FAQ_2d) and find the function in question.  Here is the definition of the URL 
constructor that throws the MalformedURLException:

public URL(String spec) throws MalformedURLException {}


Please remember that Exceptions are CLASSES just like everything else in java.  I had to 
import java.net.* at the top of my ImageApplet in order to reference MalformedURLException().
I could have been more specific with my import command.  I could have done this:

import java.net.MalformedURLException;