Merge from the pain train
[official-gcc.git] / libjava / java / awt / AlphaComposite.java
blob3c7744ba51ec8f12aa7f1870ccd860c1cd2c8b6b
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., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 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 java.awt.image.ColorModel;
42 import java.util.LinkedHashMap;
43 import java.util.Map;
45 /**
47 * @author Eric Blake (ebb9@email.byu.edu)
48 * @see Composite
49 * @see CompositeContext
50 * @since 1.3
51 * @status updated to 1.4 except for createContext, needs documentation
53 public final class AlphaComposite implements Composite
55 /** Map Long to AlphaComposites. See getInstance for details. */
56 private static final LinkedHashMap cache = new LinkedHashMap(11, 0.75f, true)
58 /** The largest the alpha composite cache can grow. */
59 private static final int MAX_CACHE_SIZE = 2048;
61 /** Prune stale entries. */
62 protected boolean removeEldestEntry(Map.Entry eldest)
63 { // XXX - FIXME Use Map.Entry, not just Entry as gcj 3.1 workaround.
64 return size() > MAX_CACHE_SIZE;
68 public static final int CLEAR = 1;
69 public static final int SRC = 2;
70 public static final int DST = 9;
71 public static final int SRC_OVER = 3;
72 public static final int DST_OVER = 4;
73 public static final int SRC_IN = 5;
74 public static final int DST_IN = 6;
75 public static final int SRC_OUT = 7;
76 public static final int DST_OUT = 8;
77 public static final int SRC_ATOP = 10;
78 public static final int DST_ATOP = 11;
79 public static final int XOR = 12;
80 public static final AlphaComposite Clear = getInstance(CLEAR);
81 public static final AlphaComposite Src = getInstance(SRC);
82 public static final AlphaComposite Dst = getInstance(DST);
83 public static final AlphaComposite SrcOver = getInstance(SRC_OVER);
84 public static final AlphaComposite DstOver = getInstance(DST_OVER);
85 public static final AlphaComposite SrcIn = getInstance(SRC_IN);
86 public static final AlphaComposite DstIn = getInstance(DST_IN);
87 public static final AlphaComposite SrcOut = getInstance(SRC_OUT);
88 public static final AlphaComposite DstOut = getInstance(DST_OUT);
89 public static final AlphaComposite SrcAtop = getInstance(SRC_ATOP);
90 public static final AlphaComposite DstAtop = getInstance(DST_ATOP);
91 public static final AlphaComposite Xor = getInstance(XOR);
93 private final int rule;
94 private final float alpha;
95 private AlphaComposite(int rule, float alpha)
97 this.rule = rule;
98 this.alpha = alpha;
102 * Creates an AlphaComposite object with the specified rule.
104 * @param rule The compositing rule.
106 * @exception IllegalArgumentException If rule is not one of the following:
107 * CLEAR, SRC, DST, SRC_OVER, DST_OVER, SRC_IN, DST_IN, SRC_OUT, DST_OUT,
108 * SRC_ATOP, DST_ATOP, or XOR.
110 public static AlphaComposite getInstance(int rule)
112 return getInstance(rule, 1);
116 * Creates an AlphaComposite object with the specified rule and the constant
117 * alpha to multiply with the alpha of the source. The source is multiplied
118 * with the specified alpha before being composited with the destination.
120 * @param rule The compositing rule.
122 * @exception IllegalArgumentException If rule is not one of the following:
123 * CLEAR, SRC, DST, SRC_OVER, DST_OVER, SRC_IN, DST_IN, SRC_OUT, DST_OUT,
124 * SRC_ATOP, DST_ATOP, or XOR.
126 public static AlphaComposite getInstance(int rule, float alpha)
128 if (rule < CLEAR || rule > XOR || ! (alpha >= 0 && alpha <= 1))
129 throw new IllegalArgumentException();
130 // This long is guaranteed unique for all valid alpha composites.
131 Long l = new Long(rule + Double.doubleToLongBits(alpha));
132 AlphaComposite a = (AlphaComposite) cache.get(l);
133 if (a == null)
135 a = new AlphaComposite(rule, alpha);
136 cache.put(l, a);
138 return a;
140 public CompositeContext createContext(ColorModel srcColorModel,
141 ColorModel dstColorModel,
142 RenderingHints hints)
144 // XXX Implement. Sun uses undocumented implementation class
145 // sun.java2d.SunCompositeContext.
146 throw new Error("not implemented");
148 public float getAlpha()
150 return alpha;
152 public int getRule()
154 return rule;
156 public int hashCode()
158 return 31 * Float.floatToIntBits(alpha) + rule;
160 public boolean equals(Object o)
162 if (! (o instanceof AlphaComposite))
163 return false;
164 AlphaComposite a = (AlphaComposite) o;
165 return rule == a.rule && alpha == a.alpha;
167 } // class AlphaComposite