home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / JBuilder8.iso / Solaris / resource / jre / demo / jfc / Java2D / src / java2d / demos / Composite / FadeAnim.java < prev   
Encoding:
Java Source  |  2002-09-06  |  16.2 KB  |  441 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.  * @(#)FadeAnim.java    1.34 02/06/13
  38.  */
  39.  
  40. package java2d.demos.Composite;
  41.  
  42. import java.awt.*;
  43. import java.awt.geom.*;
  44. import java.awt.image.*;
  45. import java.awt.event.*;
  46. import java.util.Vector;
  47. import javax.swing.*;
  48. import javax.swing.event.*;
  49. import javax.swing.border.*;
  50. import java2d.AnimatingControlsSurface;
  51. import java2d.CustomControls;
  52.  
  53.  
  54.  
  55. /**
  56.  * Animation of compositing shapes, text and images fading in and out.
  57.  */
  58. public class FadeAnim extends AnimatingControlsSurface {
  59.  
  60.     private static TexturePaint texture;
  61.     static {
  62.         int w = 10; int h = 10;
  63.         BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
  64.         Graphics2D gi = bi.createGraphics();
  65.         Color oc = Color.blue; Color ic = Color.green;
  66.         gi.setPaint(new GradientPaint(0,0,oc,w*.35f,h*.35f,ic));
  67.         gi.fillRect(0, 0, w/2, h/2);
  68.         gi.setPaint(new GradientPaint(w,0,oc,w*.65f,h*.35f,ic));
  69.         gi.fillRect(w/2, 0, w/2, h/2);
  70.         gi.setPaint(new GradientPaint(0,h,oc,w*.35f,h*.65f,ic));
  71.         gi.fillRect(0, h/2, w/2, h/2);
  72.         gi.setPaint(new GradientPaint(w,h,oc,w*.65f,h*.65f,ic));
  73.         gi.fillRect(w/2, h/2, w/2, h/2);
  74.         texture = new TexturePaint(bi,new Rectangle(0,0,w,h));
  75.     }
  76.     private static BasicStroke bs = new BasicStroke(6); 
  77.     private static Font fonts[] = {
  78.                 new Font("Times New Roman", Font.PLAIN, 64),
  79.                 new Font("serif", Font.BOLD + Font.ITALIC, 24),
  80.                 new Font("Courier", Font.BOLD, 36),
  81.                 new Font("Arial", Font.BOLD + Font.ITALIC, 48),
  82.                 new Font("Helvetica", Font.PLAIN, 52)};
  83.     private static String strings[] = {
  84.                 "Alpha", "Composite", "Src", "SrcOver", 
  85.                 "SrcIn", "SrcOut", "Clear", "DstOver", "DstIn" };
  86.     private static String imgs[] = { 
  87.                 "jumptojavastrip.png", "duke.gif", "star7.gif" };
  88.     private static Paint paints[] = { 
  89.                 Color.red, Color.blue, Color.green, Color.magenta, 
  90.                 Color.orange, Color.pink, Color.cyan, texture,
  91.                 Color.yellow, Color.lightGray, Color.white};
  92.     private Vector vector = new Vector(20);
  93.     private int numShapes, numStrings, numImages;
  94.  
  95.  
  96.     public FadeAnim() {
  97.         setBackground(Color.black);
  98.         setStrings(2);
  99.         setImages(3);
  100.         setShapes(8);
  101.         setControls(new Component[] { new DemoControls(this) });
  102.         setConstraints(new String[] { BorderLayout.EAST });
  103.     }
  104.  
  105.  
  106.     public void setImages(int num) {
  107.  
  108.         if (num < numImages) {
  109.             Vector v = new Vector(vector.size());
  110.             for (int i = 0; i < vector.size(); i++) {
  111.                 if (((ObjectData) vector.get(i)).object instanceof Image) {
  112.                     v.addElement(vector.get(i));
  113.                 }
  114.             }
  115.             vector.removeAll(v);
  116.             v.setSize(num);
  117.             vector.addAll(v);
  118.         } else {
  119.             Dimension d = getSize();
  120.             for (int i = numImages; i < num; i++) {
  121.                 Object obj = getImage(imgs[i % imgs.length]);
  122.                 if (imgs[i % imgs.length].equals("jumptojavastrip.png")) {
  123.                     int iw = ((Image) obj).getWidth(null);
  124.                     int ih = ((Image) obj).getHeight(null);
  125.                     BufferedImage bimg = new BufferedImage(iw, ih, BufferedImage.TYPE_INT_RGB);
  126.                     bimg.createGraphics().drawImage((Image) obj, 0, 0, null);
  127.                     obj = bimg;
  128.                 }
  129.                 ObjectData od = new ObjectData(obj, Color.black);
  130.                 od.reset(d.width, d.height);
  131.                 vector.addElement(od);
  132.             }
  133.         }
  134.         numImages = num;
  135.     }
  136.         
  137.  
  138.     public void setStrings(int num) {
  139.  
  140.         if (num < numStrings) {
  141.             Vector v = new Vector(vector.size());
  142.             for (int i = 0; i < vector.size(); i++) {
  143.                 if (((ObjectData) vector.get(i)).object instanceof TextData) {
  144.                     v.addElement(vector.get(i));
  145.                 }
  146.             }
  147.             vector.removeAll(v);
  148.             v.setSize(num);
  149.             vector.addAll(v);
  150.         } else {
  151.             Dimension d = getSize();
  152.             for (int i = numStrings; i < num; i++) {
  153.                 int j = i % fonts.length;
  154.                 int k = i % strings.length;
  155.                 Object obj = new TextData(strings[k], fonts[j], this); 
  156.                 ObjectData od = new ObjectData(obj, paints[i%paints.length]);
  157.                 od.reset(d.width, d.height);
  158.                 vector.addElement(od);
  159.             }
  160.         }
  161.         numStrings = num;
  162.     }
  163.         
  164.  
  165.     public void setShapes(int num) {
  166.  
  167.         if (num < numShapes) {
  168.             Vector v = new Vector(vector.size());
  169.             for (int i = 0; i < vector.size(); i++) {
  170.                 if (((ObjectData) vector.get(i)).object instanceof Shape) {
  171.                     v.addElement(vector.get(i));
  172.                 }
  173.             }
  174.             vector.removeAll(v);
  175.             v.setSize(num);
  176.             vector.addAll(v);
  177.         } else {
  178.             Dimension d = getSize();
  179.             for (int i = numShapes; i < num; i++) {
  180.                 Object obj = null;
  181.                 switch (i % 7) {
  182.                     case 0 : obj = new GeneralPath(); break;
  183.                     case 1 : obj = new Rectangle2D.Double(); break;
  184.                     case 2 : obj = new Ellipse2D.Double(); break;
  185.                     case 3 : obj = new Arc2D.Double(); break;
  186.                     case 4 : obj = new RoundRectangle2D.Double(); break;
  187.                     case 5 : obj = new CubicCurve2D.Double(); break;
  188.                     case 6 : obj = new QuadCurve2D.Double(); break;
  189.                 }
  190.                 ObjectData od = new ObjectData(obj, paints[i%paints.length]);
  191.                 od.reset(d.width, d.height);
  192.                 vector.addElement(od);
  193.             }
  194.         } 
  195.         numShapes = num;
  196.     }
  197.  
  198.  
  199.     public void reset(int w, int h) {
  200.         for (int i = 0; i < vector.size(); i++) {
  201.             ((ObjectData) vector.get(i)).reset(w, h);
  202.         }
  203.     }
  204.  
  205.  
  206.     public void step(int w, int h) {
  207.         for (int i = 0; i < vector.size(); i++) {
  208.             ((ObjectData) vector.get(i)).step(w, h);
  209.         }
  210.     }
  211.  
  212.  
  213.     public void render(int w, int h, Graphics2D g2) {
  214.         for (int i = 0; i < vector.size(); i++) {
  215.             ObjectData od = (ObjectData) vector.get(i);
  216.             AlphaComposite ac = AlphaComposite.getInstance(
  217.                                    AlphaComposite.SRC_OVER, od.alpha);
  218.             g2.setComposite(ac);
  219.             g2.setPaint(od.paint);
  220.             g2.translate(od.x, od.y);
  221.  
  222.             if (od.object instanceof Image) {
  223.                 g2.drawImage((Image) od.object, 0, 0, this);
  224.             } else if (od.object instanceof TextData) {
  225.                 g2.setFont(((TextData) od.object).font);
  226.                 g2.drawString(((TextData) od.object).string, 0, 0);
  227.             } else if (od.object instanceof QuadCurve2D 
  228.                     || od.object instanceof CubicCurve2D) 
  229.             {
  230.                 g2.setStroke(bs);
  231.                 g2.draw((Shape) od.object);
  232.             } else if (od.object instanceof Shape) {
  233.                 g2.fill((Shape) od.object);
  234.             }
  235.             g2.translate(-od.x, -od.y);
  236.         }
  237.     }
  238.  
  239.  
  240.     public static void main(String argv[]) {
  241.         createDemoFrame(new FadeAnim());
  242.     }
  243.  
  244.  
  245.     static class TextData extends Object {
  246.  
  247.     public String string;
  248.         public Font font;
  249.         public int width, height;
  250.  
  251.         public TextData(String str, Font font, Component cmp) {
  252.             string = str;
  253.             this.font = font;
  254.             FontMetrics fm = cmp.getFontMetrics(font);
  255.             width = fm.stringWidth(str);
  256.             height = fm.getHeight();
  257.         }
  258.     }
  259.  
  260.  
  261.     static class ObjectData extends Object {
  262.         final int UP = 0;
  263.         final int DOWN = 1;
  264.         Object object;
  265.         BufferedImage bimg;
  266.         Paint paint;
  267.         double x, y;
  268.         float alpha;
  269.         int alphaDirection;
  270.         int imgX;
  271.  
  272.         public ObjectData(Object object, Paint paint) {
  273.             this.object = object;
  274.             this.paint = paint;
  275.             if (object instanceof BufferedImage) {
  276.                 bimg = (BufferedImage) object;
  277.                 this.object = bimg.getSubimage(0, 0, 80, 80);    
  278.             }
  279.             getRandomXY(300, 250);
  280.             alpha = (float) Math.random();
  281.             alphaDirection = Math.random() > 0.5 ? UP : DOWN;
  282.         }
  283.  
  284.  
  285.         private void getRandomXY(int w, int h) {
  286.             if (object instanceof TextData) {
  287.                 x = Math.random() * (w - ((TextData) object).width);
  288.                 y = Math.random() * h;
  289.                 y = y < ((TextData) object).height ? ((TextData) object).height : y;
  290.             } else if (object instanceof Image) {
  291.                 x = Math.random() * (w - ((Image) object).getWidth(null));
  292.                 y = Math.random() * (h - ((Image) object).getHeight(null));
  293.             } else if (object instanceof Shape) {
  294.                 Rectangle bounds = ((Shape) object).getBounds();
  295.                 x = Math.random() * (w - bounds.width);
  296.                 y = Math.random() * (h - bounds.height);
  297.             }
  298.         }
  299.  
  300.  
  301.         public void reset(int w, int h) {
  302.             getRandomXY(w, h);
  303.             double ww = 20 + Math.random()*((w == 0 ? 400 : w)/4);
  304.             double hh = 20 + Math.random()*((h == 0 ? 300 : h)/4);
  305.             if (object instanceof Ellipse2D) {
  306.                 ((Ellipse2D) object).setFrame(0, 0, ww, hh);
  307.             } else if (object instanceof Rectangle2D) {
  308.                 ((Rectangle2D) object).setRect(0, 0, ww, ww);
  309.             } else if (object instanceof RoundRectangle2D) {
  310.                 ((RoundRectangle2D) object).setRoundRect(0, 0, hh, hh, 20, 20); 
  311.             } else if (object instanceof Arc2D) {
  312.                 ((Arc2D) object).setArc(0, 0, hh, hh, 45, 270, Arc2D.PIE);
  313.             } else if (object instanceof QuadCurve2D) {
  314.                 ((QuadCurve2D) object).setCurve(0, 0, w*.2, h*.4, w*.4, 0);
  315.             } else if (object instanceof CubicCurve2D) {
  316.                     ((CubicCurve2D) object).setCurve(0,0,30,-60,60,60,90,0);
  317.             } else if (object instanceof GeneralPath) {
  318.                 GeneralPath p = new GeneralPath();
  319.                 float size = (float) ww;
  320.                 p.moveTo(- size / 2.0f, - size / 8.0f);
  321.                 p.lineTo(+ size / 2.0f, - size / 8.0f);
  322.                 p.lineTo(- size / 4.0f, + size / 2.0f);
  323.                 p.lineTo(+         0.0f, - size / 2.0f);
  324.                 p.lineTo(+ size / 4.0f, + size / 2.0f);
  325.                 p.closePath();
  326.                 object = p;
  327.             }
  328.         }
  329.  
  330.  
  331.         public void step(int w, int h) {
  332.             if (object instanceof BufferedImage) {
  333.                 if ((imgX += 80) == 800) {
  334.                     imgX = 0;
  335.                 }
  336.                 object = bimg.getSubimage(imgX, 0, 80, 80);    
  337.             }
  338.             if (alphaDirection == UP) {
  339.                 if ((alpha += 0.05) > .99) {
  340.                     alphaDirection = DOWN;
  341.                     alpha = 1.0f;
  342.                 }
  343.             } else if (alphaDirection == DOWN) {
  344.                 if ((alpha -= .05) < 0.01) {
  345.                     alphaDirection = UP;
  346.                     alpha = 0;
  347.                     getRandomXY(w, h);
  348.                 }
  349.             }
  350.         }
  351.     }
  352.  
  353.  
  354.     static class DemoControls extends CustomControls implements ChangeListener {
  355.  
  356.         FadeAnim demo;
  357.         JSlider shapeSlider, stringSlider, imageSlider;
  358.         Font font = new Font("serif", Font.PLAIN, 10);
  359.  
  360.         public DemoControls(FadeAnim demo) {
  361.             super(demo.name);
  362.             this.demo = demo;
  363.             setBackground(Color.gray);
  364.             setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
  365.             add(Box.createVerticalStrut(5));
  366.  
  367.             JToolBar toolbar = new JToolBar(JToolBar.VERTICAL);
  368.             toolbar.setBackground(Color.gray);
  369.             toolbar.setFloatable(false);
  370.             shapeSlider = new JSlider(JSlider.HORIZONTAL,0,20,demo.numShapes);
  371.             shapeSlider.addChangeListener(this);
  372.             TitledBorder tb = new TitledBorder(new EtchedBorder());
  373.             tb.setTitleFont(font);
  374.             tb.setTitle(String.valueOf(demo.numShapes) + " Shapes");
  375.             shapeSlider.setBorder(tb);
  376.             shapeSlider.setPreferredSize(new Dimension(80,45));
  377.             toolbar.addSeparator();
  378.             toolbar.add(shapeSlider);
  379.             toolbar.addSeparator();
  380.  
  381.             stringSlider = new JSlider(JSlider.HORIZONTAL,0,10,demo.numStrings);
  382.             stringSlider.addChangeListener(this);
  383.             tb = new TitledBorder(new EtchedBorder());
  384.             tb.setTitleFont(font);
  385.             tb.setTitle(String.valueOf(demo.numStrings) + " Strings");
  386.             stringSlider.setBorder(tb);
  387.             stringSlider.setPreferredSize(new Dimension(80,45));
  388.             toolbar.add(stringSlider);
  389.             toolbar.addSeparator();
  390.  
  391.             imageSlider = new JSlider(JSlider.HORIZONTAL,0,10,demo.numImages);
  392.             imageSlider.addChangeListener(this);
  393.             tb = new TitledBorder(new EtchedBorder());
  394.             tb.setTitleFont(font);
  395.             tb.setTitle(String.valueOf(demo.numImages) + " Images");
  396.             imageSlider.setBorder(tb);
  397.             imageSlider.setPreferredSize(new Dimension(80,45));
  398.             toolbar.add(imageSlider);
  399.             toolbar.addSeparator();
  400.  
  401.             add(toolbar);
  402.         }
  403.  
  404.  
  405.         public void stateChanged(ChangeEvent e) {
  406.             JSlider slider = (JSlider) e.getSource();
  407.             int value = slider.getValue();
  408.             TitledBorder tb = (TitledBorder) slider.getBorder();
  409.             if (slider.equals(shapeSlider)) {
  410.                 tb.setTitle(String.valueOf(value) + " Shapes");
  411.                 demo.setShapes(value);
  412.             } else if (slider.equals(stringSlider)) {
  413.                 tb.setTitle(String.valueOf(value) + " Strings");
  414.                 demo.setStrings(value);
  415.             } else if (slider.equals(imageSlider)) {
  416.                 tb.setTitle(String.valueOf(value) + " Images");
  417.                 demo.setImages(value);
  418.             } 
  419.             slider.repaint();
  420.             if (demo.animating.thread == null) {
  421.                 demo.repaint();
  422.             }
  423.         }
  424.  
  425.  
  426.         public Dimension getPreferredSize() {
  427.             return new Dimension(80,0);
  428.         }
  429.  
  430.  
  431.         public void run() {
  432.             try { 
  433.                thread.sleep(999);
  434.             } catch (InterruptedException e) { return; }
  435.             shapeSlider.setValue((int) (Math.random() * 5));
  436.             stringSlider.setValue(10);
  437.             thread = null;
  438.         }
  439.     } // End DemoControls
  440. } // End FadeAnim
  441.