home *** CD-ROM | disk | FTP | other *** search
/ Magazyn WWW 2000 June / www-06-2000.iso / java / BackgroundImage.java < prev    next >
Text File  |  2000-03-14  |  2KB  |  98 lines

  1. import java.awt.*;
  2. import java.awt.image.*;
  3. import java.util.*;
  4.  
  5. public class BackgroundImage implements ImageProducer {
  6.  
  7.     private int width;
  8.     private int height;
  9.     private int wa;
  10.     private int ha;
  11.     private ColorModel model;
  12.     private Hashtable properties;
  13.     private Vector theConsumers = new Vector();
  14.     private Rectangle area;
  15.  
  16.     public BackgroundImage(int w, int h) {
  17.         width = w;
  18.         height = h;
  19.         model = ColorModel.getRGBdefault();
  20.         properties = new Hashtable();
  21.         wa=w*6/10;
  22.         ha=h*6/10;
  23.         area=new Rectangle((w-wa)/2, (h-ha)/2, wa, ha);
  24.     }
  25.  
  26.     public synchronized void addConsumer(ImageConsumer ic) {
  27.         if (theConsumers.contains(ic)) {
  28.             return;
  29.         }
  30.         theConsumers.addElement(ic);
  31.         try {
  32.             initConsumer(ic);
  33.             sendPixels(ic, 0, 0, width, height);
  34.             if (isConsumer(ic)) {
  35.                 ic.imageComplete(ImageConsumer.STATICIMAGEDONE);
  36.                 if (isConsumer(ic)) {
  37.                     ic.imageComplete(ImageConsumer.IMAGEERROR);
  38.                     removeConsumer(ic);
  39.                 }
  40.             }
  41.         } catch (Exception e) {
  42.             if (isConsumer(ic)) {
  43.                 ic.imageComplete(ImageConsumer.IMAGEERROR);
  44.             }
  45.         }
  46.     }
  47.  
  48.     public synchronized boolean isConsumer(ImageConsumer ic) {
  49.         return theConsumers.contains(ic);
  50.     }
  51.  
  52.     public synchronized void removeConsumer(ImageConsumer ic) {
  53.         theConsumers.removeElement(ic);
  54.     }
  55.  
  56.     public void startProduction(ImageConsumer ic) {
  57.         addConsumer(ic);
  58.     }
  59.  
  60.     public void requestTopDownLeftRightResend(ImageConsumer ic) {
  61.     }
  62.  
  63.     private void initConsumer(ImageConsumer ic) {
  64.         if (isConsumer(ic)) {
  65.             ic.setDimensions(width, height);
  66.         }
  67.         if (isConsumer(ic)) {
  68.             ic.setProperties(properties);
  69.         }
  70.         if (isConsumer(ic)) {
  71.             ic.setColorModel(model);
  72.         }
  73.         if (isConsumer(ic)) {
  74.             ic.setHints(ImageConsumer.TOPDOWNLEFTRIGHT | ImageConsumer.COMPLETESCANLINES);
  75.         }
  76.     }
  77.  
  78.     private void sendPixels(ImageConsumer ic, int x, int y, int w, int h) {
  79.         int[] pixels=new int[width];
  80.         int r2=width*width+height*height;
  81.         int ra2=wa*wa+ha*ha;
  82.         if (isConsumer(ic)) {
  83.             for(int y1=y; y1<(y+h); y1++) {
  84.                 for(int x1=x; x1<(x+w); x1++) {
  85.                     if (!area.contains(x1, y1)) {
  86.                         pixels[x1]=(0xff << 24) | ((255 * (x1*x1+y1*y1)/r2) & 0xff);
  87.                     } else {
  88.                         int xa=wa-(x1-area.x);
  89.                         int ya=ha-(y1-area.y);
  90.                         pixels[x1]=(0xff << 24) | ((255 *(xa*xa+ya*ya)/ra2) & 0xff);
  91.                     }
  92.                 }
  93.                 ic.setPixels(x, y1, w, 1, model, pixels, x, width);
  94.             }
  95.         }
  96.     }
  97. }
  98.