/* * @(#)ACimages.java 1.14 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.event.*; import java.awt.geom.Ellipse2D; import java.awt.geom.Rectangle2D; import java.awt.geom.RoundRectangle2D; import java.awt.font.TextLayout; import java.awt.font.FontRenderContext; import J2DCanvas; /** * Compositing shapes on images. */ public class ACimages extends J2DCanvas { private int numImgs = 9; private Image imgs[]; public ACimages() { setBackground(Color.white); String s[] = { "box", "fight", "magnify", "boxwave", "globe", "snooze", "tip", "thumbsup", "dukeplug"}; imgs = new Image[numImgs]; for (int i = 0; i < imgs.length; i++) { imgs[i] = getImage("images/" + s[i] + ".gif"); } } public void drawDemo(Graphics2D g2) { float alpha = 0.0f; Color colors[] = { Color.blue, Color.cyan, Color.green, Color.magenta, Color.orange, Color.pink, Color.red, Color.yellow, Color.lightGray }; int iw = w/3; int ih = (h-45)/3; float xx = 0, yy = 15; for (int i =0; i < imgs.length; i++) { xx = (i%3 == 0) ? 0 : xx+w/3; switch (i) { case 3 : yy = h/3+15; break; case 6 : yy = h/3*2+15; } g2.setComposite(AlphaComposite.SrcOver); g2.setColor(Color.black); AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha += .1f); String s = "a=" + Float.toString(alpha).substring(0,3); new TextLayout(s,g2.getFont(), g2.getFontRenderContext()).draw(g2, xx+3, yy-2); Shape shape=null; switch (i%3) { case 0 : shape = new Ellipse2D.Float(xx, yy, iw, ih); break; case 1 : shape = new RoundRectangle2D.Float(xx, yy, iw, ih, 25, 25); break; case 2 : shape = new Rectangle2D.Float(xx, yy, iw, ih); break; } g2.setColor(colors[i]); g2.setComposite(ac); g2.fill(shape); g2.drawImage(imgs[i], (int) xx, (int) yy, iw, ih, this); } } public static void main(String s[]) { WindowListener l = new WindowAdapter() { public void windowClosing(WindowEvent e) {System.exit(0);} }; Frame f = new Frame("Java2D Demo - ACimages"); f.addWindowListener(l); f.add("Center", new ACimages()); f.pack(); f.setSize(new Dimension(400,300)); f.show(); } }