home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / javafile / ch07 / ImageApp.java < prev    next >
Encoding:
Java Source  |  1998-12-14  |  1.3 KB  |  69 lines

  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.applet.Applet;
  4.  
  5. /*
  6.  * display a series of images
  7.  */
  8. public class ImageApp extends Applet implements ActionListener
  9. {
  10.  
  11. /*
  12.  * the number of images to load
  13.  */
  14. final int NumImages = 6;
  15. Button button1;
  16.  
  17.  
  18. /*
  19.  * an array to hold the images
  20.  */
  21. Image imgs[] = new Image[NumImages];
  22.  
  23. /*
  24.  * which image is currently displayed
  25.  */
  26. int which = 0;
  27.  
  28. /*
  29.  * init method runs when applet is loaded or reloaded
  30.  */
  31. public void init() {
  32.  
  33.     setLayout(new BorderLayout());
  34.  
  35.     Panel p = new Panel ();
  36.     add("South", p);
  37.     button1 = new Button("Next Image");
  38.     p.add (button1);
  39.     button1.addActionListener(this);
  40.     for (int i=0; i < NumImages; i+=1) {
  41.         String name = "Globe"+(i+1)+".gif";
  42.         imgs[i] = getImage (getDocumentBase(),name); 
  43.     }
  44. }
  45.  
  46. /**
  47.  * paint method is called by update
  48.  * draw the current image
  49.  * @param g - destination graphics object
  50.  */
  51. public void paint (Graphics g) {
  52.  
  53.     g.drawImage (imgs[which], 10, 10, this);
  54. }
  55.  
  56. /**
  57.  * switch to next image when button is pressed
  58.  * @param evt - event object
  59.  * @param arg - target object
  60.  */
  61. public void actionPerformed(ActionEvent evt)
  62. {
  63.         which += 1;
  64.         which %= NumImages;    // wrap around to zero
  65.         repaint ();    // causes update as soon as possible
  66. }
  67. }
  68.  
  69.