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: * @author Igor V. Stolyarov
019: * @version $Revision$
020: */package java.awt;
021:
022: import java.awt.Composite;
023: import java.awt.CompositeContext;
024: import java.awt.RenderingHints;
025: import java.awt.image.ColorModel;
026:
027: import org.apache.harmony.awt.gl.ICompositeContext;
028: import org.apache.harmony.awt.internal.nls.Messages;
029:
030: public final class AlphaComposite implements Composite {
031:
032: public static final int CLEAR = 1;
033:
034: public static final int SRC = 2;
035:
036: public static final int DST = 9;
037:
038: public static final int SRC_OVER = 3;
039:
040: public static final int DST_OVER = 4;
041:
042: public static final int SRC_IN = 5;
043:
044: public static final int DST_IN = 6;
045:
046: public static final int SRC_OUT = 7;
047:
048: public static final int DST_OUT = 8;
049:
050: public static final int SRC_ATOP = 10;
051:
052: public static final int DST_ATOP = 11;
053:
054: public static final int XOR = 12;
055:
056: public static final AlphaComposite Clear = new AlphaComposite(CLEAR);
057:
058: public static final AlphaComposite Src = new AlphaComposite(SRC);
059:
060: public static final AlphaComposite Dst = new AlphaComposite(DST);
061:
062: public static final AlphaComposite SrcOver = new AlphaComposite(
063: SRC_OVER);
064:
065: public static final AlphaComposite DstOver = new AlphaComposite(
066: DST_OVER);
067:
068: public static final AlphaComposite SrcIn = new AlphaComposite(
069: SRC_IN);
070:
071: public static final AlphaComposite DstIn = new AlphaComposite(
072: DST_IN);
073:
074: public static final AlphaComposite SrcOut = new AlphaComposite(
075: SRC_OUT);
076:
077: public static final AlphaComposite DstOut = new AlphaComposite(
078: DST_OUT);
079:
080: public static final AlphaComposite SrcAtop = new AlphaComposite(
081: SRC_ATOP);
082:
083: public static final AlphaComposite DstAtop = new AlphaComposite(
084: DST_ATOP);
085:
086: public static final AlphaComposite Xor = new AlphaComposite(XOR);
087:
088: private int rule;
089: private float alpha;
090:
091: private AlphaComposite(int rule, float alpha) {
092: if (rule < CLEAR || rule > XOR) {
093: // awt.11D=Unknown rule
094: throw new IllegalArgumentException(Messages
095: .getString("awt.11D")); //$NON-NLS-1$
096: }
097: if (alpha < 0.0f || alpha > 1.0f) {
098: // awt.11E=Wrong alpha value
099: throw new IllegalArgumentException(Messages
100: .getString("awt.11E")); //$NON-NLS-1$
101: }
102:
103: this .rule = rule;
104: this .alpha = alpha;
105: }
106:
107: private AlphaComposite(int rule) {
108: this (rule, 1.0f);
109: }
110:
111: public CompositeContext createContext(ColorModel srcColorModel,
112: ColorModel dstColorModel, RenderingHints hints) {
113: return new ICompositeContext(this , srcColorModel, dstColorModel);
114: }
115:
116: @Override
117: public boolean equals(Object obj) {
118: if (!(obj instanceof AlphaComposite)) {
119: return false;
120: }
121: AlphaComposite other = (AlphaComposite) obj;
122: return (this .rule == other.getRule() && this .alpha == other
123: .getAlpha());
124: }
125:
126: @Override
127: public int hashCode() {
128: int hash = Float.floatToIntBits(alpha);
129: int tmp = hash >>> 24;
130: hash <<= 8;
131: hash |= tmp;
132: hash ^= rule;
133: return hash;
134: }
135:
136: public int getRule() {
137: return rule;
138: }
139:
140: public float getAlpha() {
141: return alpha;
142: }
143:
144: public static AlphaComposite getInstance(int rule, float alpha) {
145: if (alpha == 1.0f) {
146: return getInstance(rule);
147: }
148: return new AlphaComposite(rule, alpha);
149: }
150:
151: public static AlphaComposite getInstance(int rule) {
152: switch (rule) {
153: case CLEAR:
154: return Clear;
155: case SRC:
156: return Src;
157: case DST:
158: return Dst;
159: case SRC_OVER:
160: return SrcOver;
161: case DST_OVER:
162: return DstOver;
163: case SRC_IN:
164: return SrcIn;
165: case DST_IN:
166: return DstIn;
167: case SRC_OUT:
168: return SrcOut;
169: case DST_OUT:
170: return DstOut;
171: case SRC_ATOP:
172: return SrcAtop;
173: case DST_ATOP:
174: return DstAtop;
175: case XOR:
176: return Xor;
177: default:
178: // awt.11D=Unknown rule
179: throw new IllegalArgumentException(Messages
180: .getString("awt.11D")); //$NON-NLS-1$
181: }
182: }
183:
184: }
|