home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / JBuilder8.iso / Solaris / resource / jre / demo / jfc / Java2D / src / java2d / demos / Images / WarpImage.java < prev   
Encoding:
Java Source  |  2002-09-06  |  4.7 KB  |  137 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.  * @(#)WarpImage.java    1.22 02/06/13
  38.  */
  39.  
  40. package java2d.demos.Images;
  41.  
  42. import java.awt.*;
  43. import java.awt.geom.CubicCurve2D;
  44. import java.awt.geom.Point2D;
  45. import java.awt.geom.FlatteningPathIterator;
  46. import java.awt.geom.PathIterator;
  47. import java2d.AnimatingSurface;
  48.  
  49.  
  50. /**
  51.  * Warps a image on a CubicCurve2D flattened path.
  52.  */
  53. public class WarpImage extends AnimatingSurface {
  54.  
  55.     private static int iw, ih, iw2, ih2;
  56.     private static Image img;
  57.     private static final int FORWARD = 0;
  58.     private static final int BACK = 1;
  59.     private Point2D pts[];
  60.     private int direction = FORWARD;
  61.     private int pNum;
  62.     private int x, y;
  63.  
  64.  
  65.     public WarpImage() {
  66.         setBackground(Color.white);
  67.         img = getImage("surfing.gif");
  68.         iw = img.getWidth(this);
  69.         ih = img.getHeight(this);
  70.         iw2 = iw/2;
  71.         ih2 = ih/2;
  72.     }
  73.  
  74.  
  75.     public void reset(int w, int h) {
  76.         pNum = 0;
  77.         direction = FORWARD;
  78.         CubicCurve2D cc = new CubicCurve2D.Float(
  79.                         w*.2f, h*.5f, w*.4f,0, w*.6f,h,w*.8f,h*.5f);
  80.         PathIterator pi = cc.getPathIterator(null, 0.1);
  81.         Point2D tmp[] = new Point2D[200];
  82.         int i = 0;
  83.         while ( !pi.isDone() ) {
  84.             float[] coords = new float[6];
  85.             switch ( pi.currentSegment(coords) ) {
  86.                 case PathIterator.SEG_MOVETO:
  87.                 case PathIterator.SEG_LINETO:
  88.                         tmp[i] = new Point2D.Float(coords[0], coords[1]);
  89.             }
  90.             i++;
  91.             pi.next();
  92.         }
  93.         pts = new Point2D[i];
  94.         System.arraycopy(tmp,0,pts,0,i);
  95.     }
  96.  
  97.  
  98.     public void step(int w, int h) {
  99.         if (pts == null) {
  100.             return;
  101.         }
  102.         x = (int) pts[pNum].getX();
  103.         y = (int) pts[pNum].getY();
  104.         if (direction == FORWARD)
  105.             if (++pNum == pts.length)
  106.                 direction = BACK;
  107.         if (direction == BACK)
  108.             if (--pNum == 0)
  109.                 direction = FORWARD;
  110.     }
  111.  
  112.  
  113.     public void render(int w, int h, Graphics2D g2) {
  114.         g2.drawImage(img,
  115.                         0,              0,              x,              y,
  116.                         0,              0,              iw2,            ih2,
  117.                         this);
  118.         g2.drawImage(img,
  119.                         x,              0,              w,              y,
  120.                         iw2,            0,              iw,             ih2,
  121.                         this);
  122.         g2.drawImage(img,
  123.                         0,              y,              x,              h,
  124.                         0,              ih2,            iw2,            ih,
  125.                         this);
  126.         g2.drawImage(img,
  127.                         x,              y,              w,              h,
  128.                         iw2,            ih2,            iw,             ih,
  129.                         this);
  130.     }
  131.  
  132.  
  133.     public static void main(String argv[]) {
  134.         createDemoFrame(new WarpImage());
  135.     }
  136. }
  137.