KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java2d > demos > Paint > Gradient


1 /*
2  * @(#)Gradient.java 1.17 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  * @(#)Gradient.java 1.17 06/08/09
39  */

40
41 package java2d.demos.Paint;
42
43
44 import java.awt.*;
45 import java.awt.font.*;
46 import java.awt.event.*;
47 import java.awt.geom.Rectangle2D JavaDoc;
48 import javax.swing.*;
49 import java2d.ControlsSurface;
50 import java2d.CustomControls;
51
52 import static java.awt.Color JavaDoc.*;
53
54
55
56 public class Gradient extends ControlsSurface {
57
58     protected Color JavaDoc innerC, outerC;
59     private DemoControls controls;
60
61
62     public Gradient() {
63         setBackground(white);
64         innerC = green;
65         outerC = blue;
66         setControls(new Component[] { new DemoControls(this) });
67     }
68
69
70     public void render(int w, int h, Graphics2D g2) {
71
72         int w2 = w/2;
73         int h2 = h/2;
74         g2.setPaint(new GradientPaint(0,0,outerC,w*.35f,h*.35f,innerC));
75         g2.fillRect(0, 0, w2, h2);
76         g2.setPaint(new GradientPaint(w,0,outerC,w*.65f,h*.35f,innerC));
77         g2.fillRect(w2, 0, w2, h2);
78         g2.setPaint(new GradientPaint(0,h,outerC,w*.35f,h*.65f,innerC));
79         g2.fillRect(0, h2, w2, h2);
80         g2.setPaint(new GradientPaint(w,h,outerC,w*.65f,h*.65f,innerC));
81         g2.fillRect(w2, h2, w2, h2);
82
83         g2.setColor(black);
84         TextLayout tl = new TextLayout(
85                 "GradientPaint", g2.getFont(), g2.getFontRenderContext());
86         tl.draw(g2, (int) (w/2-tl.getBounds().getWidth()/2),
87                 (int) (h/2+tl.getBounds().getHeight()/2));
88     }
89
90
91     public static void main(String JavaDoc s[]) {
92         createDemoFrame(new Gradient());
93     }
94
95
96     static class DemoControls extends CustomControls implements ActionListener {
97
98         Gradient demo;
99         Color JavaDoc colors[] =
100                 { red, orange, yellow, green, blue, lightGray, cyan, magenta };
101         String JavaDoc colorName[] =
102                 { "Red", "Orange", "Yellow", "Green",
103                   "Blue", "lightGray", "Cyan", "Magenta" };
104         
105         JMenuItem innerMI[] = new JMenuItem[colors.length];
106         JMenuItem outerMI[] = new JMenuItem[colors.length];
107         ColoredSquare squares[] = new ColoredSquare[colors.length];
108         JMenu imenu, omenu;
109
110         public DemoControls(Gradient demo) {
111             super(demo.name);
112             this.demo = demo;
113             JMenuBar inMenuBar = new JMenuBar();
114             add(inMenuBar);
115             JMenuBar outMenuBar = new JMenuBar();
116             add(outMenuBar);
117             Font font = new Font("serif", Font.PLAIN, 10);
118
119             imenu = (JMenu) inMenuBar.add(new JMenu("Inner Color"));
120             imenu.setFont(font);
121             imenu.setIcon(new ColoredSquare(demo.innerC));
122             omenu = (JMenu) outMenuBar.add(new JMenu("Outer Color"));
123             omenu.setFont(font);
124             omenu.setIcon(new ColoredSquare(demo.outerC));
125             for (int i = 0; i < colors.length; i++) {
126                 squares[i] = new ColoredSquare(colors[i]);
127                 innerMI[i] = imenu.add(new JMenuItem(colorName[i]));
128                 innerMI[i].setFont(font);
129                 innerMI[i].setIcon(squares[i]);
130                 innerMI[i].addActionListener(this);
131                 outerMI[i] = omenu.add(new JMenuItem(colorName[i]));
132                 outerMI[i].setFont(font);
133                 outerMI[i].setIcon(squares[i]);
134                 outerMI[i].addActionListener(this);
135             }
136         }
137
138
139         public void actionPerformed(ActionEvent e) {
140             for (int i = 0; i < colors.length; i++) {
141                 if (e.getSource().equals(innerMI[i])) {
142                     demo.innerC = colors[i];
143                     imenu.setIcon(squares[i]);
144                     break;
145                 } else if (e.getSource().equals(outerMI[i])) {
146                     demo.outerC = colors[i];
147                     omenu.setIcon(squares[i]);
148                     break;
149                 }
150             }
151             demo.repaint();
152         }
153
154
155         public Dimension getPreferredSize() {
156             return new Dimension(200,37);
157         }
158
159
160         public void run() {
161             // goto double buffering
162
if (demo.getImageType() <= 1) {
163                 demo.setImageType(2);
164             }
165             Thread JavaDoc me = Thread.currentThread();
166             while (thread == me) {
167                 for (int i = 0; i < innerMI.length; i++) {
168                     if (i != 4) {
169                         try {
170                             thread.sleep(4444);
171                         } catch (InterruptedException JavaDoc e) { return; }
172                         innerMI[i].doClick();
173                     }
174                 }
175             }
176             thread = null;
177         }
178
179
180         class ColoredSquare implements Icon {
181             Color JavaDoc color;
182             public ColoredSquare(Color JavaDoc c) {
183                 this.color = c;
184             }
185     
186             public void paintIcon(Component c, Graphics g, int x, int y) {
187                 Color JavaDoc oldColor = g.getColor();
188                 g.setColor(color);
189                 g.fill3DRect(x,y,getIconWidth(), getIconHeight(), true);
190                 g.setColor(oldColor);
191             }
192             public int getIconWidth() { return 12; }
193             public int getIconHeight() { return 12; }
194         } // End ColoredSquare class
195
} // End DemoControls
196
} // End Gradient class
197
Popular Tags