1 36 37 40 41 package java2d.demos.Images; 42 43 import java.awt.*; 44 import java.awt.geom.CubicCurve2D ; 45 import java.awt.geom.Point2D ; 46 import java.awt.geom.FlatteningPathIterator ; 47 import java.awt.geom.PathIterator ; 48 import java2d.AnimatingSurface; 49 50 51 54 public class WarpImage extends AnimatingSurface { 55 56 private static int iw, ih, iw2, ih2; 57 private static Image img; 58 private static final int FORWARD = 0; 59 private static final int BACK = 1; 60 private Point2D pts[]; 61 private int direction = FORWARD; 62 private int pNum; 63 private int x, y; 64 65 66 public WarpImage() { 67 setBackground(Color.white); 68 img = getImage("surfing.gif"); 69 iw = img.getWidth(this); 70 ih = img.getHeight(this); 71 iw2 = iw/2; 72 ih2 = ih/2; 73 } 74 75 76 public void reset(int w, int h) { 77 pNum = 0; 78 direction = FORWARD; 79 CubicCurve2D cc = new CubicCurve2D.Float ( 80 w*.2f, h*.5f, w*.4f,0, w*.6f,h,w*.8f,h*.5f); 81 PathIterator pi = cc.getPathIterator(null, 0.1); 82 Point2D tmp[] = new Point2D [200]; 83 int i = 0; 84 while ( !pi.isDone() ) { 85 float[] coords = new float[6]; 86 switch ( pi.currentSegment(coords) ) { 87 case PathIterator.SEG_MOVETO: 88 case PathIterator.SEG_LINETO: 89 tmp[i] = new Point2D.Float (coords[0], coords[1]); 90 } 91 i++; 92 pi.next(); 93 } 94 pts = new Point2D [i]; 95 System.arraycopy(tmp,0,pts,0,i); 96 } 97 98 99 public void step(int w, int h) { 100 if (pts == null) { 101 return; 102 } 103 x = (int) pts[pNum].getX(); 104 y = (int) pts[pNum].getY(); 105 if (direction == FORWARD) 106 if (++pNum == pts.length) 107 direction = BACK; 108 if (direction == BACK) 109 if (--pNum == 0) 110 direction = FORWARD; 111 } 112 113 114 public void render(int w, int h, Graphics2D g2) { 115 g2.drawImage(img, 116 0, 0, x, y, 117 0, 0, iw2, ih2, 118 this); 119 g2.drawImage(img, 120 x, 0, w, y, 121 iw2, 0, iw, ih2, 122 this); 123 g2.drawImage(img, 124 0, y, x, h, 125 0, ih2, iw2, ih, 126 this); 127 g2.drawImage(img, 128 x, y, w, h, 129 iw2, ih2, iw, ih, 130 this); 131 } 132 133 134 public static void main(String argv[]) { 135 createDemoFrame(new WarpImage()); 136 } 137 } 138 | Popular Tags |