home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / JBuilder8.iso / Solaris / resource / jre / demo / jfc / Java2D / src / java2d / demos / Lines / LineAnim.java < prev   
Encoding:
Java Source  |  2002-09-06  |  5.6 KB  |  161 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.  * @(#)LineAnim.java    1.19 02/06/13
  38.  */
  39.  
  40. package java2d.demos.Lines;
  41.  
  42. import java.awt.*;
  43. import java.awt.geom.*;
  44. import java.awt.font.TextLayout;
  45. import java2d.AnimatingSurface;
  46.  
  47.  
  48. /**
  49.  * Lines & Paths animation illustrating BasicStroke attributes.
  50.  */
  51. public class LineAnim extends AnimatingSurface {
  52.  
  53.     private static int caps[] = { BasicStroke.CAP_BUTT, 
  54.                 BasicStroke.CAP_SQUARE, BasicStroke.CAP_ROUND};
  55.     private static int joins[] = { BasicStroke.JOIN_MITER, 
  56.                 BasicStroke.JOIN_BEVEL, BasicStroke.JOIN_ROUND};
  57.     private static Color colors[] = {Color.gray, Color.pink, Color.lightGray};
  58.     private static BasicStroke bs1 = new BasicStroke(1.0f);
  59.     private static final int CLOCKWISE = 0;
  60.     private static final int COUNTERCW = 1;
  61.  
  62.     private Line2D lines[] = new Line2D[3];
  63.     private int rAmt[] = new int[lines.length];
  64.     private int direction[] = new int[lines.length];
  65.     private int speed[] = new int[lines.length];
  66.     private BasicStroke strokes[] = new BasicStroke[lines.length];
  67.     private GeneralPath path;
  68.     private Point2D[] pts;
  69.     private float size;
  70.     private Ellipse2D ellipse = new Ellipse2D.Double();
  71.  
  72.  
  73.     public LineAnim() {
  74.         setBackground(Color.white);
  75.     }
  76.  
  77.     public void reset(int w, int h) {
  78.         size = (w > h) ? h/6f : w/6f;
  79.         for (int i = 0; i < lines.length; i++) {
  80.             lines[i] = new Line2D.Float(0,0,size,0);
  81.             strokes[i] = new BasicStroke(size/3, caps[i], joins[i]);
  82.             rAmt[i] = i * 360/lines.length;
  83.             direction[i] = i%2;
  84.             speed[i] = i + 1;
  85.         }
  86.  
  87.         path = new GeneralPath();
  88.         path.moveTo(size, -size/2);
  89.         path.lineTo(size+size/2, 0);
  90.         path.lineTo(size, +size/2);
  91.  
  92.         ellipse.setFrame(w/2-size*2-4.5f,h/2-size*2-4.5f,size*4,size*4);
  93.         PathIterator pi = ellipse.getPathIterator(null, 0.9);
  94.         Point2D[] points = new Point2D[100];
  95.         int num_pts = 0;
  96.         while ( !pi.isDone() ) {
  97.             float[] pt = new float[6];
  98.             switch ( pi.currentSegment(pt) ) {
  99.                 case FlatteningPathIterator.SEG_MOVETO:
  100.                 case FlatteningPathIterator.SEG_LINETO:
  101.                     points[num_pts] = new Point2D.Float(pt[0], pt[1]);
  102.                     num_pts++;
  103.             }
  104.             pi.next();
  105.         }
  106.         pts = new Point2D[num_pts];
  107.         System.arraycopy(points, 0, pts, 0, num_pts);
  108.     }
  109.  
  110.  
  111.     public void step(int w, int h) {
  112.         for (int i = 0; i < lines.length; i++) {
  113.             if (direction[i] == CLOCKWISE) {
  114.                 rAmt[i] += speed[i];
  115.                 if (rAmt[i] == 360) {
  116.                     rAmt[i] = 0;
  117.                 }
  118.             } else {
  119.                 rAmt[i] -= speed[i];
  120.                 if (rAmt[i] == 0) {
  121.                     rAmt[i] = 360;
  122.                 }
  123.             }
  124.         }
  125.     }
  126.  
  127.  
  128.     public void render(int w, int h, Graphics2D g2) {
  129.  
  130.         ellipse.setFrame(w/2-size,h/2-size,size*2,size*2);
  131.         g2.setColor(Color.black);
  132.         g2.draw(ellipse);
  133.  
  134.         for (int i = 0; i < lines.length; i++) {
  135.             AffineTransform at = AffineTransform.getTranslateInstance(w/2,h/2);
  136.             at.rotate(Math.toRadians(rAmt[i]));
  137.             g2.setStroke(strokes[i]);
  138.             g2.setColor(colors[i]);
  139.             g2.draw(at.createTransformedShape(lines[i]));
  140.             g2.draw(at.createTransformedShape(path));
  141.  
  142.             int j = (int) ((double) rAmt[i]/360 * pts.length);
  143.             j = (j == pts.length) ? pts.length-1 : j;
  144.             ellipse.setFrame(pts[j].getX(), pts[j].getY(), 9, 9);
  145.             g2.fill(ellipse);
  146.         }
  147.  
  148.         g2.setStroke(bs1);
  149.         g2.setColor(Color.black);
  150.         for (int i = 0; i < pts.length; i++) {
  151.             ellipse.setFrame(pts[i].getX(), pts[i].getY(), 9, 9);
  152.             g2.draw(ellipse);
  153.         }
  154.     }
  155.  
  156.  
  157.     public static void main(String argv[]) {
  158.         createDemoFrame(new LineAnim());
  159.     }
  160. }
  161.