/* * @(#)WarpImage.java 1.11 98/06/29 * * Copyright 1998 by Sun Microsystems, Inc., * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A. * All rights reserved. * * This software is the confidential and proprietary information * of Sun Microsystems, Inc. ("Confidential Information"). You * shall not disclose such Confidential Information and shall use * it only in accordance with the terms of the license agreement * you entered into with Sun. */ package demos.Images; import java.awt.*; import java.awt.event.*; import java.awt.geom.CubicCurve2D; import java.awt.geom.Point2D; import java.awt.geom.FlatteningPathIterator; import java.awt.geom.PathIterator; import J2DCanvas; /** * Warps a image on a CubicCurve2D flattened path. */ public class WarpImage extends J2DCanvas { private int iw, ih, iw2, ih2; private Image img; private Point2D pts[]; private final int FORWARD = 0; private final int BACK = 1; private int direction = FORWARD; private int pNum = 0; public WarpImage() { setBackground(Color.white); img = getImage("images/surfing.gif"); iw = img.getWidth(this); ih = img.getHeight(this); iw2 = iw/2; ih2 = ih/2; runnable = true; } public void drawDemo(Graphics2D g2) { if (newBufferedImage) { CubicCurve2D cc = new CubicCurve2D.Float(w/6,h/2,w/3,0, w/1.5f,h,w-w/6,h/2); PathIterator pi = cc.getPathIterator(null); FlatteningPathIterator fpi = new FlatteningPathIterator(pi,0.1); Point2D tmp[] = new Point2D[200]; int i = 0; while ( !fpi.isDone() ) { float[] coords = new float[6]; switch ( fpi.currentSegment(coords) ) { case fpi.SEG_MOVETO: case fpi.SEG_LINETO: tmp[i] = new Point2D.Float(coords[0], coords[1]); } i++; fpi.next(); } pts = new Point2D[i]; System.arraycopy(tmp,0,pts,0,i); } int x = (int) pts[pNum].getX(); int y = (int) pts[pNum].getY(); if (direction == FORWARD) if (++pNum == pts.length) direction = BACK; if (direction == BACK) if (--pNum == 0) direction = FORWARD; g2.drawImage(img, 0, 0, x, y, 0, 0, iw2, ih2, this); g2.drawImage(img, x, 0, w, y, iw2, 0, iw, ih2, this); g2.drawImage(img, 0, y, x, h, 0, ih2, iw2, ih, this); g2.drawImage(img, x, y, w, h, iw2, ih2, iw, ih, this); } public static void main(String s[]) { final WarpImage demo = new WarpImage(); WindowListener l = new WindowAdapter() { public void windowClosing(WindowEvent e) {System.exit(0);} public void windowDeiconified(WindowEvent e) { demo.start(); } public void windowIconified(WindowEvent e) { demo.stop(); } }; Frame f = new Frame("Java2D Demo - WarpImage"); f.addWindowListener(l); f.add("Center", demo); f.pack(); f.setSize(new Dimension(400,300)); f.show(); demo.start(); } }