001: /*
002: * @(#)AlphaComposite.java 1.7 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: *
026: */
027:
028: package java.awt;
029:
030: public final class AlphaComposite implements Composite {
031: /**
032: * Porter-Duff Clear rule.
033: * Both the color and the alpha of the destination are cleared.
034: * Neither the source nor the destination is used as input.
035: *<p>
036: * Fs = 0 and Fd = 0, thus:
037: *<pre>
038: * Cd = 0
039: * Ad = 0
040: *</pre>
041: *
042: */
043: public static final int CLEAR = 1;
044: /**
045: * Porter-Duff Source rule.
046: * The source is copied to the destination.
047: * The destination is not used as input.
048: *<p>
049: * Fs = 1 and Fd = 0, thus:
050: *<pre>
051: * Cd = Cs
052: * Ad = As
053: *</pre>
054: */
055: public static final int SRC = 2;
056: /**
057: * Porter-Duff Source Over Destination rule.
058: * The source is composited over the destination.
059: *<p>
060: * Fs = 1 and Fd = (1-As), thus:
061: *<pre>
062: * Cd = Cs + Cd*(1-As)
063: * Ad = As + Ad*(1-As)
064: *</pre>
065: */
066: public static final int SRC_OVER = 3;
067: /**
068: * <code>AlphaComposite</code> object that implements the opaque CLEAR rule
069: * with an alpha of 1.0f.
070: * @see #CLEAR
071: */
072: public static final AlphaComposite Clear = new AlphaComposite(CLEAR);
073: /**
074: * <code>AlphaComposite</code> object that implements the opaque SRC rule
075: * with an alpha of 1.0f.
076: * @see #SRC
077: */
078: public static final AlphaComposite Src = new AlphaComposite(SRC);
079: /**
080: * <code>AlphaComposite</code> object that implements the opaque SRC_OVER rule
081: * with an alpha of 1.0f.
082: * @see #SRC_OVER
083: */
084: public static final AlphaComposite SrcOver = new AlphaComposite(
085: SRC_OVER);
086: private static final int MIN_RULE = CLEAR;
087: private static final int MAX_RULE = SRC_OVER;
088: float extraAlpha;
089: int rule;
090:
091: private AlphaComposite(int rule) {
092: this (rule, 1.0f);
093: }
094:
095: private AlphaComposite(int rule, float alpha) {
096: if (alpha < 0.0f || alpha > 1.0f) {
097: throw new IllegalArgumentException(
098: "alpha value out of range");
099: }
100: if (rule < MIN_RULE || rule > MAX_RULE) {
101: throw new IllegalArgumentException("unknown composite rule");
102: }
103: this .rule = rule;
104: this .extraAlpha = alpha;
105: }
106:
107: /**
108: * Creates an <code>AlphaComposite</code> object with the specified rule.
109: * @param rule the compositing rule
110: * @throws IllegalArgumentException if <code>rule</code> is not one of
111: * the following: {@link #CLEAR}, {@link #SRC}, {@link #DST},
112: * {@link #SRC_OVER}, {@link #DST_OVER}, {@link #SRC_IN},
113: * {@link #DST_IN}, {@link #SRC_OUT}, {@link #DST_OUT},
114: * {@link #SRC_ATOP}, {@link #DST_ATOP}, or {@link #XOR}
115: */
116: public static AlphaComposite getInstance(int rule) {
117: switch (rule) {
118: case CLEAR:
119: return Clear;
120:
121: case SRC:
122: return Src;
123:
124: case SRC_OVER:
125: return SrcOver;
126:
127: default:
128: throw new IllegalArgumentException("unknown composite rule");
129: }
130: }
131:
132: /**
133: * Creates an <code>AlphaComposite</code> object with the specified rule and
134: * the constant alpha to multiply with the alpha of the source.
135: * The source is multiplied with the specified alpha before being composited
136: * with the destination.
137: * @param rule the compositing rule
138: * @param alpha the constant alpha to be multiplied with the alpha of
139: * the source. <code>alpha</code> must be a floating point number in the
140: * inclusive range [0.0, 1.0].
141: */
142: public static AlphaComposite getInstance(int rule, float alpha) {
143: if (alpha == 1.0f) {
144: return getInstance(rule);
145: }
146: return new AlphaComposite(rule, alpha);
147: }
148:
149: /**
150: * Returns the alpha value of this<code>AlphaComposite</code>. If this
151: * <code>AlphaComposite</code> does not have an alpha value, 1.0 is returned.
152: * @return the alpha value of this <code>AlphaComposite</code>.
153: */
154: public float getAlpha() {
155: return extraAlpha;
156: }
157:
158: /**
159: * Returns the compositing rule of this <code>AlphaComposite</code>.
160: * @return the compositing rule of this <code>AlphaComposite</code>.
161: */
162: public int getRule() {
163: return rule;
164: }
165:
166: /**
167: * Returns the hashcode for this composite.
168: * @return a hash code for this composite.
169: */
170: public int hashCode() {
171: return (Float.floatToIntBits(extraAlpha) * 31 + rule);
172: }
173:
174: /**
175: * Tests if the specified {@link Object} is equal to this
176: * <code>AlphaComposite</code> object.
177: * @param obj the <code>Object</code> to test for equality
178: * @return <code>true</code> if <code>obj</code> equals this
179: * <code>AlphaComposite</code>; <code>false</code> otherwise.
180: */
181: public boolean equals(Object obj) {
182: if (!(obj instanceof AlphaComposite)) {
183: return false;
184: }
185: AlphaComposite ac = (AlphaComposite) obj;
186: if (rule != ac.rule) {
187: return false;
188: }
189: if (extraAlpha != ac.extraAlpha) {
190: return false;
191: }
192: return true;
193: }
194: }
|