home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / JBuilder8.iso / Solaris / resource / jre / demo / jfc / SwingSet2 / src / BezierAnimationPanel.java < prev    next >
Encoding:
Java Source  |  2002-09-06  |  8.9 KB  |  334 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.  * @(#)BezierAnimationPanel.java    1.10 02/06/13
  38.  */
  39.  
  40.  
  41. import javax.swing.*;
  42. import javax.swing.event.*;
  43. import javax.swing.text.*;
  44. import javax.swing.border.*;
  45. import javax.swing.colorchooser.*;
  46. import javax.swing.filechooser.*;
  47. import javax.accessibility.*;
  48.  
  49. import java.awt.*;
  50. import java.awt.font.*;
  51. import java.awt.geom.*;
  52. import java.awt.image.*;
  53. import java.awt.event.*;
  54.  
  55. /**
  56.  * BezierAnimationPanel
  57.  *
  58.  * @version 1.10 06/13/02
  59.  * @author Jim Graham
  60.  * @author Jeff Dinkins (removed dynamic setting changes, made swing friendly)
  61.  */
  62. class BezierAnimationPanel extends JPanel implements Runnable {
  63.  
  64.     Color backgroundColor =  new Color(0,     0, 153);
  65.     Color outerColor      =  new Color(255, 255, 255);
  66.     Color gradientColorA  =  new Color(255,   0, 101);
  67.     Color gradientColorB  =  new Color(255, 255,   0);
  68.  
  69.     boolean bgChanged = false;
  70.  
  71.     GradientPaint gradient = null;
  72.     
  73.     public final int NUMPTS = 6;
  74.  
  75.     float animpts[] = new float[NUMPTS * 2];
  76.  
  77.     float deltas[] = new float[NUMPTS * 2];
  78.  
  79.     float staticpts[] = {
  80.      50.0f,   0.0f,
  81.     150.0f,   0.0f,
  82.     200.0f,  75.0f,
  83.     150.0f, 150.0f,
  84.      50.0f, 150.0f,
  85.       0.0f,  75.0f,
  86.     };
  87.  
  88.     float movepts[] = new float[staticpts.length];
  89.  
  90.     BufferedImage img;
  91.  
  92.     Rectangle bounds = null;
  93.  
  94.     Thread anim;
  95.  
  96.     /**
  97.      * BezierAnimationPanel Constructor
  98.      */
  99.     public BezierAnimationPanel() {
  100.     addHierarchyListener(
  101.         new HierarchyListener() {
  102.            public void hierarchyChanged(HierarchyEvent e) {
  103.            if(isShowing()) {
  104.                start();
  105.            } else {
  106.                stop();
  107.            }
  108.            }
  109.        }
  110.     );
  111.     setBackground(getBackgroundColor());
  112.     }
  113.  
  114.     public Color getGradientColorA() {
  115.     return gradientColorA;
  116.     }
  117.  
  118.     public void setGradientColorA(Color c) {
  119.     if(c != null) {
  120.         gradientColorA = c;
  121.     }
  122.     }
  123.  
  124.     public Color getGradientColorB() {
  125.     return gradientColorB;
  126.     }
  127.  
  128.     public void setGradientColorB(Color c) {
  129.     if(c != null) {
  130.         gradientColorB = c;
  131.     }
  132.     }
  133.  
  134.     public Color getOuterColor() {
  135.     return outerColor;
  136.     }
  137.  
  138.     public void setOuterColor(Color c) {
  139.     if(c != null) {
  140.         outerColor = c;
  141.     }
  142.     }
  143.  
  144.     public Color getBackgroundColor() {
  145.     return backgroundColor;
  146.     }
  147.  
  148.     public void setBackgroundColor(Color c) {
  149.     if(c != null) {
  150.         backgroundColor = c;
  151.         setBackground(c);
  152.         bgChanged = true;
  153.     }
  154.     }
  155.  
  156.     public void start() {
  157.     Dimension size = getSize();
  158.     for (int i = 0; i < animpts.length; i += 2) {
  159.         animpts[i + 0] = (float) (Math.random() * size.width);
  160.         animpts[i + 1] = (float) (Math.random() * size.height);
  161.         deltas[i + 0] = (float) (Math.random() * 4.0 + 2.0);
  162.         deltas[i + 1] = (float) (Math.random() * 4.0 + 2.0);
  163.         if (animpts[i + 0] > size.width / 6.0f) {
  164.         deltas[i + 0] = -deltas[i + 0];
  165.         }
  166.         if (animpts[i + 1] > size.height / 6.0f) {
  167.         deltas[i + 1] = -deltas[i + 1];
  168.         }
  169.     }
  170.     anim = new Thread(this);
  171.     anim.setPriority(Thread.MIN_PRIORITY);
  172.     anim.start();
  173.     }
  174.  
  175.     public synchronized void stop() {
  176.     anim = null;
  177.     notify();
  178.     }
  179.  
  180.     public void animate(float[] pts, float[] deltas, int index, int limit) {
  181.     float newpt = pts[index] + deltas[index];
  182.     if (newpt <= 0) {
  183.         newpt = -newpt;
  184.         deltas[index] = (float) (Math.random() * 3.0 + 2.0);
  185.     } else if (newpt >= (float) limit) {
  186.         newpt = 2.0f * limit - newpt;
  187.         deltas[index] = - (float) (Math.random() * 3.0 + 2.0);
  188.     }
  189.     pts[index] = newpt;
  190.     }
  191.  
  192.     public void run() {
  193.     Thread me = Thread.currentThread();
  194.     while (getSize().width <= 0) {
  195.         try {
  196.         anim.sleep(500);
  197.         } catch (InterruptedException e) {
  198.         return;
  199.         }
  200.         }
  201.          
  202.     Graphics2D g2d = null;
  203.     Graphics2D BufferG2D = null;
  204.     Graphics2D ScreenG2D = null;
  205.     BasicStroke solid = new BasicStroke(9.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND, 9.0f);
  206.     GeneralPath gp = new GeneralPath(GeneralPath.WIND_NON_ZERO);
  207.     int rule = AlphaComposite.SRC_OVER;
  208.     AlphaComposite opaque = AlphaComposite.SrcOver;
  209.     AlphaComposite blend = AlphaComposite.getInstance(rule, 0.9f);
  210.     AlphaComposite set = AlphaComposite.Src;
  211.     int frame = 0;
  212.     int frametmp = 0;
  213.     Dimension oldSize = getSize();
  214.     Shape clippath = null;
  215.     while (anim == me) {
  216.         Dimension size = getSize();
  217.         if (size.width != oldSize.width || size.height != oldSize.height) {
  218.         img = null;
  219.         clippath = null;
  220.         if (BufferG2D != null) {
  221.             BufferG2D.dispose();
  222.             BufferG2D = null;
  223.         }
  224.         if (ScreenG2D != null) {
  225.             ScreenG2D.dispose();
  226.             ScreenG2D = null;
  227.         }
  228.         }
  229.         oldSize = size;
  230.  
  231.         if (img == null) {
  232.         img = (BufferedImage) createImage(size.width, size.height);
  233.         }
  234.  
  235.         if (BufferG2D == null) {
  236.         BufferG2D = img.createGraphics();
  237.         BufferG2D.setRenderingHint(RenderingHints.KEY_RENDERING,
  238.                        RenderingHints.VALUE_RENDER_DEFAULT);
  239.         BufferG2D.setClip(clippath);
  240.         }
  241.         g2d = BufferG2D;
  242.  
  243.         float[] ctrlpts;
  244.         for (int i = 0; i < animpts.length; i += 2) {
  245.         animate(animpts, deltas, i + 0, size.width);
  246.         animate(animpts, deltas, i + 1, size.height);
  247.         }
  248.         ctrlpts = animpts;
  249.         int len = ctrlpts.length;
  250.         gp.reset();
  251.         int dir = 0;
  252.         float prevx = ctrlpts[len - 2];
  253.         float prevy = ctrlpts[len - 1];
  254.         float curx = ctrlpts[0];
  255.         float cury = ctrlpts[1];
  256.         float midx = (curx + prevx) / 2.0f;
  257.         float midy = (cury + prevy) / 2.0f;
  258.         gp.moveTo(midx, midy);
  259.         for (int i = 2; i <= ctrlpts.length; i += 2) {
  260.         float x1 = (midx + curx) / 2.0f;
  261.         float y1 = (midy + cury) / 2.0f;
  262.         prevx = curx;
  263.         prevy = cury;
  264.         if (i < ctrlpts.length) {
  265.             curx = ctrlpts[i + 0];
  266.             cury = ctrlpts[i + 1];
  267.         } else {
  268.             curx = ctrlpts[0];
  269.             cury = ctrlpts[1];
  270.         }
  271.         midx = (curx + prevx) / 2.0f;
  272.         midy = (cury + prevy) / 2.0f;
  273.         float x2 = (prevx + midx) / 2.0f;
  274.         float y2 = (prevy + midy) / 2.0f;
  275.         gp.curveTo(x1, y1, x2, y2, midx, midy);
  276.         }
  277.         gp.closePath();
  278.  
  279.         g2d.setComposite(set);
  280.         g2d.setBackground(backgroundColor);
  281.         g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
  282.                  RenderingHints.VALUE_ANTIALIAS_OFF);
  283.  
  284.         if(bgChanged || bounds == null) {
  285.         bounds = new Rectangle(0, 0, getWidth(), getHeight());
  286.         bgChanged = false;
  287.         }
  288.         // g2d.clearRect(bounds.x-5, bounds.y-5, bounds.x + bounds.width + 5, bounds.y + bounds.height + 5);
  289.         g2d.clearRect(0, 0, getWidth(), getHeight());
  290.  
  291.         g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
  292.                  RenderingHints.VALUE_ANTIALIAS_ON);
  293.         g2d.setColor(outerColor);
  294.         g2d.setComposite(opaque);
  295.         g2d.setStroke(solid);
  296.         g2d.draw(gp);
  297.         g2d.setPaint(gradient);
  298.  
  299.         if(!bgChanged) {
  300.         bounds = gp.getBounds();
  301.         } else {
  302.         bounds = new Rectangle(0, 0, getWidth(), getHeight());
  303.         bgChanged = false;
  304.         }
  305.         gradient = new GradientPaint(bounds.x, bounds.y, gradientColorA,
  306.                      bounds.x + bounds.width, bounds.y + bounds.height,
  307.                      gradientColorB, true);
  308.         g2d.setComposite(blend);
  309.         g2d.fill(gp);
  310.  
  311.         if (g2d == BufferG2D) {
  312.         repaint();
  313.         }
  314.         ++frame;
  315.         Thread.yield();
  316.     }
  317.     if (g2d != null) {    
  318.         g2d.dispose();
  319.     }
  320.     }
  321.  
  322.     public void paint(Graphics g) {
  323.     synchronized (this) {
  324.        Graphics2D g2d = (Graphics2D) g;
  325.        if (img != null) {
  326.            int imgw = img.getWidth();
  327.            int imgh = img.getHeight();
  328.            g2d.setComposite(AlphaComposite.Src);
  329.            g2d.drawImage(img, null, 0, 0);
  330.        }
  331.         }
  332.     }
  333. }
  334.