GradAnim.java
Upload User: cdlibang
Upload Date: 2016-07-17
Package Size: 774k
Code Size: 6k
Category:

2D Graphic

Development Platform:

Java

  1. /*
  2.  * @(#)GradAnim.java 1.10 99/04/23
  3.  *
  4.  * Copyright (c) 1998, 1999 by Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
  7.  * modify and redistribute this software in source and binary code form,
  8.  * provided that i) this copyright notice and license appear on all copies of
  9.  * the software; and ii) Licensee does not utilize the software in a manner
  10.  * which is disparaging to Sun.
  11.  * 
  12.  * This software is provided "AS IS," without a warranty of any kind. ALL
  13.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
  14.  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
  15.  * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
  16.  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
  17.  * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
  18.  * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
  19.  * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
  20.  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
  21.  * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
  22.  * POSSIBILITY OF SUCH DAMAGES.
  23.  * 
  24.  * This software is not designed or intended for use in on-line control of
  25.  * aircraft, air traffic, aircraft navigation or aircraft communications; or in
  26.  * the design, construction, operation or maintenance of any nuclear
  27.  * facility. Licensee represents and warrants that it will not use or
  28.  * redistribute the Software for such purposes.
  29.  */
  30. package demos.Paint;
  31. import java.awt.*;
  32. import java.awt.event.*;
  33. import AnimatingContext;
  34. import DemoSurface;
  35. import DemoPanel;
  36. /**
  37.  * GradientPaint animation.
  38.  */
  39. public class GradAnim extends DemoSurface implements AnimatingContext {
  40.     private static final int MAX_HUE = 256 * 6;
  41.     private animval x1, y1, x2, y2;
  42.     private int hue = (int) (Math.random() * MAX_HUE);
  43.     public GradAnim() {
  44.         setBackground(Color.white);
  45.         x1 = new animval(0, 300, 2, 10);
  46.         y1 = new animval(0, 300, 2, 10);
  47.         x2 = new animval(0, 300, 2, 10);
  48.         y2 = new animval(0, 300, 2, 10);
  49.     }
  50.     public void reset(int w, int h) {
  51.         x1.newlimits(0, w);
  52.         y1.newlimits(0, h);
  53.         x2.newlimits(0, w);
  54.         y2.newlimits(0, h);
  55.     }
  56.     public void step(int w, int h) {
  57.         x1.anim(); y1.anim();
  58.         x2.anim(); y2.anim();
  59.         hue = (hue + (int) (Math.random() * 10)) % MAX_HUE;
  60.     }
  61.     public static Color getColor(int hue) {
  62.         int leg = (hue / 256) % 6;
  63.         int step = (hue % 256) * 2;
  64.         int falling = (step < 256) ? 255 : 511 - step;
  65.         int rising = (step < 256) ? step : 255;
  66.         int r, g, b;
  67.         r = g = b = 0;
  68.         switch (leg) {
  69.         case 0:
  70.             r = 255;
  71.             break;
  72.         case 1:
  73.             r = falling;
  74.             g = rising;
  75.             break;
  76.         case 2:
  77.             g = 255;
  78.             break;
  79.         case 3:
  80.             g = falling;
  81.             b = rising;
  82.             break;
  83.         case 4:
  84.             b = 255;
  85.             break;
  86.         case 5:
  87.             b = falling;
  88.             r = rising;
  89.             break;
  90.         }
  91.         return new Color(r, g, b);
  92.     }
  93.     public void drawDemo(int w, int h, Graphics2D g2) {
  94.         Color c1 = getColor(hue);
  95.         Color c2 = getColor(hue + 256 * 3);
  96.         GradientPaint gp = new GradientPaint(x1.getFlt(), y1.getFlt(), c1,
  97.                                          x2.getFlt(), y2.getFlt(), c2,
  98.                                          true);
  99.         g2.setPaint(gp);
  100.         g2.fillRect(0, 0, w, h);
  101.         g2.setColor(Color.yellow);
  102.         g2.drawLine(x1.getInt(), y1.getInt(), x2.getInt(), y2.getInt());
  103.     }
  104.     public class animval {
  105.         float curval;
  106.         float lowval;
  107.         float highval;
  108.         float currate;
  109.         float lowrate;
  110.         float highrate;
  111.         public animval(int lowval, int highval,
  112.                        int lowrate, int highrate) {
  113.             this.lowval = lowval;
  114.             this.highval = highval;
  115.             this.lowrate = lowrate;
  116.             this.highrate = highrate;
  117.             this.curval = randval(lowval, highval);
  118.             this.currate = randval(lowrate, highrate);
  119.         }
  120.         public float randval(float low, float high) {
  121.             return (float) (low + Math.random() * (high - low));
  122.         }
  123.         public float getFlt() {
  124.             return curval;
  125.         }
  126.         public int getInt() {
  127.             return (int) curval;
  128.         }
  129.         public void anim() {
  130.             curval += currate;
  131.             clip();
  132.         }
  133.         public void clip() {
  134.             if (curval > highval) {
  135.                 curval = highval - (curval - highval);
  136.                 if (curval < lowval) {
  137.                     curval = highval;
  138.                 }
  139.                 currate = - randval(lowrate, highrate);
  140.             } else if (curval < lowval) {
  141.                 curval = lowval + (lowval - curval);
  142.                 if (curval > highval) {
  143.                     curval = lowval;
  144.                 }
  145.                 currate = randval(lowrate, highrate);
  146.             }
  147.         }
  148.         public void newlimits(int lowval, int highval) {
  149.             this.lowval = lowval;
  150.             this.highval = highval;
  151.             clip();
  152.         }
  153.     }
  154.     public static void main(String argv[]) {
  155.         final DemoPanel dp = new DemoPanel(new GradAnim());
  156.         Frame f = new Frame("Java2D Demo - GradAnim");
  157.         f.addWindowListener(new WindowAdapter() {
  158.             public void windowClosing(WindowEvent e) {System.exit(0);}
  159.             public void windowDeiconified(WindowEvent e) { 
  160.                 dp.surface.start(); 
  161.             }
  162.             public void windowIconified(WindowEvent e) { 
  163.                 dp.surface.stop(); 
  164.             }
  165.         });
  166.         f.add("Center", dp);
  167.         f.pack();
  168.         f.setSize(new Dimension(400,300));
  169.         f.show();
  170.         dp.surface.start();
  171.     }
  172. }