home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / ui / drawing / example / ImageUpdater.java < prev    next >
Encoding:
Java Source  |  1997-07-13  |  3.2 KB  |  88 lines

  1. /*
  2.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. import java.applet.*;
  18. import java.awt.*;
  19. import java.awt.image.ImageObserver;
  20.  
  21. public class ImageUpdater extends Applet {
  22.     /*
  23.      * Written by Jim Graham.
  24.      * This applet draws a big image scaled to its width and height as
  25.      * specified in the <APPLET> tag, and a small image scaled by the
  26.      * same ratio as the big image and positioned in the center of it.
  27.      */
  28.     Image bigimg, smallimg;
  29.     int smallx, smally, smallw, smallh;
  30.     boolean sizeknown = false;
  31.     boolean errored = false;
  32.  
  33.     public void init() {
  34.         bigimg = getImage(getCodeBase(), "../images/bigimg.gif");
  35.         smallimg = getImage(getCodeBase(), "../images/smallimg.gif");
  36.         positionImages();
  37.     }
  38.  
  39.     public boolean imageUpdate(Image theimg, int infoflags,
  40.                                int x, int y, int w, int h) {
  41.         if ((infoflags & (ERROR)) != 0) {
  42.             errored = true;
  43.         }
  44.         if ((infoflags & (WIDTH | HEIGHT)) != 0) {
  45.         positionImages();
  46.         }
  47.         boolean done = ((infoflags & (ERROR | FRAMEBITS | ALLBITS)) != 0);
  48.         // Repaint immediately if we are done, otherwise batch up
  49.         // repaint requests every 100 milliseconds
  50.         repaint(done ? 0 : 100);
  51.         return !done;
  52.     }
  53.  
  54.     public synchronized void positionImages() {
  55.     Dimension d = size();
  56.         int bigw = bigimg.getWidth(this);
  57.         int bigh = bigimg.getHeight(this);
  58.         smallw = smallimg.getWidth(this); 
  59.         smallh = smallimg.getHeight(this); 
  60.         if (bigw < 0 || bigh < 0 || smallw < 0 || smallh < 0) {
  61.             return;
  62.         }
  63.         smallw = smallw * d.width / bigw;
  64.         smallh = smallh * d.height / bigh;
  65.         smallx = (d.width - smallw) / 2;
  66.         smally = (d.height - smallh) / 2;
  67.         sizeknown = true;
  68.     }
  69.  
  70.     public synchronized void paint(Graphics g) {
  71.     Dimension d = size();
  72.         int appw = d.width;
  73.         int apph = d.height;
  74.         if (errored) {
  75.             // The images had a problem - just draw a big red rectangle
  76.             g.setColor(Color.red);
  77.             g.fillRect(0, 0, appw, apph);
  78.             return;
  79.         }
  80.         // Scale the big image to the width and height of the applet
  81.         g.drawImage(bigimg, 0, 0, appw, apph, this);
  82.         if (sizeknown) {
  83.             // Scale the small image to the central region calculated above.
  84.             g.drawImage(smallimg, smallx, smally, smallw, smallh, this);
  85.         }
  86.     }
  87. }
  88.