Path: blob/master/src/demo/share/jfc/J2Ddemo/java2d/demos/Images/WarpImage.java
41175 views
/*1*2* Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7*8* - Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10*11* - Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* - Neither the name of Oracle nor the names of its16* contributors may be used to endorse or promote products derived17* from this software without specific prior written permission.18*19* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS20* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,21* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR22* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR23* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,24* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,25* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR26* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF27* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING28* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS29* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.30*/31package java2d.demos.Images;323334import java.awt.Color;35import java.awt.Graphics2D;36import java.awt.Image;37import java.awt.geom.CubicCurve2D;38import java.awt.geom.PathIterator;39import java.awt.geom.Point2D;40import java2d.AnimatingSurface;414243/**44* Warps a image on a CubicCurve2D flattened path.45*/46@SuppressWarnings("serial")47public class WarpImage extends AnimatingSurface {4849private static int iw, ih, iw2, ih2;50private static Image img;51private static final int FORWARD = 0;52private static final int BACK = 1;53private Point2D[] pts;54private int direction = FORWARD;55private int pNum;56private int x, y;5758@SuppressWarnings("LeakingThisInConstructor")59public WarpImage() {60setBackground(Color.white);61img = getImage("surfing.png");62iw = img.getWidth(this);63ih = img.getHeight(this);64iw2 = iw / 2;65ih2 = ih / 2;66}6768@Override69public void reset(int w, int h) {70pNum = 0;71direction = FORWARD;72CubicCurve2D cc = new CubicCurve2D.Float(73w * .2f, h * .5f, w * .4f, 0, w * .6f, h, w * .8f, h * .5f);74PathIterator pi = cc.getPathIterator(null, 0.1);75Point2D[] tmp = new Point2D[200];76int i = 0;77while (!pi.isDone()) {78float[] coords = new float[6];79switch (pi.currentSegment(coords)) {80case PathIterator.SEG_MOVETO:81case PathIterator.SEG_LINETO:82tmp[i] = new Point2D.Float(coords[0], coords[1]);83}84i++;85pi.next();86}87pts = new Point2D[i];88System.arraycopy(tmp, 0, pts, 0, i);89}9091@Override92public void step(int w, int h) {93if (pts == null) {94return;95}96x = (int) pts[pNum].getX();97y = (int) pts[pNum].getY();98if (direction == FORWARD) {99if (++pNum == pts.length) {100direction = BACK;101}102}103if (direction == BACK) {104if (--pNum == 0) {105direction = FORWARD;106}107}108}109110@Override111public void render(int w, int h, Graphics2D g2) {112g2.drawImage(img,1130, 0, x, y,1140, 0, iw2, ih2,115this);116g2.drawImage(img,117x, 0, w, y,118iw2, 0, iw, ih2,119this);120g2.drawImage(img,1210, y, x, h,1220, ih2, iw2, ih,123this);124g2.drawImage(img,125x, y, w, h,126iw2, ih2, iw, ih,127this);128}129130public static void main(String[] argv) {131createDemoFrame(new WarpImage());132}133}134135136