APPLETS AND IMAGES

CONTENTS

1. Introduction
2. Example Applet (with Image)
3. Image map



1. INTRODUCTION

As with GUIs with can display images in applets using similar techniques as those described before.




2. SIMPLE EXAMPLE

Some example code is given in Table 1 that displays an image.

// Image Viewer
// Frans Coenen
// Dept. Comp. Sci., University of Liverpool
// Wednesday 31 January 20001

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

public class ImageExample extends Applet {

    /* ------ FIELDS ------ */

    Image squareObject;
    
    /* ------ METHODS ------ */
    
    /* INIT: Override init() */
    
    public void init() {
    	squareObject = getImage(getDocumentBase(),"squareObject1.gif");
	}
	
    /* PAINT */
    
    public void paint(Graphics g) {
        g.drawImage(squareObject,20,40,300,300,this);
	}
    }

Table 1: Sound applet

The associated .html file is given in Table 2 below; click here to view the effect.

.

< HTML >
< HEAD >
< TITLE >APPLET EXAMPLE (IMAGE)< /TITLE >
< /HEAD >
< BODY >
< H1 >APPLET EXAMPLE (IMAGE)< /H1 >
< CENTER >
< APPLET code=ImageExample.class width=340 height=400 >
< /APPLET >
< /CENTER >
< /BODY >
< /HTML >

Table 2: "image" HTML file




3. IMAGE MAP

The exampe code given in Table 3 extends that given in Table 1 to produce an image map, i.e. an image with "hot" areas.

// Image Example 2 (Image map)
// Frans Coenen
// Dept. Comp. Sci., University of Liverpool
// Wednesday 31 January 20001

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

public class ImageExample2 extends Applet implements MouseMotionListener {

    /* ------ FIELDS ------ */

    Image squareObject;
    
    /* ------ METHODS ------ */
    
    /* INIT: Override init() */
    
    public void init() {
    	squareObject = getImage(getDocumentBase(),"squareObject1.gif");
	
	// Add motion listener
	
	addMouseMotionListener(this);
	}
	
    /* PAINT */
    
    public void paint(Graphics g) {
        g.drawImage(squareObject,20,40,300,300,this);
	}
    
    /* MOUSE DRAGGED */
   
    public void mouseDragged(MouseEvent event) {}
    
    /* MOUSE MOVED */
    
    public void mouseMoved(MouseEvent event) {
        int y = event.getY();
    	int x = event.getX();
	
        Graphics g = getGraphics();
	
	// Erase previous name from window
	
	g.setColor(Color.white);
	g.fillRect(70,355,200,45);
	g.setColor(Color.black);
	
	// Displ;ay body part
	
	if (x>15 && x < 125) {
	    if (110 < y && y < 235) g.drawString("Right arm",80,370); 
	    }
	else {    
	    if (x>265 && x < 320) {
		if (110 < y && y < 235) g.drawString("Left arm",80,370); 
		}
	    else {
	        if (x>=125 && x < =265) {
	            if (y>40 && y < 110) g.drawString("Top of Head",80,370);
		    else {
		        if (y>=110 && y < 215) g.drawString("Square face",80,370);
		        else {
		            if (y>=215 && y < 255) g.drawString("Legs",80,370);
			    else if (y>=255 && y < 335) g.drawString("Red boots",80,370);
			    }
			}
		    }
		}
	    }
	}
    }   

Table 3: Sound applet

The associated .html file is given in Table 4 below; click here to view the effect.

.

< HTML >
< HEAD >
< TITLE >APPLET EXAMPLE (IMAGE MAP)< /TITLE >
< /HEAD >
< BODY >
< H1 >APPLET EXAMPLE (IMAGE MAP)< /H1 >
< CENTER >
< APPLET code=ImageExample.class width=340 height=400 >
< /APPLET >
< /CENTER >
< /BODY >
< /HTML >  

Table 2: "image" HTML file




Created and maintained by Frans Coenen. Last updated 01 February 2001