/* * @(#)ClipAnim.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.Clipping; import java.awt.*; import java.awt.geom.*; import java.awt.event.*; import J2DCanvas; /** * Animated clipping of an image & composited shapes. */ public class ClipAnim extends J2DCanvas { private Image img; private Shape clip; private Rectangle2D rectClip = new Rectangle2D.Float(); private final double OINC[] = {5.0, 3.0}; private final double SINC[] = {5.0, 5.0}; private double x, y; private double ix = OINC[0]; private double iy = OINC[1]; private double iw = SINC[0]; private double ih = SINC[1]; private double ew, eh; // ellipse width & height public ClipAnim() { setBackground(Color.white); img = getImage("images/clouds.jpg"); runnable = true; } public void drawDemo(Graphics2D g2) { if (newBufferedImage) { x = Math.random()*w; y = Math.random()*h; ew = (Math.random()*w)/2; eh = (Math.random()*h)/2; } x += ix; y += iy; ew += iw; eh += ih; if (ew > w/2) { ew = w/2; iw = Math.random() * -w/16 - 1; } if (ew < w/8) { ew = w/8; iw = Math.random() * w/16 + 1; } if (eh > h/2) { eh = h/2; ih = Math.random() * -h/16 - 1; } if (eh < h/8) { eh = h/8; ih = Math.random() * h/16 + 1; } if ((x+ew) > w) { x = (w - ew)-1; ix = Math.random() * -w/32 - 1; } if (x < 0) { x = 2; ix = Math.random() * w/32 + 1; } if ((y+eh) > h) { y = (h - eh)-2; iy = Math.random() * -h/32 - 1; } if (y < 0) { y = 2; iy = Math.random() * h/32 + 1; } clip = new Ellipse2D.Float((float)x, (float)y, (float)ew, (float)eh); rectClip.setRect(x+5, y+5, ew-10, eh-10); g2.setClip(clip); g2.clip(rectClip); g2.setComposite(AlphaComposite.SrcOver); g2.drawImage(img, 0, 0, w, h, this); AlphaComposite composite = AlphaComposite.getInstance( AlphaComposite.SRC_OVER, .5f); GeneralPath p = new GeneralPath(); p.moveTo(- w / 2.0f, - h / 8.0f); p.lineTo(+ w / 2.0f, - h / 8.0f); p.lineTo(- w / 4.0f, + h / 2.0f); p.lineTo(+ 0.0f, - h / 2.0f); p.lineTo(+ w / 4.0f, + h / 2.0f); p.closePath(); AffineTransform at = new AffineTransform(); at.translate(w*.5f, h*.5f); g2.setPaint(new Color(255, 0, 0)); g2.transform(at); g2.setComposite(composite); g2.setStroke(new BasicStroke(20.0f)); g2.draw(p); g2.setPaint(new Color(0, 255, 0)); at.setToIdentity(); g2.setTransform(at); Shape shape = null; for (int yy = 0; yy < h; yy += 50) { for (int xx = 0, i=0; xx < w; i++, xx += 50) { switch (i) { case 0 : shape = new Arc2D.Float(xx, yy, 25, 25, 45, 270, Arc2D.PIE); break; case 1 : shape = new Ellipse2D.Float(xx, yy, 25, 25); break; case 2 : shape = new RoundRectangle2D.Float(xx, yy, 25, 25, 4, 4); break; case 3 : shape = new Rectangle2D.Float(xx, yy, 25, 25); i = -1; } g2.fill(shape); } } g2.setClip(new Rectangle(0,0,w,h)); } public static void main(String s[]) { final ClipAnim demo = new ClipAnim(); 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 - ClipAnim"); f.addWindowListener(l); f.add("Center", demo); f.pack(); f.setSize(new Dimension(400,300)); f.show(); demo.start(); } }