1 /*
2 *
3 * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * - Neither the name of Oracle nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34
35 import java.awt.Color;
36 import java.awt.Graphics2D;
37 import java.awt.Image;
38 import java.awt.geom.CubicCurve2D;
39 import java.awt.geom.PathIterator;
40 import java.awt.geom.Point2D;
41 import java2d.AnimatingSurface;
42
43
44 /**
45 * Warps a image on a CubicCurve2D flattened path.
46 */
47 @SuppressWarnings("serial")
48 public class WarpImage extends AnimatingSurface {
49
50 private static int iw, ih, iw2, ih2;
51 private static Image img;
52 private static final int FORWARD = 0;
53 private static final int BACK = 1;
54 private Point2D pts[];
55 private int direction = FORWARD;
56 private int pNum;
57 private int x, y;
58
59 @SuppressWarnings("LeakingThisInConstructor")
60 public WarpImage() {
61 setBackground(Color.white);
62 img = getImage("surfing.png");
63 iw = img.getWidth(this);
64 ih = img.getHeight(this);
65 iw2 = iw / 2;
66 ih2 = ih / 2;
67 }
68
69 @Override
70 public void reset(int w, int h) {
71 pNum = 0;
72 direction = FORWARD;
73 CubicCurve2D cc = new CubicCurve2D.Float(
74 w * .2f, h * .5f, w * .4f, 0, w * .6f, h, w * .8f, h * .5f);
75 PathIterator pi = cc.getPathIterator(null, 0.1);
76 Point2D tmp[] = new Point2D[200];
77 int i = 0;
78 while (!pi.isDone()) {
79 float[] coords = new float[6];
80 switch (pi.currentSegment(coords)) {
81 case PathIterator.SEG_MOVETO:
82 case PathIterator.SEG_LINETO:
83 tmp[i] = new Point2D.Float(coords[0], coords[1]);
84 }
85 i++;
86 pi.next();
87 }
88 pts = new Point2D[i];
89 System.arraycopy(tmp, 0, pts, 0, i);
90 }
91
92 @Override
93 public void step(int w, int h) {
94 if (pts == null) {
95 return;
96 }
111 @Override
112 public void render(int w, int h, Graphics2D g2) {
113 g2.drawImage(img,
114 0, 0, x, y,
115 0, 0, iw2, ih2,
116 this);
117 g2.drawImage(img,
118 x, 0, w, y,
119 iw2, 0, iw, ih2,
120 this);
121 g2.drawImage(img,
122 0, y, x, h,
123 0, ih2, iw2, ih,
124 this);
125 g2.drawImage(img,
126 x, y, w, h,
127 iw2, ih2, iw, ih,
128 this);
129 }
130
131 public static void main(String argv[]) {
132 createDemoFrame(new WarpImage());
133 }
134 }
|
1 /*
2 *
3 * Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * - Neither the name of Oracle nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34
35 import java.awt.Color;
36 import java.awt.Graphics2D;
37 import java.awt.Image;
38 import java.awt.geom.CubicCurve2D;
39 import java.awt.geom.PathIterator;
40 import java.awt.geom.Point2D;
41 import java2d.AnimatingSurface;
42
43
44 /**
45 * Warps a image on a CubicCurve2D flattened path.
46 */
47 @SuppressWarnings("serial")
48 public class WarpImage extends AnimatingSurface {
49
50 private static int iw, ih, iw2, ih2;
51 private static Image img;
52 private static final int FORWARD = 0;
53 private static final int BACK = 1;
54 private Point2D[] pts;
55 private int direction = FORWARD;
56 private int pNum;
57 private int x, y;
58
59 @SuppressWarnings("LeakingThisInConstructor")
60 public WarpImage() {
61 setBackground(Color.white);
62 img = getImage("surfing.png");
63 iw = img.getWidth(this);
64 ih = img.getHeight(this);
65 iw2 = iw / 2;
66 ih2 = ih / 2;
67 }
68
69 @Override
70 public void reset(int w, int h) {
71 pNum = 0;
72 direction = FORWARD;
73 CubicCurve2D cc = new CubicCurve2D.Float(
74 w * .2f, h * .5f, w * .4f, 0, w * .6f, h, w * .8f, h * .5f);
75 PathIterator pi = cc.getPathIterator(null, 0.1);
76 Point2D[] tmp = new Point2D[200];
77 int i = 0;
78 while (!pi.isDone()) {
79 float[] coords = new float[6];
80 switch (pi.currentSegment(coords)) {
81 case PathIterator.SEG_MOVETO:
82 case PathIterator.SEG_LINETO:
83 tmp[i] = new Point2D.Float(coords[0], coords[1]);
84 }
85 i++;
86 pi.next();
87 }
88 pts = new Point2D[i];
89 System.arraycopy(tmp, 0, pts, 0, i);
90 }
91
92 @Override
93 public void step(int w, int h) {
94 if (pts == null) {
95 return;
96 }
111 @Override
112 public void render(int w, int h, Graphics2D g2) {
113 g2.drawImage(img,
114 0, 0, x, y,
115 0, 0, iw2, ih2,
116 this);
117 g2.drawImage(img,
118 x, 0, w, y,
119 iw2, 0, iw, ih2,
120 this);
121 g2.drawImage(img,
122 0, y, x, h,
123 0, ih2, iw2, ih,
124 this);
125 g2.drawImage(img,
126 x, y, w, h,
127 iw2, ih2, iw, ih,
128 this);
129 }
130
131 public static void main(String[] argv) {
132 createDemoFrame(new WarpImage());
133 }
134 }
|