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

  1. /*
  2.  * @(#)TexturePaintContext.java    1.9 98/03/18
  3.  *
  4.  * Copyright 1997, 1998 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.BufferedImage;
  19. import java.awt.image.Raster;
  20. import java.awt.image.WritableRaster;
  21. import java.awt.image.ColorModel;
  22.  
  23. class TexturePaintContext implements PaintContext {
  24.  
  25.     BufferedImage bufImg;
  26.     WritableRaster textureRaster;
  27.     int xOrg;
  28.     int yOrg;
  29.     int bWidth;
  30.     int bHeight;
  31.     
  32.     public TexturePaintContext(BufferedImage bufImg, int x, int y) {
  33.         this.bufImg = bufImg;
  34.         this.textureRaster = bufImg.getRaster();
  35.         this.xOrg = x;
  36.         this.yOrg = y;
  37.         this.bWidth = bufImg.getWidth();
  38.         this.bHeight = bufImg.getHeight();
  39.     }
  40.     
  41.     /**
  42.      * Release the resources allocated for the operation.
  43.      */
  44.     public void dispose() {
  45.         // Nothing to dispose
  46.     }
  47.  
  48.     /**
  49.      * Return the ColorModel of the output.
  50.      */
  51.     public ColorModel getColorModel() {
  52.         return bufImg.getColorModel();
  53.     }
  54.  
  55.     int calcUpperLeft(int a, int w, int b) {
  56.         int d = Math.abs((b-a)%w);
  57.         if (a > b) {
  58.             return (b-(w-d));
  59.         } else {
  60.             return (b-d);
  61.         }
  62.     }
  63.     int calcLowerRight(int a, int w, int b) {
  64.         int d = Math.abs((b-a)%w);
  65.         if (a > b) {
  66.             return (b+d);
  67.         } else {
  68.             return (b+(w-d));
  69.         }
  70.     }
  71.     
  72.     /**
  73.      * Return a Raster containing the colors generated for the graphics
  74.      * operation.
  75.      * @param x,y,w,h The area in device space for which colors are
  76.      * generated.
  77.      */
  78.     public Raster getRaster(int x, int y, int w, int h) {
  79.         int x1 = calcUpperLeft(xOrg, bWidth, x);
  80.         int y1 = calcUpperLeft(yOrg, bHeight, y);
  81.         int x2 = calcLowerRight(xOrg, bWidth, x+w);
  82.         int y2 = calcLowerRight(yOrg, bHeight, y+h);
  83.         int nW = ((x2 - x1)/bWidth);
  84.         int nH = ((y2 - y1)/bHeight);
  85.         int xPos = 0;
  86.         int yPos = 0;
  87.         Rectangle rasterRect = new Rectangle(x, y, w, h);
  88.         Rectangle txtrRect = new Rectangle(0, 0, bWidth, bHeight);
  89.         Rectangle subrRect = new Rectangle();
  90.         int locx;
  91.         int locy;
  92.         
  93.         Raster subR;
  94.  
  95.         WritableRaster t = textureRaster.createCompatibleWritableRaster(w, h);
  96.  
  97.         for (int i = 0 ; i < nH ; i++) {
  98.             xPos = 0;
  99.             for (int j = 0 ; j < nW ; j++) {
  100.                 locx = (j*bWidth)+x1;
  101.                 locy = (i*bHeight)+y1;
  102.                 txtrRect.setLocation(locx, locy);
  103.                 subrRect = rasterRect.intersection(txtrRect);
  104.                 
  105.                 subR = textureRaster.createSubRaster(subrRect.x-locx,
  106.                                                      subrRect.y-locy,
  107.                                                      subrRect.width,
  108.                                                      subrRect.height,
  109.                                                      0, 0, null);
  110.                 t.setPixelData(xPos, yPos, subR);
  111.                 xPos += subrRect.width;
  112.             }
  113.             yPos += subrRect.height;
  114.         }
  115.         return t;
  116.     }
  117.  
  118. }
  119.