KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java2d > demos > Composite > ACrules


1 /*
2  * @(#)ACrules.java 1.21 06/08/09
3  *
4  * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * -Redistribution of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  *
12  * -Redistribution in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * Neither the name of Sun Microsystems, Inc. or the names of contributors may
17  * be used to endorse or promote products derived from this software without
18  * specific prior written permission.
19  *
20  * This software is provided "AS IS," without a warranty of any kind. ALL
21  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
22  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
23  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN")
24  * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
25  * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
26  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
27  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
28  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
29  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
30  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
31  *
32  * You acknowledge that this software is not designed, licensed or intended
33  * for use in the design, construction, operation or maintenance of any
34  * nuclear facility.
35  */

36
37 /*
38  * @(#)ACrules.java 1.21 06/08/09
39  */

40
41 package java2d.demos.Composite;
42
43 import java.awt.*;
44 import java.awt.geom.GeneralPath JavaDoc;
45 import java.awt.image.BufferedImage JavaDoc;
46 import java.awt.font.TextLayout JavaDoc;
47 import java.awt.font.FontRenderContext JavaDoc;
48 import java.awt.font.LineMetrics JavaDoc;
49 import java2d.AnimatingSurface;
50
51 import static java.awt.AlphaComposite JavaDoc.*;
52
53
54 /**
55  * All the AlphaCompositing Rules demonstrated.
56  */

57 public class ACrules extends AnimatingSurface {
58
59     private static String JavaDoc compNames[] = {
60         "Src",
61         "SrcOver",
62         "SrcIn",
63         "SrcOut",
64         "SrcAtop",
65         "Clear",
66
67         "Dst",
68         "DstOver",
69         "DstIn",
70         "DstOut",
71         "DstAtop",
72     "Xor",
73     };
74
75     private static AlphaComposite JavaDoc compObjs[] = {
76         Src, SrcOver, SrcIn, SrcOut, SrcAtop, Clear,
77         Dst, DstOver, DstIn, DstOut, DstAtop, Xor,
78     };
79
80     private static int NUM_RULES = compObjs.length;
81     private static int HALF_NUM_RULES = NUM_RULES / 2;
82
83     private int fadeIndex;
84     private static float fadeValues[][] = {
85         { 1.0f,-0.1f, 0.0f, 1.0f, 0.0f, 1.0f},
86         { 0.0f, 0.1f, 1.0f, 1.0f,-0.1f, 0.0f},
87         { 1.0f, 0.0f, 1.0f, 0.0f, 0.1f, 1.0f},
88     };
89     private static String JavaDoc fadeNames[] = {
90         "Src => transparent, Dest opaque",
91         "Src => opaque, Dest => transparent",
92         "Src opaque, Dest => opaque",
93     };
94     private static Font f = new Font("serif", Font.PLAIN, 10);
95     private float srca = fadeValues[fadeIndex][0];
96     private float dsta = fadeValues[fadeIndex][3];
97     private String JavaDoc fadeLabel = fadeNames[0];
98     private BufferedImage JavaDoc statBI, animBI;
99     private int PADLEFT, PADRIGHT, HPAD;
100     private int PADABOVE, PADBELOW, VPAD;
101     private int RECTWIDTH, RECTHEIGHT;
102     private int PADDEDWIDTH, PADDEDHEIGHT;
103     private GeneralPath JavaDoc srcpath = new GeneralPath JavaDoc();
104     private GeneralPath JavaDoc dstpath = new GeneralPath JavaDoc();
105     private LineMetrics JavaDoc lm;
106     private BufferedImage JavaDoc dBI, sBI;
107     private GradientPaint gradientDst, gradientSrc;
108
109
110     public ACrules() {
111         setBackground(Color.white);
112     }
113
114
115     public void reset(int w, int h) {
116         setSleepAmount(400);
117         FontRenderContext JavaDoc frc = new FontRenderContext JavaDoc(null, false, false);
118         lm = f.getLineMetrics(compNames[0], frc);
119         
120         PADLEFT = (w < 150) ? 10 : 15;
121         PADRIGHT = (w < 150) ? 10 : 15;
122         HPAD = (PADLEFT + PADRIGHT);
123         PADBELOW = (h < 250) ? 1 : 2;
124         PADABOVE = PADBELOW + (int) lm.getHeight();
125         VPAD = (PADABOVE + PADBELOW);
126         RECTWIDTH = w/4 - HPAD;
127         RECTWIDTH = (RECTWIDTH < 6) ? 6 : RECTWIDTH;
128         RECTHEIGHT = (h-VPAD)/HALF_NUM_RULES - VPAD;
129         RECTHEIGHT = (RECTHEIGHT < 6) ? 6 : RECTHEIGHT;
130         PADDEDWIDTH = (RECTWIDTH + HPAD);
131         PADDEDHEIGHT = (RECTHEIGHT + VPAD);
132         
133         srcpath.reset();
134         srcpath.moveTo(0,0);
135         srcpath.lineTo(RECTWIDTH,0);
136         srcpath.lineTo(0,RECTHEIGHT);
137         srcpath.closePath();
138         
139         dstpath.reset();
140         dstpath.moveTo(0,0);
141         dstpath.lineTo(RECTWIDTH,RECTHEIGHT);
142         dstpath.lineTo(RECTWIDTH,0);
143         dstpath.closePath();
144         
145         dBI = new BufferedImage JavaDoc(RECTWIDTH, RECTHEIGHT,
146                                     BufferedImage.TYPE_INT_ARGB);
147         sBI = new BufferedImage JavaDoc(RECTWIDTH, RECTHEIGHT,
148                                     BufferedImage.TYPE_INT_ARGB);
149         gradientDst = new GradientPaint(0, 0,
150                                       new Color(1.0f, 0.0f, 0.0f, 1.0f),
151                                       0, RECTHEIGHT,
152                                       new Color(1.0f, 0.0f, 0.0f, 0.0f));
153         gradientSrc = new GradientPaint(0, 0,
154                                       new Color(0.0f, 0.0f, 1.0f, 1.0f),
155                                       RECTWIDTH, 0,
156                                       new Color(0.0f, 0.0f, 1.0f, 0.0f));
157         statBI = new BufferedImage JavaDoc(w/2, h, BufferedImage.TYPE_INT_RGB);
158         statBI = drawCompBI(statBI, true);
159         animBI = new BufferedImage JavaDoc(w/2, h, BufferedImage.TYPE_INT_RGB);
160     }
161
162
163     public void step(int w, int h) {
164         if (getSleepAmount() == 5000) {
165             setSleepAmount(200);
166         }
167
168         srca = srca + fadeValues[fadeIndex][1];
169         dsta = dsta + fadeValues[fadeIndex][4];
170         fadeLabel = fadeNames[fadeIndex];
171         if (srca < 0 || srca > 1.0 || dsta < 0 || dsta > 1.0) {
172             setSleepAmount(5000);
173             srca = fadeValues[fadeIndex][2];
174             dsta = fadeValues[fadeIndex][5];
175             if (fadeIndex++ == fadeValues.length-1) {
176                 fadeIndex = 0;
177             }
178         }
179     }
180
181
182     public void render(int w, int h, Graphics2D g2) {
183
184         if (statBI == null || animBI == null) {
185             return;
186         }
187         g2.drawImage(statBI, 0, 0, null);
188         g2.drawImage(drawCompBI(animBI, false), w/2, 0, null);
189
190         g2.setColor(Color.black);
191         FontRenderContext JavaDoc frc = g2.getFontRenderContext();
192         TextLayout JavaDoc tl = new TextLayout JavaDoc("AC Rules", g2.getFont(), frc);
193         tl.draw(g2, 15.0f, (float) tl.getBounds().getHeight()+3.0f);
194
195         tl = new TextLayout JavaDoc(fadeLabel, f, frc);
196         float x = (float) (w*0.75-tl.getBounds().getWidth()/2);
197         if ((x + tl.getBounds().getWidth()) > w) {
198             x = (float) (w - tl.getBounds().getWidth());
199         }
200         tl.draw(g2, x, (float) tl.getBounds().getHeight()+3.0f);
201     }
202
203
204     private BufferedImage JavaDoc drawCompBI(BufferedImage JavaDoc bi, boolean doGradient)
205     {
206         Graphics2D big = bi.createGraphics();
207         big.setColor(getBackground());
208         big.fillRect(0, 0, bi.getWidth(), bi.getHeight());
209         big.setRenderingHint(RenderingHints.KEY_ANTIALIASING, AntiAlias);
210         big.setFont(f);
211
212         Graphics2D gD = dBI.createGraphics();
213         gD.setRenderingHint(RenderingHints.KEY_ANTIALIASING, AntiAlias);
214         Graphics2D gS = sBI.createGraphics();
215         gS.setRenderingHint(RenderingHints.KEY_ANTIALIASING, AntiAlias);
216
217         int x = 0, y = 0;
218         int yy = (int) lm.getHeight() + VPAD;
219
220         for (int i = 0; i < compNames.length; i++) {
221             y = (i == 0 || i == HALF_NUM_RULES) ? yy : y + PADDEDHEIGHT;
222             x = (i >= HALF_NUM_RULES) ? bi.getWidth()/2+PADLEFT : PADLEFT;
223             big.translate(x, y);
224
225             gD.setComposite(Clear);
226             gD.fillRect(0, 0, RECTWIDTH, RECTHEIGHT);
227             gD.setComposite(Src);
228             if (doGradient) {
229                 gD.setPaint(gradientDst);
230                 gD.fillRect(0, 0, RECTWIDTH, RECTHEIGHT);
231             } else {
232                 gD.setPaint(new Color(1.0f, 0.0f, 0.0f, dsta));
233                 gD.fill(dstpath);
234             }
235
236             gS.setComposite(Clear);
237             gS.fillRect(0, 0, RECTWIDTH, RECTHEIGHT);
238             gS.setComposite(Src);
239             if (doGradient) {
240                 gS.setPaint(gradientSrc);
241                 gS.fillRect(0, 0, RECTWIDTH, RECTHEIGHT);
242             } else {
243                 gS.setPaint(new Color(0.0f, 0.0f, 1.0f, srca));
244                 gS.fill(srcpath);
245             }
246
247             gD.setComposite(compObjs[i]);
248             gD.drawImage(sBI, 0, 0, null);
249
250             big.drawImage(dBI, 0, 0, null);
251             big.setColor(Color.black);
252             big.drawString(compNames[i], 0, -lm.getDescent());
253             big.drawRect(0, 0, RECTWIDTH, RECTHEIGHT);
254             big.translate(-x, -y);
255         }
256
257         gD.dispose();
258         gS.dispose();
259         big.dispose();
260
261         return bi;
262     }
263
264
265     public static void main(String JavaDoc argv[]) {
266         createDemoFrame(new ACrules());
267     }
268 }
269
Popular Tags