1 36 37 40 41 package java2d.demos.Paint; 42 43 import java.awt.*; 44 import java.awt.MultipleGradientPaint.CycleMethod ; 45 import java.awt.event.ActionEvent ; 46 import java.awt.event.ActionListener ; 47 import java.awt.geom.Point2D ; 48 import javax.swing.JComboBox ; 49 import java2d.AnimatingControlsSurface; 50 import java2d.CustomControls; 51 52 53 56 public class GradAnim extends AnimatingControlsSurface { 57 58 private static final int BASIC_GRADIENT = 0; 59 private static final int LINEAR_GRADIENT = 1; 60 private static final int RADIAL_GRADIENT = 2; 61 private static final int FOCUS_GRADIENT = 3; 62 63 private static final int MAX_HUE = 256 * 6; 64 private animval x1, y1, x2, y2; 65 private int hue = (int) (Math.random() * MAX_HUE); 66 private int gradientType; 67 68 69 public GradAnim() { 70 setBackground(Color.white); 71 setControls(new Component[] { new DemoControls(this) }); 72 x1 = new animval(0, 300, 2, 10); 73 y1 = new animval(0, 300, 2, 10); 74 x2 = new animval(0, 300, 2, 10); 75 y2 = new animval(0, 300, 2, 10); 76 gradientType = BASIC_GRADIENT; 77 } 78 79 80 public void reset(int w, int h) { 81 x1.newlimits(0, w); 82 y1.newlimits(0, h); 83 x2.newlimits(0, w); 84 y2.newlimits(0, h); 85 } 86 87 88 public void step(int w, int h) { 89 x1.anim(); y1.anim(); 90 x2.anim(); y2.anim(); 91 hue = (hue + (int) (Math.random() * 10)) % MAX_HUE; 92 } 93 94 95 public static Color getColor(int hue) { 96 int leg = (hue / 256) % 6; 97 int step = (hue % 256) * 2; 98 int falling = (step < 256) ? 255 : 511 - step; 99 int rising = (step < 256) ? step : 255; 100 int r, g, b; 101 r = g = b = 0; 102 switch (leg) { 103 case 0: 104 r = 255; 105 break; 106 case 1: 107 r = falling; 108 g = rising; 109 break; 110 case 2: 111 g = 255; 112 break; 113 case 3: 114 g = falling; 115 b = rising; 116 break; 117 case 4: 118 b = 255; 119 break; 120 case 5: 121 b = falling; 122 r = rising; 123 break; 124 } 125 return new Color(r, g, b); 126 } 127 128 129 public void render(int w, int h, Graphics2D g2) { 130 float fx1 = x1.getFlt(); 131 float fy1 = y1.getFlt(); 132 float fx2 = x2.getFlt(); 133 float fy2 = y2.getFlt(); 134 135 if ((fx1 == fx2) && (fy1 == fy2)) { 136 fx2++; 138 fy2++; 139 } 140 141 Color c1 = getColor(hue); 142 Color c2 = getColor(hue + 256 * 3); 143 Paint gp; 144 145 switch (gradientType) { 146 case BASIC_GRADIENT: 147 default: 148 gp = new GradientPaint(fx1, fy1, c1, 149 fx2, fy2, c2, 150 true); 151 break; 152 case LINEAR_GRADIENT: 153 { 154 float[] fractions = new float[] {0.0f, 0.2f, 1.0f}; 155 Color c3 = getColor(hue + 256 * 2); 156 Color[] colors = new Color[] {c1, c2, c3}; 157 gp = new LinearGradientPaint(fx1, fy1, 158 fx2, fy2, 159 fractions, colors, 160 CycleMethod.REFLECT); 161 } 162 break; 163 164 case RADIAL_GRADIENT: 165 { 166 float[] fractions = {0.0f, 0.2f, 0.8f, 1.0f}; 167 Color c3 = getColor(hue + 256 * 2); 168 Color c4 = getColor(hue + 256 * 4); 169 Color[] colors = new Color[] {c1, c2, c3, c4}; 170 float radius = (float)Point2D.distance(fx1, fy1, fx2, fy2); 171 gp = new RadialGradientPaint(fx1, fy1, radius, 172 fractions, colors, 173 CycleMethod.REFLECT); 174 } 175 break; 176 177 case FOCUS_GRADIENT: 178 { 179 float[] fractions = {0.0f, 0.2f, 0.8f, 1.0f}; 180 Color c3 = getColor(hue + 256 * 4); 181 Color c4 = getColor(hue + 256 * 2); 182 Color[] colors = new Color[] {c1, c2, c3, c4}; 183 float radius = (float)Point2D.distance(fx1, fy1, fx2, fy2); 184 float max = Math.max(w, h); 185 radius = max * (((radius / max) * 0.9f) + 0.1f); 192 gp = new RadialGradientPaint(fx2, fy2, radius, 193 fx1, fy1, 194 fractions, colors, 195 CycleMethod.REPEAT); 196 } 197 break; 198 } 199 g2.setPaint(gp); 200 g2.fillRect(0, 0, w, h); 201 g2.setColor(Color.yellow); 202 g2.drawLine(x1.getInt(), y1.getInt(), x2.getInt(), y2.getInt()); 203 } 204 205 206 public class animval { 207 float curval; 208 float lowval; 209 float highval; 210 float currate; 211 float lowrate; 212 float highrate; 213 214 public animval(int lowval, int highval, 215 int lowrate, int highrate) { 216 this.lowval = lowval; 217 this.highval = highval; 218 this.lowrate = lowrate; 219 this.highrate = highrate; 220 this.curval = randval(lowval, highval); 221 this.currate = randval(lowrate, highrate); 222 } 223 224 public float randval(float low, float high) { 225 return (float) (low + Math.random() * (high - low)); 226 } 227 228 public float getFlt() { 229 return curval; 230 } 231 232 public int getInt() { 233 return (int) curval; 234 } 235 236 public void anim() { 237 curval += currate; 238 clip(); 239 } 240 241 public void clip() { 242 if (curval > highval) { 243 curval = highval - (curval - highval); 244 if (curval < lowval) { 245 curval = highval; 246 } 247 currate = - randval(lowrate, highrate); 248 } else if (curval < lowval) { 249 curval = lowval + (lowval - curval); 250 if (curval > highval) { 251 curval = lowval; 252 } 253 currate = randval(lowrate, highrate); 254 } 255 } 256 257 public void newlimits(int lowval, int highval) { 258 this.lowval = lowval; 259 this.highval = highval; 260 clip(); 261 } 262 } 263 264 265 public static void main(String argv[]) { 266 createDemoFrame(new GradAnim()); 267 } 268 269 270 class DemoControls extends CustomControls implements ActionListener { 271 272 GradAnim demo; 273 JComboBox combo; 274 275 public DemoControls(GradAnim demo) { 276 super(demo.name); 277 this.demo = demo; 278 combo = new JComboBox (); 279 combo.addActionListener(this); 280 combo.addItem("2-color GradientPaint"); 281 combo.addItem("3-color LinearGradientPaint"); 282 combo.addItem("4-color RadialGradientPaint"); 283 combo.addItem("4-color RadialGradientPaint with focus"); 284 combo.setSelectedIndex(0); 285 add(combo); 286 } 287 288 289 public void actionPerformed(ActionEvent e) { 290 int index = combo.getSelectedIndex(); 291 if (index >= 0) { 292 demo.gradientType = index; 293 } 294 if (demo.animating.thread == null) { 295 demo.repaint(); 296 } 297 } 298 299 public Dimension getPreferredSize() { 300 return new Dimension(200,41); 301 } 302 303 304 public void run() { 305 Thread me = Thread.currentThread(); 306 while (thread == me) { 307 for (int i = 0; i < combo.getItemCount(); i++) { 308 combo.setSelectedIndex(i); 309 try { 310 thread.sleep(4444); 311 } catch (InterruptedException e) { return; } 312 } 313 } 314 thread = null; 315 } 316 } 317 } 318 | Popular Tags |