home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / awt / GradientPaintContext.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  4.6 KB  |  191 lines

  1. /*
  2.  * @(#)GradientPaintContext.java    1.12 98/03/18
  3.  *
  4.  * Copyright 1997 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.awt;
  16.  
  17. import java.awt.color.ColorSpace;
  18. import java.awt.image.Raster;
  19. import java.awt.image.WritableRaster;
  20. import sun.awt.image.IntegerComponentRaster;
  21. import java.awt.image.ColorModel;
  22. import java.awt.image.DirectColorModel;
  23. import java.awt.geom.Point2D;
  24.  
  25. class GradientPaintContext implements PaintContext {
  26.     static ColorModel xrgbmodel =
  27.     new DirectColorModel(24, 0x00ff0000, 0x0000ff00, 0x000000ff);
  28.  
  29.     double x1;
  30.     double y1;
  31.     double dx;
  32.     double dy;
  33.     boolean cyclic;
  34.     int interp[];
  35.     Raster saved;
  36.     ColorModel model;
  37.  
  38.     public GradientPaintContext(Point2D p1, Point2D p2,
  39.                 Color c1, Color c2,
  40.                 boolean cyclic) {
  41.     double x1, y1, x2, y2;
  42.     int rgb1, rgb2;
  43.     x1 = p1.getX();
  44.     x2 = p2.getX();
  45.     if (x1 > x2) {
  46.         y1 = x1;
  47.         x1 = x2;
  48.         x2 = y1;
  49.         y1 = p2.getY();
  50.         y2 = p1.getY();
  51.         rgb1 = c2.getRGBA();
  52.         rgb2 = c1.getRGBA();
  53.     } else {
  54.         y1 = p1.getY();
  55.         y2 = p2.getY();
  56.         rgb1 = c1.getRGBA();
  57.         rgb2 = c2.getRGBA();
  58.     }
  59.     double dx = x2 - x1;
  60.     double dy = y2 - y1;
  61.     double lenSq = dx * dx + dy * dy;
  62.     this.x1 = x1;
  63.     this.y1 = y1;
  64.     if (lenSq >= Double.MIN_VALUE) {
  65.         dx = dx / lenSq;
  66.         dy = dy / lenSq;
  67.         if (cyclic) {
  68.         dx = dx % 1.0;
  69.         dy = dy % 1.0;
  70.         }
  71.     }
  72.     this.dx = dx;
  73.     this.dy = dy;
  74.     this.cyclic = cyclic;
  75.     int a1 = (rgb1 >> 24) & 0xff;
  76.     int r1 = (rgb1 >> 16) & 0xff;
  77.     int g1 = (rgb1 >>  8) & 0xff;
  78.     int b1 = (rgb1      ) & 0xff;
  79.     int da = ((rgb2 >> 24) & 0xff) - a1;
  80.     int dr = ((rgb2 >> 16) & 0xff) - r1;
  81.     int dg = ((rgb2 >>  8) & 0xff) - g1;
  82.     int db = ((rgb2      ) & 0xff) - b1;
  83.     if (((rgb1 & rgb2) >>> 24) == 0xff) {
  84.         model = xrgbmodel;
  85.     } else {
  86.         model = ColorModel.getRGBdefault();
  87.     }
  88.     interp = new int[cyclic ? 513 : 257];
  89.     for (int i = 0; i <= 256; i++) {
  90.         float rel = i / 256.0f;
  91.         int rgb =
  92.         (((int) (a1 + da * rel)) << 24) |
  93.         (((int) (r1 + dr * rel)) << 16) |
  94.         (((int) (g1 + dg * rel)) <<  8) |
  95.         (((int) (b1 + db * rel))      );
  96.         interp[i] = rgb;
  97.         if (cyclic) {
  98.         interp[512 - i] = rgb;
  99.         }
  100.     }
  101.     }
  102.     
  103.     /**
  104.      * Release the resources allocated for the operation.
  105.      */
  106.     public void dispose() {
  107.     saved = null;
  108.     }
  109.  
  110.     /**
  111.      * Return the ColorModel of the output.
  112.      */
  113.     public ColorModel getColorModel() {
  114.         return model;
  115.     }
  116.  
  117.     /**
  118.      * Return a Raster containing the colors generated for the graphics
  119.      * operation.
  120.      * @param x,y,w,h The area in device space for which colors are
  121.      * generated.
  122.      */
  123.     public Raster getRaster(int x, int y, int w, int h) {
  124.     double rowrel = (x - x1) * dx + (y - y1) * dy;
  125.  
  126.     Raster rast = saved;
  127.     if (rast == null || rast.getWidth() < w || rast.getHeight() < h) {
  128.         rast = getColorModel().createCompatibleWritableRaster(w, h);
  129.         saved = rast;
  130.     }
  131.     IntegerComponentRaster irast = (IntegerComponentRaster) rast;
  132.     int off = irast.getDataOffset(0);
  133.     int adjust = irast.getScanlineStride() - w;
  134.     int[] pixels = irast.getDataStorage();
  135.  
  136.     if (cyclic) {
  137.         cycleFillRaster(pixels, off, adjust, w, h, rowrel, dx, dy);
  138.     } else {
  139.         clipFillRaster(pixels, off, adjust, w, h, rowrel, dx, dy);
  140.     }
  141.  
  142.     return rast;
  143.     }
  144.  
  145.     void cycleFillRaster(int[] pixels, int off, int adjust, int w, int h,
  146.              double rowrel, double dx, double dy) {
  147.     rowrel = rowrel % 2.0;
  148.     int irowrel = ((int) (rowrel * (1 << 30))) << 1;
  149.     int idx = (int) (-dx * (1 << 31));
  150.     int idy = (int) (-dy * (1 << 31));
  151.     while (--h >= 0) {
  152.         int icolrel = irowrel;
  153.         for (int j = w; j > 0; j--) {
  154.         pixels[off++] = interp[icolrel >>> 23];
  155.         icolrel += idx;
  156.         }
  157.  
  158.         off += adjust;
  159.         irowrel += idy;
  160.         }
  161.     }
  162.  
  163.     void clipFillRaster(int[] pixels, int off, int adjust, int w, int h,
  164.             double rowrel, double dx, double dy) {
  165.     while (--h >= 0) {
  166.         double colrel = rowrel;
  167.         int j = w;
  168.         if (colrel <= 0.0) {
  169.         int rgb = interp[0];
  170.         do {
  171.             pixels[off++] = rgb;
  172.             colrel += dx;
  173.         } while (--j > 0 && colrel <= 0.0);
  174.         }
  175.         while (colrel < 1.0 && --j >= 0) {
  176.         pixels[off++] = interp[(int) (colrel * 256)];
  177.         colrel += dx;
  178.         }
  179.         if (j > 0) {
  180.         int rgb = interp[256];
  181.         do {
  182.             pixels[off++] = rgb;
  183.         } while (--j > 0);
  184.         }
  185.  
  186.         off += adjust;
  187.         rowrel += dy;
  188.         }
  189.     }
  190. }
  191.