home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / JBuilder8.iso / Solaris / resource / jre / demo / jfc / Java2D / src / java2d / demos / Composite / ACrules.java < prev    next >
Encoding:
Java Source  |  2002-09-06  |  9.4 KB  |  277 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.  * @(#)ACrules.java    1.16 02/06/13
  38.  */
  39.  
  40. package java2d.demos.Composite;
  41.  
  42. import java.awt.*;
  43. import java.awt.geom.GeneralPath;
  44. import java.awt.image.BufferedImage;
  45. import java.awt.font.TextLayout;
  46. import java.awt.font.FontRenderContext;
  47. import java.awt.font.LineMetrics;
  48. import java2d.AnimatingSurface;
  49.  
  50.  
  51. /**
  52.  * All the AlphaCompositing Rules demonstrated.
  53.  */
  54. public class ACrules extends AnimatingSurface {
  55.  
  56.     private static String compNames[] = {
  57.         "Src",
  58.         "SrcOver",
  59.         "SrcIn",
  60.         "SrcOut",
  61.         "SrcAtop",
  62.         "Clear",
  63.  
  64.         "Dst",
  65.         "DstOver",
  66.         "DstIn",
  67.         "DstOut",
  68.         "DstAtop",
  69.     "Xor",
  70.     };
  71.  
  72.     private static AlphaComposite compObjs[] = {
  73.         AlphaComposite.Src,
  74.         AlphaComposite.SrcOver,
  75.         AlphaComposite.SrcIn,
  76.         AlphaComposite.SrcOut,
  77.         AlphaComposite.SrcAtop,
  78.         AlphaComposite.Clear,
  79.  
  80.         AlphaComposite.Dst,
  81.         AlphaComposite.DstOver,
  82.         AlphaComposite.DstIn,
  83.         AlphaComposite.DstOut,
  84.         AlphaComposite.DstAtop,
  85.         AlphaComposite.Xor,
  86.     };
  87.  
  88.     private static int NUM_RULES = compObjs.length;
  89.     private static int HALF_NUM_RULES = NUM_RULES / 2;
  90.  
  91.     private int fadeIndex;
  92.     private static float fadeValues[][] = {
  93.         { 1.0f,-0.1f, 0.0f, 1.0f, 0.0f, 1.0f},
  94.         { 0.0f, 0.1f, 1.0f, 1.0f,-0.1f, 0.0f},
  95.         { 1.0f, 0.0f, 1.0f, 0.0f, 0.1f, 1.0f},
  96.     };
  97.     private static String fadeNames[] = {
  98.         "Src => transparent, Dest opaque",
  99.         "Src => opaque, Dest => transparent",
  100.         "Src opaque, Dest => opaque",
  101.     };
  102.     private static Font f = new Font("serif", Font.PLAIN, 10);
  103.     private float srca = fadeValues[fadeIndex][0];
  104.     private float dsta = fadeValues[fadeIndex][3];
  105.     private String fadeLabel = fadeNames[0];
  106.     private BufferedImage statBI, animBI;
  107.     private int PADLEFT, PADRIGHT, HPAD;
  108.     private int PADABOVE, PADBELOW, VPAD;
  109.     private int RECTWIDTH, RECTHEIGHT;
  110.     private int PADDEDWIDTH, PADDEDHEIGHT;
  111.     private GeneralPath srcpath = new GeneralPath();
  112.     private GeneralPath dstpath = new GeneralPath();
  113.     private LineMetrics lm;
  114.     private BufferedImage dBI, sBI;
  115.     private GradientPaint gradientDst, gradientSrc;
  116.  
  117.  
  118.     public ACrules() {
  119.         setBackground(Color.white);
  120.     }
  121.  
  122.  
  123.     public void reset(int w, int h) {
  124.         setSleepAmount(400);
  125.         FontRenderContext frc = new FontRenderContext(null, false, false);
  126.         lm = f.getLineMetrics(compNames[0], frc);
  127.         
  128.         PADLEFT  = (w < 150) ? 10 : 15;
  129.         PADRIGHT = (w < 150) ? 10 : 15;
  130.         HPAD     = (PADLEFT + PADRIGHT);
  131.         PADBELOW = (h < 250) ? 1 : 2;
  132.         PADABOVE = PADBELOW + (int) lm.getHeight();
  133.         VPAD     = (PADABOVE + PADBELOW);
  134.         RECTWIDTH = w/4 - HPAD;
  135.         RECTWIDTH = (RECTWIDTH < 6) ? 6 : RECTWIDTH;
  136.         RECTHEIGHT = (h-VPAD)/HALF_NUM_RULES - VPAD;
  137.         RECTHEIGHT = (RECTHEIGHT < 6) ? 6 : RECTHEIGHT;
  138.         PADDEDWIDTH  = (RECTWIDTH  + HPAD);
  139.         PADDEDHEIGHT = (RECTHEIGHT + VPAD);
  140.         
  141.         srcpath.reset();
  142.         srcpath.moveTo(0,0);
  143.         srcpath.lineTo(RECTWIDTH,0);
  144.         srcpath.lineTo(0,RECTHEIGHT);
  145.         srcpath.closePath();
  146.         
  147.         dstpath.reset();
  148.         dstpath.moveTo(0,0);
  149.         dstpath.lineTo(RECTWIDTH,RECTHEIGHT);
  150.         dstpath.lineTo(RECTWIDTH,0);
  151.         dstpath.closePath();
  152.         
  153.         dBI = new BufferedImage(RECTWIDTH, RECTHEIGHT,
  154.                                     BufferedImage.TYPE_INT_ARGB);
  155.         sBI = new BufferedImage(RECTWIDTH, RECTHEIGHT,
  156.                                     BufferedImage.TYPE_INT_ARGB);
  157.         gradientDst = new GradientPaint(0, 0,
  158.                                       new Color(1.0f, 0.0f, 0.0f, 1.0f),
  159.                                       0, RECTHEIGHT,
  160.                                       new Color(1.0f, 0.0f, 0.0f, 0.0f));
  161.         gradientSrc = new GradientPaint(0, 0,
  162.                                       new Color(0.0f, 0.0f, 1.0f, 1.0f),
  163.                                       RECTWIDTH, 0,
  164.                                       new Color(0.0f, 0.0f, 1.0f, 0.0f));
  165.         statBI = new BufferedImage(w/2, h, BufferedImage.TYPE_INT_RGB);
  166.         statBI = drawCompBI(statBI, true);
  167.         animBI = new BufferedImage(w/2, h, BufferedImage.TYPE_INT_RGB);
  168.     }
  169.  
  170.  
  171.     public void step(int w, int h) {
  172.         if (getSleepAmount() == 5000) {
  173.             setSleepAmount(200);
  174.         }
  175.  
  176.         srca = srca + fadeValues[fadeIndex][1];
  177.         dsta = dsta + fadeValues[fadeIndex][4];
  178.         fadeLabel = fadeNames[fadeIndex];
  179.         if (srca < 0 || srca > 1.0 || dsta < 0 || dsta > 1.0) {
  180.             setSleepAmount(5000);
  181.             srca = fadeValues[fadeIndex][2];
  182.             dsta = fadeValues[fadeIndex][5];
  183.             if (fadeIndex++ == fadeValues.length-1) {
  184.                 fadeIndex = 0;
  185.             }
  186.         }
  187.     }
  188.  
  189.  
  190.     public void render(int w, int h, Graphics2D g2) {
  191.  
  192.         if (statBI == null || animBI == null) {
  193.             return;
  194.         }
  195.         g2.drawImage(statBI, 0, 0, null);
  196.         g2.drawImage(drawCompBI(animBI, false), w/2, 0, null);
  197.  
  198.         g2.setColor(Color.black);
  199.         FontRenderContext frc = g2.getFontRenderContext();
  200.         TextLayout tl = new TextLayout("AC Rules", g2.getFont(), frc);
  201.         tl.draw(g2, 15.0f, (float) tl.getBounds().getHeight()+3.0f);
  202.  
  203.         tl = new TextLayout(fadeLabel, f, frc);
  204.         float x = (float) (w*0.75-tl.getBounds().getWidth()/2);
  205.         if ((x + tl.getBounds().getWidth()) > w) {
  206.             x = (float) (w - tl.getBounds().getWidth());
  207.         }
  208.         tl.draw(g2, x, (float) tl.getBounds().getHeight()+3.0f);
  209.     }
  210.  
  211.  
  212.     private BufferedImage drawCompBI(BufferedImage bi, boolean doGradient)
  213.     {
  214.         Graphics2D big = bi.createGraphics();
  215.         big.setColor(getBackground());
  216.         big.fillRect(0, 0, bi.getWidth(), bi.getHeight());
  217.         big.setRenderingHint(RenderingHints.KEY_ANTIALIASING, AntiAlias);
  218.         big.setFont(f);
  219.  
  220.         Graphics2D gD = dBI.createGraphics();
  221.         gD.setRenderingHint(RenderingHints.KEY_ANTIALIASING, AntiAlias);
  222.         Graphics2D gS = sBI.createGraphics();
  223.         gS.setRenderingHint(RenderingHints.KEY_ANTIALIASING, AntiAlias);
  224.  
  225.         int x = 0, y = 0;
  226.         int yy = (int) lm.getHeight() + VPAD;
  227.  
  228.         for (int i = 0; i < compNames.length; i++) {
  229.             y = (i == 0 || i == HALF_NUM_RULES) ? yy : y + PADDEDHEIGHT;
  230.             x = (i >= HALF_NUM_RULES) ? bi.getWidth()/2+PADLEFT : PADLEFT;
  231.             big.translate(x, y);
  232.  
  233.             gD.setComposite(AlphaComposite.Clear);
  234.             gD.fillRect(0, 0, RECTWIDTH, RECTHEIGHT);
  235.             gD.setComposite(AlphaComposite.Src);
  236.             if (doGradient) {
  237.                 gD.setPaint(gradientDst);
  238.                 gD.fillRect(0, 0, RECTWIDTH, RECTHEIGHT);
  239.             } else {
  240.                 gD.setPaint(new Color(1.0f, 0.0f, 0.0f, dsta));
  241.                 gD.fill(dstpath);
  242.             }
  243.  
  244.             gS.setComposite(AlphaComposite.Clear);
  245.             gS.fillRect(0, 0, RECTWIDTH, RECTHEIGHT);
  246.             gS.setComposite(AlphaComposite.Src);
  247.             if (doGradient) {
  248.                 gS.setPaint(gradientSrc);
  249.                 gS.fillRect(0, 0, RECTWIDTH, RECTHEIGHT);
  250.             } else {
  251.                 gS.setPaint(new Color(0.0f, 0.0f, 1.0f, srca));
  252.                 gS.fill(srcpath);
  253.             }
  254.  
  255.             gD.setComposite(compObjs[i]);
  256.             gD.drawImage(sBI, 0, 0, null);
  257.  
  258.             big.drawImage(dBI, 0, 0, null);
  259.             big.setColor(Color.black);
  260.             big.drawString(compNames[i], 0, -lm.getDescent());
  261.             big.drawRect(0, 0, RECTWIDTH, RECTHEIGHT);
  262.             big.translate(-x, -y);
  263.         }
  264.  
  265.         gD.dispose();
  266.         gS.dispose();
  267.         big.dispose();
  268.  
  269.         return bi;
  270.     }
  271.  
  272.  
  273.     public static void main(String argv[]) {
  274.         createDemoFrame(new ACrules());
  275.     }
  276. }
  277.