home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / JBuilder8.iso / Solaris / resource / jre / demo / jfc / Java2D / src / java2d / demos / Composite / ACimages.java next >
Encoding:
Java Source  |  2002-09-06  |  4.1 KB  |  116 lines

  1. /*
  2.  * Copyright (c) 2002 Sun Microsystems, Inc. All  Rights Reserved.
  3.  * 
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  * 
  8.  * -Redistributions of source code must retain the above copyright
  9.  *  notice, this list of conditions and the following disclaimer.
  10.  * 
  11.  * -Redistribution in binary form must reproduct the above copyright
  12.  *  notice, this list of conditions and the following disclaimer in
  13.  *  the documentation and/or other materials provided with the distribution.
  14.  * 
  15.  * Neither the name of Sun Microsystems, Inc. or the names of contributors
  16.  * may be used to endorse or promote products derived from this software
  17.  * without specific prior written permission.
  18.  * 
  19.  * This software is provided "AS IS," without a warranty of any kind. ALL
  20.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
  21.  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
  22.  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT
  23.  * BE LIABLE FOR ANY DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT
  24.  * OF OR RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THE SOFTWARE OR ITS
  25.  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
  26.  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
  27.  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
  28.  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN
  29.  * IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  30.  * 
  31.  * You acknowledge that Software is not designed, licensed or intended for
  32.  * use in the design, construction, operation or maintenance of any nuclear
  33.  * facility.
  34.  */
  35.  
  36. /*
  37.  * @(#)ACimages.java    1.22 02/06/13
  38.  */
  39.  
  40. package java2d.demos.Composite;
  41.  
  42. import java.awt.*;
  43. import java.awt.geom.Ellipse2D;
  44. import java.awt.geom.Rectangle2D;
  45. import java.awt.geom.RoundRectangle2D;
  46. import java.awt.font.TextLayout;
  47. import java.awt.font.FontRenderContext;
  48. import java2d.Surface;
  49.  
  50.  
  51. /**
  52.  * Compositing shapes on images.
  53.  */
  54. public class ACimages extends Surface {
  55.  
  56.     private static String s[] = { "box", "fight", "magnify",
  57.                         "boxwave", "globe", "snooze",
  58.                         "tip", "thumbsup", "dukeplug"};
  59.     private static Image imgs[] = new Image[s.length];
  60.     private static Color colors[] = { Color.blue, Color.cyan, Color.green,
  61.                         Color.magenta, Color.orange, Color.pink,
  62.                         Color.red, Color.yellow, Color.lightGray };
  63.  
  64.  
  65.     public ACimages() {
  66.         setBackground(Color.white);
  67.         for (int i = 0; i < imgs.length; i++) {
  68.             imgs[i] = getImage(s[i] + ".gif");
  69.         }
  70.     }
  71.  
  72.  
  73.     public void render(int w, int h, Graphics2D g2) {
  74.  
  75.         float alpha = 0.0f;
  76.         int iw = w/3;
  77.         int ih = (h-45)/3;
  78.         float xx = 0, yy = 15;
  79.  
  80.         for (int i =0; i < imgs.length; i++) {
  81.  
  82.             xx = (i%3 == 0) ? 0 : xx+w/3;
  83.             switch (i) {
  84.                 case 3 : yy = h/3+15; break;
  85.                 case 6 : yy = h/3*2+15;
  86.             }
  87.  
  88.             g2.setComposite(AlphaComposite.SrcOver);
  89.             g2.setColor(Color.black);
  90.             AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha += .1f);
  91.             String s = "a=" + Float.toString(alpha).substring(0,3);
  92.             new TextLayout(s,g2.getFont(), g2.getFontRenderContext()).draw(g2, xx+3, yy-2);
  93.  
  94.             Shape shape=null;
  95.  
  96.             switch (i%3) {
  97.                 case 0 : shape = new Ellipse2D.Float(xx, yy, iw, ih);
  98.                         break;
  99.                 case 1 : shape = new RoundRectangle2D.Float(xx, yy, iw, ih, 25, 25);
  100.                         break;
  101.                 case 2 : shape = new Rectangle2D.Float(xx, yy, iw, ih);
  102.                         break;
  103.             }
  104.             g2.setColor(colors[i]);
  105.             g2.setComposite(ac);
  106.             g2.fill(shape);
  107.             g2.drawImage(imgs[i], (int) xx, (int) yy, iw, ih, null);
  108.         }
  109.     }
  110.  
  111.  
  112.     public static void main(String s[]) {
  113.         createDemoFrame(new ACimages());
  114.     }
  115. }
  116.