|
/* ImageStatusMessage.java
*
* A Java Applet demonstrating how to use an image map to display messages
* on the window status bar.
*
* Authored by Khim Theng
* khim_theng@completeesolutions.com
*
* Last modified on 05/11/2000
*/
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*; //Classes to handle events
public class ImageStatusMessage extends Applet implements MouseListener, MouseMotionListener
{
Image mapImage; //Declare an image object
MediaTracker trackImage; //Used to track images
int width, height; //Variables to hold the width and height
public void init()
{
addMouseListener(this); //Register MouseListener
addMouseMotionListener(this); //Register MouseMotionListener
trackImage = new MediaTracker(this); //Used to determine when an image is loaded completely
//getDocumentBase() method indicates where to locate the HTML file
//The second argument is the name of the image map.
mapImage = getImage(getDocumentBase(), "3dogs.jpg");
//Register the loading image with the MediaTracker object trackImage
trackImage.addImage(mapImage, 0);
try
{
trackImage.waitForAll();
}
catch(InterruptedException e) {}
width = mapImage.getWidth(this);
height = mapImage.getHeight(this);
resize(width, height);
} //End of init
public void paint(Graphics g)
{
g.drawImage(mapImage, 0, 0, this);
}
public void mouseMoved(MouseEvent e)
{
//Display the message on the status bar when the mouse is over the image
showStatus(displayLocationMessage(e.getX()));
}
//Empty abstract methods
public void mouseExited(MouseEvent e) {}
public void mouseDragged(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {}
public String displayLocationMessage(int x)
{
//Determine the width of each icon
int iconWidth = width/3;
//Output the value
if (x >= 0 && x <= iconWidth)
return "Tung! Tung! My boy!";
else if (x > iconWidth && x <= iconWidth * 2)
return "Teeny-weeny! What a sweet dog!";
else if (x > iconWidth * 2 && x <= iconWidth * 3)
return "Truc! The cleanest dog in the whole wide world!";
return "";
}
} //End of ImageStatusMessage
|