001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package java.awt;
019:
020: import java.awt.geom.AffineTransform;
021: import java.awt.geom.Rectangle2D;
022: import java.awt.image.BufferedImage;
023: import java.awt.image.ColorModel;
024: import java.awt.image.DataBuffer;
025: import java.awt.image.DataBufferByte;
026: import java.awt.image.DataBufferInt;
027: import java.awt.image.DataBufferUShort;
028:
029: import org.apache.harmony.awt.internal.nls.Messages;
030:
031: public class TexturePaint implements Paint {
032: /**
033: * The BufferedImage object used as texture
034: */
035: BufferedImage img;
036:
037: /**
038: * The Rectangle2D bounds of texture piece to be painted
039: */
040: Rectangle2D anchor;
041:
042: public TexturePaint(BufferedImage img, Rectangle2D anchor) {
043: if (img == null) {
044: // awt.114=Image is null
045: throw new NullPointerException(Messages
046: .getString("awt.114")); //$NON-NLS-1$
047: }
048: if (anchor == null) {
049: // awt.115=Anchor is null
050: throw new NullPointerException(Messages
051: .getString("awt.115")); //$NON-NLS-1$
052: }
053: this .img = img;
054: this .anchor = anchor;
055: }
056:
057: public BufferedImage getImage() {
058: return img;
059: }
060:
061: public PaintContext createContext(ColorModel cm, Rectangle device,
062: Rectangle2D user, AffineTransform t, RenderingHints hints) {
063: Object value = hints.get(RenderingHints.KEY_INTERPOLATION);
064: boolean bilinear = (value != null)
065: && (value != RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
066: int type = img.getType();
067: DataBuffer buf = img.getRaster().getDataBuffer();
068: if (buf instanceof DataBufferInt) {
069: if (type == BufferedImage.TYPE_INT_ARGB
070: || type == BufferedImage.TYPE_INT_ARGB_PRE
071: || type == BufferedImage.TYPE_INT_BGR
072: || type == BufferedImage.TYPE_INT_RGB) {
073: if (bilinear) {
074: return new TexturePaintContext.IntBilinear(img,
075: anchor, t);
076: }
077: return new TexturePaintContext.IntSimple(img, anchor, t);
078: }
079: } else if (buf instanceof DataBufferByte) {
080: if (type == BufferedImage.TYPE_BYTE_GRAY) {
081: if (bilinear) {
082: return new TexturePaintContext.ByteBilinear(img,
083: anchor, t);
084: }
085: return new TexturePaintContext.ByteSimple(img, anchor,
086: t);
087: }
088: } else if (buf instanceof DataBufferUShort) {
089: if (type == BufferedImage.TYPE_USHORT_GRAY) {
090: if (bilinear) {
091: return new TexturePaintContext.ShortBilinear(img,
092: anchor, t);
093: }
094: return new TexturePaintContext.ShortSimple(img, anchor,
095: t);
096: }
097: }
098: if (bilinear) {
099: if (type != BufferedImage.TYPE_BYTE_INDEXED) {
100: return new TexturePaintContext.CommonBilinear(img,
101: anchor, t);
102: }
103: } else {
104: return new TexturePaintContext.CommonSimple(img, anchor, t);
105: }
106: return new TexturePaintContext(img, anchor, t);
107: }
108:
109: public int getTransparency() {
110: return img.getColorModel().getTransparency();
111: }
112:
113: public Rectangle2D getAnchorRect() {
114: return anchor;
115: }
116: }
|