ACrules.java
Upload User: cdlibang
Upload Date: 2016-07-17
Package Size: 774k
Code Size: 10k
Category:

2D Graphic

Development Platform:

Java

  1. /*
  2.  * @(#)ACrules.java 1.10 99/04/23
  3.  *
  4.  * Copyright (c) 1998, 1999 by Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
  7.  * modify and redistribute this software in source and binary code form,
  8.  * provided that i) this copyright notice and license appear on all copies of
  9.  * the software; and ii) Licensee does not utilize the software in a manner
  10.  * which is disparaging to Sun.
  11.  * 
  12.  * This software is provided "AS IS," without a warranty of any kind. ALL
  13.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
  14.  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
  15.  * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
  16.  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
  17.  * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
  18.  * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
  19.  * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
  20.  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
  21.  * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
  22.  * POSSIBILITY OF SUCH DAMAGES.
  23.  * 
  24.  * This software is not designed or intended for use in on-line control of
  25.  * aircraft, air traffic, aircraft navigation or aircraft communications; or in
  26.  * the design, construction, operation or maintenance of any nuclear
  27.  * facility. Licensee represents and warrants that it will not use or
  28.  * redistribute the Software for such purposes.
  29.  */
  30. package demos.Composite;
  31. import java.awt.*;
  32. import java.awt.event.*;
  33. import java.awt.geom.GeneralPath;
  34. import java.awt.image.BufferedImage;
  35. import java.awt.font.TextLayout;
  36. import java.awt.font.FontRenderContext;
  37. import java.awt.font.LineMetrics;
  38. import AnimatingContext;
  39. import DemoSurface;
  40. import DemoPanel;
  41. /**
  42.  * All the AlphaCompositing Rules demonstrated.
  43.  */
  44. public class ACrules extends DemoSurface implements AnimatingContext {
  45.     private static String compNames[] = {
  46.         "(Source)",
  47.         "Src",
  48.         "SrcOver",
  49.         "SrcIn",
  50.         "SrcOut",
  51.         "(Dest)",
  52.         "Clear",
  53.         "DstOver",
  54.         "DstIn",
  55.         "DstOut"
  56.     };
  57.     private static AlphaComposite compObjs[] = {
  58.         AlphaComposite.Src,
  59.         AlphaComposite.Src,
  60.         AlphaComposite.SrcOver,
  61.         AlphaComposite.SrcIn,
  62.         AlphaComposite.SrcOut,
  63.         AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.0f),
  64.         AlphaComposite.Clear,
  65.         AlphaComposite.DstOver,
  66.         AlphaComposite.DstIn,
  67.         AlphaComposite.DstOut
  68.     };
  69.     private int fadeIndex;
  70.     private static float fadeValues[][] = {
  71.         { 1.0f,-0.1f, 0.0f, 1.0f, 0.0f, 1.0f},
  72.         { 0.0f, 0.1f, 1.0f, 1.0f,-0.1f, 0.0f},
  73.         { 1.0f, 0.0f, 1.0f, 0.0f, 0.1f, 1.0f},
  74.     };
  75.     private static String fadeNames[] = {
  76.         "Src => transparent, Dest opaque",
  77.         "Src => opaque, Dest => transparent",
  78.         "Src opaque, Dest => opaque",
  79.     };
  80.     private static Font f = new Font("serif", Font.PLAIN, 10);
  81.     private float srca = fadeValues[fadeIndex][0];
  82.     private float dsta = fadeValues[fadeIndex][3];
  83.     private String fadeLabel = fadeNames[0];
  84.     private BufferedImage statBI, animBI;
  85.     private int PADLEFT, PADRIGHT, HPAD;
  86.     private int PADABOVE, PADBELOW, VPAD;
  87.     private int RECTWIDTH, RECTHEIGHT;
  88.     private int PADDEDWIDTH, PADDEDHEIGHT;
  89.     private GeneralPath srcpath = new GeneralPath();
  90.     private GeneralPath dstpath = new GeneralPath();
  91.     private LineMetrics lm;
  92.     private BufferedImage dBI, sBI;
  93.     private GradientPaint gradientDst, gradientSrc;
  94.     public ACrules() {
  95.         setBackground(Color.white);
  96.     }
  97.     public void reset(int w, int h) {
  98.         setSleepAmount(400);
  99.         FontRenderContext frc = new FontRenderContext(null, false, false);
  100.         lm = f.getLineMetrics(compNames[0], frc);
  101.         
  102.         PADLEFT  = (w < 150) ? 10 : 15;
  103.         PADRIGHT = (w < 150) ? 10 : 15;
  104.         HPAD     = (PADLEFT + PADRIGHT);
  105.         PADABOVE = 2 + (int) lm.getHeight();
  106.         PADBELOW = 2;
  107.         VPAD     = (PADABOVE + PADBELOW);
  108.         RECTWIDTH = w/4 - HPAD;
  109.         RECTWIDTH = (RECTWIDTH < 6) ? 6 : RECTWIDTH;
  110.         RECTHEIGHT = (h-VPAD)/5 - VPAD;
  111.         RECTHEIGHT = (RECTHEIGHT < 6) ? 6 : RECTHEIGHT;
  112.         PADDEDWIDTH  = (RECTWIDTH  + HPAD);
  113.         PADDEDHEIGHT = (RECTHEIGHT + VPAD);
  114.         
  115.         srcpath.reset();
  116.         srcpath.moveTo(0,0);
  117.         srcpath.lineTo(RECTWIDTH,0);
  118.         srcpath.lineTo(RECTWIDTH,RECTHEIGHT/2);
  119.         srcpath.lineTo(0,RECTHEIGHT);
  120.         srcpath.closePath();
  121.         
  122.         dstpath.reset();
  123.         dstpath.moveTo(0,0);
  124.         dstpath.lineTo(0,RECTHEIGHT/2);
  125.         dstpath.lineTo(RECTWIDTH,RECTHEIGHT);
  126.         dstpath.lineTo(RECTWIDTH,0);
  127.         dstpath.closePath();
  128.         
  129.         dBI = new BufferedImage(RECTWIDTH, RECTHEIGHT,
  130.                                     BufferedImage.TYPE_INT_ARGB);
  131.         sBI = new BufferedImage(RECTWIDTH, RECTHEIGHT,
  132.                                     BufferedImage.TYPE_INT_ARGB);
  133.         gradientDst = new GradientPaint(0, 0,
  134.                                       new Color(1.0f, 0.0f, 0.0f, 1.0f),
  135.                                       0, RECTHEIGHT,
  136.                                       new Color(1.0f, 0.0f, 0.0f, 0.0f));
  137.         gradientSrc = new GradientPaint(0, 0,
  138.                                       new Color(0.0f, 0.0f, 1.0f, 1.0f),
  139.                                       RECTWIDTH, 0,
  140.                                       new Color(0.0f, 0.0f, 1.0f, 0.0f));
  141.         statBI = new BufferedImage(w/2, h, BufferedImage.TYPE_INT_RGB);
  142.         statBI = drawCompBI(statBI, true);
  143.         animBI = new BufferedImage(w/2, h, BufferedImage.TYPE_INT_RGB);
  144.     }
  145.     public void step(int w, int h) {
  146.         if (getSleepAmount() == 5000) {
  147.             setSleepAmount(200);
  148.         }
  149.         srca = srca + fadeValues[fadeIndex][1];
  150.         dsta = dsta + fadeValues[fadeIndex][4];
  151.         fadeLabel = fadeNames[fadeIndex];
  152.         if (srca < 0 || srca > 1.0 || dsta < 0 || dsta > 1.0) {
  153.             setSleepAmount(5000);
  154.             srca = fadeValues[fadeIndex][2];
  155.             dsta = fadeValues[fadeIndex][5];
  156.             if (fadeIndex++ == fadeValues.length-1) {
  157.                 fadeIndex = 0;
  158.             }
  159.         }
  160.     }
  161.     public void drawDemo(int w, int h, Graphics2D g2) {
  162.         if (statBI == null || animBI == null) {
  163.             return;
  164.         }
  165.         g2.drawImage(statBI, 0, 0, null);
  166.         g2.drawImage(drawCompBI(animBI, false), w/2, 0, null);
  167.         g2.setColor(Color.black);
  168.         FontRenderContext frc = g2.getFontRenderContext();
  169.         TextLayout tl = new TextLayout("AC Rules", g2.getFont(), frc);
  170.         tl.draw(g2, 15.0f, (float) tl.getBounds().getHeight()+3.0f);
  171.         tl = new TextLayout(fadeLabel, f, frc);
  172.         float x = (float) (w*0.75-tl.getBounds().getWidth()/2);
  173.         if ((x + tl.getBounds().getWidth()) > w) {
  174.             x = (float) (w - tl.getBounds().getWidth());
  175.         }
  176.         tl.draw(g2, x, (float) tl.getBounds().getHeight()+3.0f);
  177.     }
  178.     private BufferedImage drawCompBI(BufferedImage bi, boolean doGradient)
  179.     {
  180.         Graphics2D big = bi.createGraphics();
  181.         big.setColor(getBackground());
  182.         big.fillRect(0, 0, bi.getWidth(), bi.getHeight());
  183.         big.setRenderingHint(RenderingHints.KEY_ANTIALIASING, AntiAlias);
  184.         big.setFont(f);
  185.         Graphics2D gD = dBI.createGraphics();
  186.         gD.setRenderingHint(RenderingHints.KEY_ANTIALIASING, AntiAlias);
  187.         Graphics2D gS = sBI.createGraphics();
  188.         gS.setRenderingHint(RenderingHints.KEY_ANTIALIASING, AntiAlias);
  189.         int x = 0, y = 0;
  190.         int yy = (int) lm.getHeight() + VPAD;
  191.         for (int i = 0; i < compNames.length; i++) {
  192.             y = (i == 0 || i == 5) ? yy : y + PADDEDHEIGHT;
  193.             x = (i >= 5) ? bi.getWidth()/2+PADLEFT : PADLEFT;
  194.             big.translate(x, y);
  195.             gD.setComposite(AlphaComposite.Clear);
  196.             gD.fillRect(0, 0, RECTWIDTH, RECTHEIGHT);
  197.             gD.setComposite(AlphaComposite.Src);
  198.             if (doGradient) {
  199.                 gD.setPaint(gradientDst);
  200.                 gD.fillRect(0, 0, RECTWIDTH, RECTHEIGHT);
  201.             } else {
  202.                 gD.setPaint(new Color(1.0f, 0.0f, 0.0f, dsta));
  203.                 gD.fill(dstpath);
  204.             }
  205.             gS.setComposite(AlphaComposite.Clear);
  206.             gS.fillRect(0, 0, RECTWIDTH, RECTHEIGHT);
  207.             gS.setComposite(AlphaComposite.Src);
  208.             if (doGradient) {
  209.                 gS.setPaint(gradientSrc);
  210.                 gS.fillRect(0, 0, RECTWIDTH, RECTHEIGHT);
  211.             } else {
  212.                 gS.setPaint(new Color(0.0f, 0.0f, 1.0f, srca));
  213.                 gS.fill(srcpath);
  214.             }
  215.             gD.setComposite(compObjs[i]);
  216.             gD.drawImage(sBI, 0, 0, null);
  217.             big.drawImage(dBI, 0, 0, null);
  218.             big.setColor(Color.black);
  219.             big.drawString(compNames[i], 0, -lm.getDescent());
  220.             big.drawRect(0, 0, RECTWIDTH, RECTHEIGHT);
  221.             big.translate(-x, -y);
  222.         }
  223.         gD.dispose();
  224.         gS.dispose();
  225.         big.dispose();
  226.         return bi;
  227.     }
  228.     public static void main(String argv[]) {
  229.         final DemoPanel dp = new DemoPanel(new ACrules());
  230.         Frame f = new Frame("Java2D Demo - ACrules");
  231.         f.addWindowListener(new WindowAdapter() {
  232.             public void windowClosing(WindowEvent e) {System.exit(0);}
  233.             public void windowDeiconified(WindowEvent e) { 
  234.                 dp.surface.start(); 
  235.             }
  236.             public void windowIconified(WindowEvent e) { 
  237.                 dp.surface.stop(); 
  238.             }
  239.         });
  240.         f.add("Center", dp);
  241.         f.pack();
  242.         f.setSize(new Dimension(400,300));
  243.         f.show();
  244.         dp.surface.start();
  245.     }
  246. }