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

2D Graphic

Development Platform:

Java

  1. /*
  2.  * @(#)WarpImage.java 1.16 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.Images;
  31. import java.awt.*;
  32. import java.awt.event.*;
  33. import java.awt.geom.CubicCurve2D;
  34. import java.awt.geom.Point2D;
  35. import java.awt.geom.FlatteningPathIterator;
  36. import java.awt.geom.PathIterator;
  37. import AnimatingContext;
  38. import DemoSurface;
  39. import DemoPanel;
  40. /**
  41.  * Warps a image on a CubicCurve2D flattened path.
  42.  */
  43. public class WarpImage extends DemoSurface implements AnimatingContext {
  44.     private static int iw, ih, iw2, ih2;
  45.     private static Image img;
  46.     private static final int FORWARD = 0;
  47.     private static final int BACK = 1;
  48.     private Point2D pts[];
  49.     private int direction = FORWARD;
  50.     private int pNum;
  51.     private int x, y;
  52.     public WarpImage() {
  53.         setBackground(Color.white);
  54.         img = getImage("surfing.gif");
  55.         iw = img.getWidth(this);
  56.         ih = img.getHeight(this);
  57.         iw2 = iw/2;
  58.         ih2 = ih/2;
  59.     }
  60.     public void reset(int w, int h) {
  61.         pNum = 0;
  62.         direction = FORWARD;
  63.         CubicCurve2D cc = new CubicCurve2D.Float(
  64.                         w*.2f, h*.5f, w*.4f,0, w*.6f,h,w*.8f,h*.5f);
  65.         PathIterator pi = cc.getPathIterator(null, 0.1);
  66.         Point2D tmp[] = new Point2D[200];
  67.         int i = 0;
  68.         while ( !pi.isDone() ) {
  69.             float[] coords = new float[6];
  70.             switch ( pi.currentSegment(coords) ) {
  71.                 case pi.SEG_MOVETO:
  72.                 case pi.SEG_LINETO:
  73.                         tmp[i] = new Point2D.Float(coords[0], coords[1]);
  74.             }
  75.             i++;
  76.             pi.next();
  77.         }
  78.         pts = new Point2D[i];
  79.         System.arraycopy(tmp,0,pts,0,i);
  80.     }
  81.     public void step(int w, int h) {
  82.         if (pts == null) {
  83.             return;
  84.         }
  85.         x = (int) pts[pNum].getX();
  86.         y = (int) pts[pNum].getY();
  87.         if (direction == FORWARD)
  88.             if (++pNum == pts.length)
  89.                 direction = BACK;
  90.         if (direction == BACK)
  91.             if (--pNum == 0)
  92.                 direction = FORWARD;
  93.     }
  94.     public void drawDemo(int w, int h, Graphics2D g2) {
  95.         g2.drawImage(img,
  96.                         0,              0,              x,              y,
  97.                         0,              0,              iw2,            ih2,
  98.                         this);
  99.         g2.drawImage(img,
  100.                         x,              0,              w,              y,
  101.                         iw2,            0,              iw,             ih2,
  102.                         this);
  103.         g2.drawImage(img,
  104.                         0,              y,              x,              h,
  105.                         0,              ih2,            iw2,            ih,
  106.                         this);
  107.         g2.drawImage(img,
  108.                         x,              y,              w,              h,
  109.                         iw2,            ih2,            iw,             ih,
  110.                         this);
  111.     }
  112.     public static void main(String argv[]) {
  113.         final DemoPanel dp = new DemoPanel(new WarpImage());
  114.         Frame f = new Frame("Java2D Demo - WarpImage");
  115.         f.addWindowListener(new WindowAdapter() {
  116.             public void windowClosing(WindowEvent e) {System.exit(0);}
  117.             public void windowDeiconified(WindowEvent e) { 
  118.                 dp.surface.start(); 
  119.             }
  120.             public void windowIconified(WindowEvent e) { 
  121.                 dp.surface.stop(); 
  122.             }
  123.         });
  124.         f.add("Center", dp);
  125.         f.pack();
  126.         f.setSize(new Dimension(400,300));
  127.         f.show();
  128.         dp.surface.start();
  129.     }
  130. }