/* * @(#)FadeAnim.java 1.20 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.Composite; import java.awt.*; import java.awt.geom.*; import java.awt.image.*; import java.awt.event.*; import java.awt.font.TextLayout; import java.awt.font.FontRenderContext; import J2DCanvas; /** * Animation of compositing shapes, text and images fading in and out. */ public class FadeAnim extends J2DCanvas { private double[] xx, yy; private Object[] objects; private Color colors[] = { Color.blue, Color.cyan, Color.green, Color.magenta, Color.orange, Color.pink, Color.red, Color.yellow, Color.lightGray, Color.white }; private float alphas[]; private final int UP = 0; private final int DOWN = 1; private int alphaDirection[]; private Image imgs[]; private int imgN; public FadeAnim() { setBackground(Color.black); objects = new Object[16]; objects[0] = getImage("images/jumptojavastrip.gif"); imgs = new Image[10]; MediaTracker tracker = new MediaTracker(this); for (int i=0, x=0; i < 10; i++, x+=80) { CropImageFilter cif = new CropImageFilter(x, 0, 80, 80); ImageProducer ip = ((Image) objects[0]).getSource(); ip = new FilteredImageSource(ip, cif); imgs[i] = getToolkit().createImage(ip); tracker.addImage(imgs[i], i); } try { tracker.waitForAll(); } catch (Exception e) { e.printStackTrace(); } objects[1] = new Ellipse2D.Float(); objects[2] = new Rectangle2D.Float(); objects[3] = new Arc2D.Float(); objects[4] = null; objects[5] = new RoundRectangle2D.Float(); objects[6] = new Ellipse2D.Float(); objects[7] = new Rectangle2D.Float(); objects[8] = null; objects[9] = new Arc2D.Float(); objects[10] = new QuadCurve2D.Float(); objects[11] = new CubicCurve2D.Float(); objects[12] = null; objects[13] = new RoundRectangle2D.Float(); objects[14] = getImage("images/duke.gif"); objects[15] = getImage("images/star7.gif"); xx = new double[objects.length]; yy = new double[objects.length]; alphas = new float[objects.length]; alphaDirection = new int[objects.length]; for (int i = 0; i < objects.length; i++) { alphas[i] = (float) Math.random(); alphaDirection[i] = ((i%2) == 0) ? UP : DOWN; } runnable = true; } public void getRandomXY(int i) { if (objects[i] instanceof TextLayout) { xx[i] = Math.random() * (w - ((TextLayout) objects[i]).getBounds().getWidth()); double y = Math.random() * h; yy[i] = (y < ((TextLayout) objects[i]).getAscent()) ? ((TextLayout) objects[i]).getAscent() : y; } else if (objects[i] instanceof Image) { xx[i] = Math.random() * (w - ((Image) objects[i]).getWidth(this)); yy[i] = Math.random() * (h - ((Image) objects[i]).getHeight(this)); } else { xx[i] = Math.random() * (w -((Shape) objects[i]).getBounds().getWidth()); yy[i] = Math.random() * (h -((Shape) objects[i]).getBounds().getHeight()); } } public void drawDemo(Graphics2D g2) { if (newBufferedImage) { FontRenderContext frc = g2.getFontRenderContext(); Font f = new Font("Courier", Font.BOLD, 48); objects[4] = new TextLayout("Alpha", f, frc); f = new Font("serif", Font.BOLD + Font.ITALIC, 32); objects[8] = new TextLayout("Composite", f, frc); f = new Font("Helvetica", Font.PLAIN, 32); objects[12] = new TextLayout("Java2D", f, frc); for (int i = 0; i < objects.length; i++ ) getRandomXY(i); } for (int i = 0; i < objects.length; i++) { if (alphaDirection[i] == UP) { if ((alphas[i] += 0.05) > .99) { alphaDirection[i] = DOWN; alphas[i] = 1.0f; } } else if (alphaDirection[i] == DOWN) { if ((alphas[i] -= .05) < 0.01) { alphaDirection[i] = UP; alphas[i] = 0; getRandomXY(i); if (i == 0) imgN = 0; } } AlphaComposite composite = AlphaComposite.getInstance( AlphaComposite.SRC_OVER, alphas[i]); g2.setComposite(composite); g2.setColor(colors[i%colors.length]); if (objects[i] instanceof TextLayout) { ((TextLayout)objects[i]).draw(g2, (float) xx[i], (float) yy[i]); } else if (objects[i] instanceof Image ) { if (i == 0) objects[i] = imgs[(imgN++)%imgs.length]; g2.drawImage((Image) objects[i], (int) xx[i], (int) yy[i], null); } else { if (objects[i] instanceof Ellipse2D) { ((Ellipse2D) objects[i]).setFrame(xx[i], yy[i], w/6, w/6); } else if (objects[i] instanceof Rectangle2D) { ((Rectangle2D) objects[i]).setRect(xx[i], yy[i], w/6, h/6); } else if (objects[i] instanceof RoundRectangle2D) { ((RoundRectangle2D) objects[i]).setRoundRect(xx[i], yy[i],w/8,w/8,10,10); } else if (objects[i] instanceof Arc2D) { ((Arc2D) objects[i]).setArc(xx[i], yy[i], w/5, w/5, 45, 270, Arc2D.PIE); } else if (objects[i] instanceof QuadCurve2D) { ((QuadCurve2D) objects[i]).setCurve(xx[i], yy[i], xx[i]+w/4, yy[i]-h/8, xx[i]+w/2, yy[i]); } else if (objects[i] instanceof CubicCurve2D) { ((CubicCurve2D) objects[i]).setCurve(xx[i], yy[i], xx[i]+30, yy[i]-h/8, xx[i]+h/8, yy[i]+60, xx[i]+90,yy[i]); } if (objects[i] instanceof QuadCurve2D || objects[i] instanceof CubicCurve2D) { g2.setStroke(new BasicStroke(5.0f)); g2.draw((Shape) objects[i]); } else g2.fill((Shape) objects[i]); } } } public static void main(String s[]) { final FadeAnim demo = new FadeAnim(); 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 - FadeAnim"); f.addWindowListener(l); f.add("Center", demo); f.pack(); f.setSize(new Dimension(400,300)); f.show(); demo.start(); } }