KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)ACimages.java 1.28 06/08/29
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  * @(#)ACimages.java 1.28 06/08/29
39  */

40
41 package java2d.demos.Composite;
42
43 import java.awt.*;
44 import java.awt.geom.Ellipse2D JavaDoc;
45 import java.awt.geom.Rectangle2D JavaDoc;
46 import java.awt.geom.RoundRectangle2D JavaDoc;
47 import java.awt.font.TextLayout JavaDoc;
48 import java.awt.font.FontRenderContext JavaDoc;
49 import java2d.Surface;
50
51 import static java.awt.Color JavaDoc.*;
52
53 /**
54  * Compositing shapes on images.
55  */

56 public class ACimages extends Surface {
57
58     private static String JavaDoc s[] = { "box", "fight", "magnify",
59                         "boxwave", "globe", "snooze",
60                         "tip", "thumbsup", "dukeplug"};
61     private static Image imgs[] = new Image[s.length];
62     private static Color JavaDoc colors[] = { BLUE, CYAN, GREEN,
63                         MAGENTA, ORANGE, PINK, RED, YELLOW, LIGHT_GRAY };
64
65
66     public ACimages() {
67         setBackground(WHITE);
68         for (int i = 0; i < imgs.length; i++) {
69             imgs[i] = getImage(s[i] + ".gif");
70         }
71     }
72
73
74     public void render(int w, int h, Graphics2D g2) {
75
76         float alpha = 0.0f;
77         int iw = w/3;
78         int ih = (h-45)/3;
79         float xx = 0, yy = 15;
80
81         for (int i =0; i < imgs.length; i++) {
82
83             xx = (i%3 == 0) ? 0 : xx+w/3;
84             switch (i) {
85                 case 3 : yy = h/3+15; break;
86                 case 6 : yy = h/3*2+15;
87             }
88
89             g2.setComposite(AlphaComposite.SrcOver);
90             g2.setColor(BLACK);
91             AlphaComposite ac = AlphaComposite.SrcOver.derive(alpha += .1f);
92             String JavaDoc s = "a=" + Float.toString(alpha).substring(0,3);
93             new TextLayout JavaDoc(s,g2.getFont(), g2.getFontRenderContext()).draw(g2, xx+3, yy-2);
94
95             Shape shape=null;
96
97             switch (i%3) {
98                 case 0 : shape = new Ellipse2D.Float JavaDoc(xx, yy, iw, ih);
99                         break;
100                 case 1 : shape = new RoundRectangle2D.Float JavaDoc(xx, yy, iw, ih, 25, 25);
101                         break;
102                 case 2 : shape = new Rectangle2D.Float JavaDoc(xx, yy, iw, ih);
103                         break;
104             }
105             g2.setColor(colors[i]);
106             g2.setComposite(ac);
107             g2.fill(shape);
108             g2.drawImage(imgs[i], (int) xx, (int) yy, iw, ih, null);
109         }
110     }
111
112
113     public static void main(String JavaDoc s[]) {
114         createDemoFrame(new ACimages());
115     }
116 }
117
Popular Tags