home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-03-20 | 3.5 KB | 119 lines |
- /*
- * @(#)TexturePaintContext.java 1.9 98/03/18
- *
- * Copyright 1997, 1998 by Sun Microsystems, Inc.,
- * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
- * All rights reserved.
- *
- * This software is the confidential and proprietary information
- * of Sun Microsystems, Inc. ("Confidential Information"). You
- * shall not disclose such Confidential Information and shall use
- * it only in accordance with the terms of the license agreement
- * you entered into with Sun.
- */
-
- package java.awt;
-
- import java.awt.color.ColorSpace;
- import java.awt.image.BufferedImage;
- import java.awt.image.Raster;
- import java.awt.image.WritableRaster;
- import java.awt.image.ColorModel;
-
- class TexturePaintContext implements PaintContext {
-
- BufferedImage bufImg;
- WritableRaster textureRaster;
- int xOrg;
- int yOrg;
- int bWidth;
- int bHeight;
-
- public TexturePaintContext(BufferedImage bufImg, int x, int y) {
- this.bufImg = bufImg;
- this.textureRaster = bufImg.getRaster();
- this.xOrg = x;
- this.yOrg = y;
- this.bWidth = bufImg.getWidth();
- this.bHeight = bufImg.getHeight();
- }
-
- /**
- * Release the resources allocated for the operation.
- */
- public void dispose() {
- // Nothing to dispose
- }
-
- /**
- * Return the ColorModel of the output.
- */
- public ColorModel getColorModel() {
- return bufImg.getColorModel();
- }
-
- int calcUpperLeft(int a, int w, int b) {
- int d = Math.abs((b-a)%w);
- if (a > b) {
- return (b-(w-d));
- } else {
- return (b-d);
- }
- }
- int calcLowerRight(int a, int w, int b) {
- int d = Math.abs((b-a)%w);
- if (a > b) {
- return (b+d);
- } else {
- return (b+(w-d));
- }
- }
-
- /**
- * Return a Raster containing the colors generated for the graphics
- * operation.
- * @param x,y,w,h The area in device space for which colors are
- * generated.
- */
- public Raster getRaster(int x, int y, int w, int h) {
- int x1 = calcUpperLeft(xOrg, bWidth, x);
- int y1 = calcUpperLeft(yOrg, bHeight, y);
- int x2 = calcLowerRight(xOrg, bWidth, x+w);
- int y2 = calcLowerRight(yOrg, bHeight, y+h);
- int nW = ((x2 - x1)/bWidth);
- int nH = ((y2 - y1)/bHeight);
- int xPos = 0;
- int yPos = 0;
- Rectangle rasterRect = new Rectangle(x, y, w, h);
- Rectangle txtrRect = new Rectangle(0, 0, bWidth, bHeight);
- Rectangle subrRect = new Rectangle();
- int locx;
- int locy;
-
- Raster subR;
-
- WritableRaster t = textureRaster.createCompatibleWritableRaster(w, h);
-
- for (int i = 0 ; i < nH ; i++) {
- xPos = 0;
- for (int j = 0 ; j < nW ; j++) {
- locx = (j*bWidth)+x1;
- locy = (i*bHeight)+y1;
- txtrRect.setLocation(locx, locy);
- subrRect = rasterRect.intersection(txtrRect);
-
- subR = textureRaster.createSubRaster(subrRect.x-locx,
- subrRect.y-locy,
- subrRect.width,
- subrRect.height,
- 0, 0, null);
- t.setPixelData(xPos, yPos, subR);
- xPos += subrRect.width;
- }
- yPos += subrRect.height;
- }
- return t;
- }
-
- }
-