home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / JBuilder8.iso / Solaris / resource / jre / demo / jfc / Java2D / src / java2d / demos / Paint / GradAnim.java next >
Encoding:
Java Source  |  2002-09-06  |  5.5 KB  |  190 lines

  1. /*
  2.  * Copyright (c) 2002 Sun Microsystems, Inc. All  Rights Reserved.
  3.  * 
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  * 
  8.  * -Redistributions of source code must retain the above copyright
  9.  *  notice, this list of conditions and the following disclaimer.
  10.  * 
  11.  * -Redistribution in binary form must reproduct the above copyright
  12.  *  notice, this list of conditions and the following disclaimer in
  13.  *  the documentation and/or other materials provided with the distribution.
  14.  * 
  15.  * Neither the name of Sun Microsystems, Inc. or the names of contributors
  16.  * may be used to endorse or promote products derived from this software
  17.  * without specific prior written permission.
  18.  * 
  19.  * This software is provided "AS IS," without a warranty of any kind. ALL
  20.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
  21.  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
  22.  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT
  23.  * BE LIABLE FOR ANY DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT
  24.  * OF OR RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THE SOFTWARE OR ITS
  25.  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
  26.  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
  27.  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
  28.  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN
  29.  * IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  30.  * 
  31.  * You acknowledge that Software is not designed, licensed or intended for
  32.  * use in the design, construction, operation or maintenance of any nuclear
  33.  * facility.
  34.  */
  35.  
  36. /*
  37.  * @(#)GradAnim.java    1.15 02/06/13
  38.  */
  39.  
  40. package java2d.demos.Paint;
  41.  
  42. import java.awt.*;
  43. import java2d.AnimatingSurface;
  44.  
  45.  
  46. /**
  47.  * GradientPaint animation.
  48.  */
  49. public class GradAnim extends AnimatingSurface {
  50.  
  51.     private static final int MAX_HUE = 256 * 6;
  52.     private animval x1, y1, x2, y2;
  53.     private int hue = (int) (Math.random() * MAX_HUE);
  54.  
  55.  
  56.     public GradAnim() {
  57.         setBackground(Color.white);
  58.         x1 = new animval(0, 300, 2, 10);
  59.         y1 = new animval(0, 300, 2, 10);
  60.         x2 = new animval(0, 300, 2, 10);
  61.         y2 = new animval(0, 300, 2, 10);
  62.     }
  63.  
  64.  
  65.     public void reset(int w, int h) {
  66.         x1.newlimits(0, w);
  67.         y1.newlimits(0, h);
  68.         x2.newlimits(0, w);
  69.         y2.newlimits(0, h);
  70.     }
  71.  
  72.  
  73.     public void step(int w, int h) {
  74.         x1.anim(); y1.anim();
  75.         x2.anim(); y2.anim();
  76.         hue = (hue + (int) (Math.random() * 10)) % MAX_HUE;
  77.     }
  78.  
  79.  
  80.     public static Color getColor(int hue) {
  81.         int leg = (hue / 256) % 6;
  82.         int step = (hue % 256) * 2;
  83.         int falling = (step < 256) ? 255 : 511 - step;
  84.         int rising = (step < 256) ? step : 255;
  85.         int r, g, b;
  86.         r = g = b = 0;
  87.         switch (leg) {
  88.         case 0:
  89.             r = 255;
  90.             break;
  91.         case 1:
  92.             r = falling;
  93.             g = rising;
  94.             break;
  95.         case 2:
  96.             g = 255;
  97.             break;
  98.         case 3:
  99.             g = falling;
  100.             b = rising;
  101.             break;
  102.         case 4:
  103.             b = 255;
  104.             break;
  105.         case 5:
  106.             b = falling;
  107.             r = rising;
  108.             break;
  109.         }
  110.         return new Color(r, g, b);
  111.     }
  112.  
  113.  
  114.     public void render(int w, int h, Graphics2D g2) {
  115.         Color c1 = getColor(hue);
  116.         Color c2 = getColor(hue + 256 * 3);
  117.         GradientPaint gp = new GradientPaint(x1.getFlt(), y1.getFlt(), c1,
  118.                                          x2.getFlt(), y2.getFlt(), c2,
  119.                                          true);
  120.         g2.setPaint(gp);
  121.         g2.fillRect(0, 0, w, h);
  122.         g2.setColor(Color.yellow);
  123.         g2.drawLine(x1.getInt(), y1.getInt(), x2.getInt(), y2.getInt());
  124.     }
  125.  
  126.  
  127.     public class animval {
  128.         float curval;
  129.         float lowval;
  130.         float highval;
  131.         float currate;
  132.         float lowrate;
  133.         float highrate;
  134.  
  135.         public animval(int lowval, int highval,
  136.                        int lowrate, int highrate) {
  137.             this.lowval = lowval;
  138.             this.highval = highval;
  139.             this.lowrate = lowrate;
  140.             this.highrate = highrate;
  141.             this.curval = randval(lowval, highval);
  142.             this.currate = randval(lowrate, highrate);
  143.         }
  144.  
  145.         public float randval(float low, float high) {
  146.             return (float) (low + Math.random() * (high - low));
  147.         }
  148.  
  149.         public float getFlt() {
  150.             return curval;
  151.         }
  152.  
  153.         public int getInt() {
  154.             return (int) curval;
  155.         }
  156.  
  157.         public void anim() {
  158.             curval += currate;
  159.             clip();
  160.         }
  161.  
  162.         public void clip() {
  163.             if (curval > highval) {
  164.                 curval = highval - (curval - highval);
  165.                 if (curval < lowval) {
  166.                     curval = highval;
  167.                 }
  168.                 currate = - randval(lowrate, highrate);
  169.             } else if (curval < lowval) {
  170.                 curval = lowval + (lowval - curval);
  171.                 if (curval > highval) {
  172.                     curval = lowval;
  173.                 }
  174.                 currate = randval(lowrate, highrate);
  175.             }
  176.         }
  177.  
  178.         public void newlimits(int lowval, int highval) {
  179.             this.lowval = lowval;
  180.             this.highval = highval;
  181.             clip();
  182.         }
  183.     }
  184.  
  185.  
  186.     public static void main(String argv[]) {
  187.         createDemoFrame(new GradAnim());
  188.     }
  189. }
  190.