1 /*
   2  *
   3  * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
   4  *
   5  * Redistribution and use in source and binary forms, with or without
   6  * modification, are permitted provided that the following conditions
   7  * are met:
   8  *
   9  *   - Redistributions of source code must retain the above copyright
  10  *     notice, this list of conditions and the following disclaimer.
  11  *
  12  *   - Redistributions in binary form must reproduce the above copyright
  13  *     notice, this list of conditions and the following disclaimer in the
  14  *     documentation and/or other materials provided with the distribution.
  15  *
  16  *   - Neither the name of Oracle nor the names of its
  17  *     contributors may be used to endorse or promote products derived
  18  *     from this software without specific prior written permission.
  19  *
  20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  21  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31  */
  32 package java2d.demos.Composite;
  33 
  34 
  35 import static java.awt.Color.BLACK;
  36 import static java.awt.Color.BLUE;
  37 import static java.awt.Color.CYAN;
  38 import static java.awt.Color.GREEN;
  39 import static java.awt.Color.LIGHT_GRAY;
  40 import static java.awt.Color.MAGENTA;
  41 import static java.awt.Color.ORANGE;
  42 import static java.awt.Color.PINK;
  43 import static java.awt.Color.RED;
  44 import static java.awt.Color.WHITE;
  45 import static java.awt.Color.YELLOW;
  46 import java.awt.AlphaComposite;
  47 import java.awt.Color;
  48 import java.awt.Graphics2D;
  49 import java.awt.Image;
  50 import java.awt.Shape;
  51 import java.awt.font.TextLayout;
  52 import java.awt.geom.Ellipse2D;
  53 import java.awt.geom.Rectangle2D;
  54 import java.awt.geom.RoundRectangle2D;
  55 import java2d.Surface;
  56 
  57 
  58 /**
  59  * Compositing shapes on images.
  60  */
  61 @SuppressWarnings("serial")
  62 public class ACimages extends Surface {
  63 
  64     private static final String s[] = { "box", "fight", "magnify",
  65         "boxwave", "globe", "snooze",
  66         "tip", "thumbsup", "dukeplug" };
  67     private static Image imgs[] = new Image[s.length];
  68     private static Color colors[] = { BLUE, CYAN, GREEN,
  69         MAGENTA, ORANGE, PINK, RED, YELLOW, LIGHT_GRAY };
  70 
  71     public ACimages() {
  72         setBackground(WHITE);
  73         for (int i = 0; i < imgs.length; i++) {
  74             imgs[i] = getImage(s[i] + ".png");
  75         }
  76     }
  77 
  78     @Override
  79     public void render(int w, int h, Graphics2D g2) {
  80 
  81         float alpha = 0.0f;
  82         int iw = w / 3;
  83         int ih = (h - 45) / 3;
  84         float xx = 0, yy = 15;
  85 
  86         for (int i = 0; i < imgs.length; i++) {
  87 
  88             xx = (i % 3 == 0) ? 0 : xx + w / 3;
  89             switch (i) {
  90                 case 3:
  91                     yy = h / 3 + 15;
  92                     break;
  93                 case 6:
  94                     yy = h / 3 * 2 + 15;
  95             }
  96 
  97             g2.setComposite(AlphaComposite.SrcOver);
  98             g2.setColor(BLACK);
  99             AlphaComposite ac = AlphaComposite.SrcOver.derive(alpha += .1f);
 100             String str = "a=" + Float.toString(alpha).substring(0, 3);
 101             new TextLayout(str, g2.getFont(), g2.getFontRenderContext()).draw(g2, xx
 102                     + 3, yy - 2);
 103 
 104             Shape shape = null;
 105 
 106             switch (i % 3) {
 107                 case 0:
 108                     shape = new Ellipse2D.Float(xx, yy, iw, ih);
 109                     break;
 110                 case 1:
 111                     shape = new RoundRectangle2D.Float(xx, yy, iw, ih, 25, 25);
 112                     break;
 113                 case 2:
 114                     shape = new Rectangle2D.Float(xx, yy, iw, ih);
 115                     break;
 116             }
 117             g2.setColor(colors[i]);
 118             g2.setComposite(ac);
 119             g2.fill(shape);
 120             g2.drawImage(imgs[i], (int) xx, (int) yy, iw, ih, null);
 121         }
 122     }
 123 
 124     public static void main(String s[]) {
 125         createDemoFrame(new ACimages());
 126     }
 127 }