KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java2d > demos > Images > WarpImage


1 /*
2  * @(#)WarpImage.java 1.26 05/11/17
3  *
4  * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * -Redistribution of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  *
12  * -Redistribution in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * Neither the name of Sun Microsystems, Inc. or the names of contributors may
17  * be used to endorse or promote products derived from this software without
18  * specific prior written permission.
19  *
20  * This software is provided "AS IS," without a warranty of any kind. ALL
21  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
22  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
23  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN")
24  * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
25  * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
26  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
27  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
28  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
29  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
30  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
31  *
32  * You acknowledge that this software is not designed, licensed or intended
33  * for use in the design, construction, operation or maintenance of any
34  * nuclear facility.
35  */

36
37 /*
38  * @(#)WarpImage.java 1.26 05/11/17
39  */

40
41 package java2d.demos.Images;
42
43 import java.awt.*;
44 import java.awt.geom.CubicCurve2D JavaDoc;
45 import java.awt.geom.Point2D JavaDoc;
46 import java.awt.geom.FlatteningPathIterator JavaDoc;
47 import java.awt.geom.PathIterator JavaDoc;
48 import java2d.AnimatingSurface;
49
50
51 /**
52  * Warps a image on a CubicCurve2D flattened path.
53  */

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 JavaDoc 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 JavaDoc cc = new CubicCurve2D.Float JavaDoc(
80                         w*.2f, h*.5f, w*.4f,0, w*.6f,h,w*.8f,h*.5f);
81         PathIterator JavaDoc pi = cc.getPathIterator(null, 0.1);
82         Point2D JavaDoc tmp[] = new Point2D JavaDoc[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 JavaDoc(coords[0], coords[1]);
90             }
91             i++;
92             pi.next();
93         }
94         pts = new Point2D JavaDoc[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 JavaDoc argv[]) {
135         createDemoFrame(new WarpImage());
136     }
137 }
138
Popular Tags