Source of ImagesApplet.java


  1: //ImagesApplet.java
  2: //Applet to display photographic images on the screen

  4: import java.awt.*;
  5: import java.awt.event.*;
  6: import java.applet.*;

  8: public class ImagesApplet extends Applet
  9: {
 10:     //Declare names of four Image variables.
 11:     //Three are jpeg files and one is a gif file.
 12:     Image dancers,
 13:           mask,
 14:           figure,
 15:           calculator;

 17:     //Override init() to load image files.
 18:     public void init()
 19:     {
 20:         dancers    = getImage(getDocumentBase(),
 21:                               "images/dancers.jpg");
 22:         mask       = getImage(getDocumentBase(),
 23:                               "images/mask.jpg");
 24:         figure     = getImage(getDocumentBase(),
 25:                               "images/figure.jpg");
 26:         calculator = getImage(getDocumentBase(),
 27:                               "images/calculator.gif");
 28:     }

 30:     //Display images on the screen; one image is displayed twice.
 31:     public void paint(Graphics g)
 32:     {
 33:         g.drawImage(dancers,     100, 100, 150, 120, this);
 34:         g.drawImage(mask,        300, 100, 150, 120, this);
 35:         g.drawImage(figure,      500, 100, 150, 120, this);
 36:         g.drawImage(calculator,  200, 240, 150, 120, this);
 37:         g.drawImage(calculator,  465, 290,  30,  20, this);
 38:     }
 39: }

 41: /*
 42: getDocumentBase() is an Applet method that returns
 43: the URL of the document containing the applet.

 45: getCodeBase() is an Applet method that returns
 46: the URL of the applet itself.

 48: When the html file and the applet class file are
 49: in the same directory, as they are for this example,
 50: either could be used in the above context.
 51: */