Imported GNU Classpath 0.90
[official-gcc.git] / libjava / classpath / java / awt / AlphaComposite.java
blob92b9e09a60dd6450f56b9b92bbeff6d86e187236
1 /* AlphaComposite.java -- provides a context for performing alpha compositing
2 Copyright (C) 2002, 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package java.awt;
41 import gnu.java.awt.java2d.AlphaCompositeContext;
43 import java.awt.image.ColorModel;
44 import java.util.LinkedHashMap;
45 import java.util.Map;
47 /**
49 * @author Eric Blake (ebb9@email.byu.edu)
50 * @see Composite
51 * @see CompositeContext
52 * @since 1.3
53 * @status updated to 1.4 except for createContext, needs documentation
55 public final class AlphaComposite implements Composite
57 /** Map Long to AlphaComposites. See getInstance for details. */
58 private static final LinkedHashMap cache = new LinkedHashMap(11, 0.75f, true)
60 /** The largest the alpha composite cache can grow. */
61 private static final int MAX_CACHE_SIZE = 2048;
63 /** Prune stale entries. */
64 protected boolean removeEldestEntry(Map.Entry eldest)
65 { // XXX - FIXME Use Map.Entry, not just Entry as gcj 3.1 workaround.
66 return size() > MAX_CACHE_SIZE;
70 public static final int CLEAR = 1;
71 public static final int SRC = 2;
72 public static final int DST = 9;
73 public static final int SRC_OVER = 3;
74 public static final int DST_OVER = 4;
75 public static final int SRC_IN = 5;
76 public static final int DST_IN = 6;
77 public static final int SRC_OUT = 7;
78 public static final int DST_OUT = 8;
79 public static final int SRC_ATOP = 10;
80 public static final int DST_ATOP = 11;
81 public static final int XOR = 12;
82 public static final AlphaComposite Clear = getInstance(CLEAR);
83 public static final AlphaComposite Src = getInstance(SRC);
84 public static final AlphaComposite Dst = getInstance(DST);
85 public static final AlphaComposite SrcOver = getInstance(SRC_OVER);
86 public static final AlphaComposite DstOver = getInstance(DST_OVER);
87 public static final AlphaComposite SrcIn = getInstance(SRC_IN);
88 public static final AlphaComposite DstIn = getInstance(DST_IN);
89 public static final AlphaComposite SrcOut = getInstance(SRC_OUT);
90 public static final AlphaComposite DstOut = getInstance(DST_OUT);
91 public static final AlphaComposite SrcAtop = getInstance(SRC_ATOP);
92 public static final AlphaComposite DstAtop = getInstance(DST_ATOP);
93 public static final AlphaComposite Xor = getInstance(XOR);
95 private final int rule;
96 private final float alpha;
97 private AlphaComposite(int rule, float alpha)
99 this.rule = rule;
100 this.alpha = alpha;
104 * Creates an AlphaComposite object with the specified rule.
106 * @param rule The compositing rule.
108 * @exception IllegalArgumentException If rule is not one of the following:
109 * CLEAR, SRC, DST, SRC_OVER, DST_OVER, SRC_IN, DST_IN, SRC_OUT, DST_OUT,
110 * SRC_ATOP, DST_ATOP, or XOR.
112 public static AlphaComposite getInstance(int rule)
114 return getInstance(rule, 1);
118 * Creates an AlphaComposite object with the specified rule and the constant
119 * alpha to multiply with the alpha of the source. The source is multiplied
120 * with the specified alpha before being composited with the destination.
122 * @param rule The compositing rule.
124 * @exception IllegalArgumentException If rule is not one of the following:
125 * CLEAR, SRC, DST, SRC_OVER, DST_OVER, SRC_IN, DST_IN, SRC_OUT, DST_OUT,
126 * SRC_ATOP, DST_ATOP, or XOR.
128 public static AlphaComposite getInstance(int rule, float alpha)
130 if (rule < CLEAR || rule > XOR || ! (alpha >= 0 && alpha <= 1))
131 throw new IllegalArgumentException();
132 // This long is guaranteed unique for all valid alpha composites.
133 Long l = new Long(rule + Double.doubleToLongBits(alpha));
134 AlphaComposite a = (AlphaComposite) cache.get(l);
135 if (a == null)
137 a = new AlphaComposite(rule, alpha);
138 cache.put(l, a);
140 return a;
144 * Creates a {@link CompositeContext} that can be used to perform
145 * compositing operations according to this AlphaComposite settings.
147 * @param srcColorModel the color model of the source raster
148 * @param dstColorModel the color model of the destination raster
149 * @param hints the rendering hints to use
151 * @return a {@link CompositeContext} that can be used to perform
152 * compositing operations according to this AlphaComposite settings
154 public CompositeContext createContext(ColorModel srcColorModel,
155 ColorModel dstColorModel,
156 RenderingHints hints)
158 return new AlphaCompositeContext(this, srcColorModel, dstColorModel);
161 public float getAlpha()
163 return alpha;
165 public int getRule()
167 return rule;
169 public int hashCode()
171 return 31 * Float.floatToIntBits(alpha) + rule;
173 public boolean equals(Object o)
175 if (! (o instanceof AlphaComposite))
176 return false;
177 AlphaComposite a = (AlphaComposite) o;
178 return rule == a.rule && alpha == a.alpha;
180 } // class AlphaComposite