home *** CD-ROM | disk | FTP | other *** search
Java Source | 2000-03-14 | 2.4 KB | 98 lines |
- import java.awt.*;
- import java.awt.image.*;
- import java.util.*;
-
- public class BackgroundImage implements ImageProducer {
-
- private int width;
- private int height;
- private int wa;
- private int ha;
- private ColorModel model;
- private Hashtable properties;
- private Vector theConsumers = new Vector();
- private Rectangle area;
-
- public BackgroundImage(int w, int h) {
- width = w;
- height = h;
- model = ColorModel.getRGBdefault();
- properties = new Hashtable();
- wa=w*6/10;
- ha=h*6/10;
- area=new Rectangle((w-wa)/2, (h-ha)/2, wa, ha);
- }
-
- public synchronized void addConsumer(ImageConsumer ic) {
- if (theConsumers.contains(ic)) {
- return;
- }
- theConsumers.addElement(ic);
- try {
- initConsumer(ic);
- sendPixels(ic, 0, 0, width, height);
- if (isConsumer(ic)) {
- ic.imageComplete(ImageConsumer.STATICIMAGEDONE);
- if (isConsumer(ic)) {
- ic.imageComplete(ImageConsumer.IMAGEERROR);
- removeConsumer(ic);
- }
- }
- } catch (Exception e) {
- if (isConsumer(ic)) {
- ic.imageComplete(ImageConsumer.IMAGEERROR);
- }
- }
- }
-
- public synchronized boolean isConsumer(ImageConsumer ic) {
- return theConsumers.contains(ic);
- }
-
- public synchronized void removeConsumer(ImageConsumer ic) {
- theConsumers.removeElement(ic);
- }
-
- public void startProduction(ImageConsumer ic) {
- addConsumer(ic);
- }
-
- public void requestTopDownLeftRightResend(ImageConsumer ic) {
- }
-
- private void initConsumer(ImageConsumer ic) {
- if (isConsumer(ic)) {
- ic.setDimensions(width, height);
- }
- if (isConsumer(ic)) {
- ic.setProperties(properties);
- }
- if (isConsumer(ic)) {
- ic.setColorModel(model);
- }
- if (isConsumer(ic)) {
- ic.setHints(ImageConsumer.TOPDOWNLEFTRIGHT | ImageConsumer.COMPLETESCANLINES);
- }
- }
-
- private void sendPixels(ImageConsumer ic, int x, int y, int w, int h) {
- int[] pixels=new int[width];
- int r2=width*width+height*height;
- int ra2=wa*wa+ha*ha;
- if (isConsumer(ic)) {
- for(int y1=y; y1<(y+h); y1++) {
- for(int x1=x; x1<(x+w); x1++) {
- if (!area.contains(x1, y1)) {
- pixels[x1]=(0xff << 24) | ((255 * (x1*x1+y1*y1)/r2) & 0xff);
- } else {
- int xa=wa-(x1-area.x);
- int ya=ha-(y1-area.y);
- pixels[x1]=(0xff << 24) | ((255 *(xa*xa+ya*ya)/ra2) & 0xff);
- }
- }
- ic.setPixels(x, y1, w, 1, model, pixels, x, width);
- }
- }
- }
- }
-