home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / JBuilder8.iso / Solaris / resource / jre / demo / jfc / Java2D / src / java2d / demos / Clipping / ClipAnim.java < prev    next >
Encoding:
Java Source  |  2002-09-06  |  9.5 KB  |  296 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.  * @(#)ClipAnim.java    1.21 02/06/13
  38.  */
  39.  
  40. package java2d.demos.Clipping;
  41.  
  42. import java.awt.*;
  43. import java.awt.event.*;
  44. import java.awt.geom.*;
  45. import java.awt.image.BufferedImage;
  46. import javax.swing.*;
  47. import java2d.AnimatingControlsSurface;
  48. import java2d.CustomControls;
  49.  
  50.  
  51. /**
  52.  * Animated clipping of an image & composited shapes.
  53.  */
  54. public class ClipAnim extends AnimatingControlsSurface {
  55.  
  56.     private static Image dimg, cimg;
  57.     private static Color redBlend = new Color(255, 0, 0, 120);
  58.     private static Color greenBlend = new Color(0, 255, 0, 120);
  59.     private static BasicStroke bs = new BasicStroke(20.0f);
  60.     static TexturePaint texture;
  61.     static {
  62.         BufferedImage bi = new BufferedImage(5, 5, BufferedImage.TYPE_INT_RGB);
  63.         Graphics2D big = bi.createGraphics();
  64.         big.setBackground(Color.yellow);
  65.         big.clearRect(0,0,5,5);
  66.         big.setColor(Color.red);
  67.         big.fillRect(0,0,3,3);
  68.         texture = new TexturePaint(bi,new Rectangle(0,0,5,5));
  69.     }
  70.     private AnimVal animval[] = new AnimVal[3]; 
  71.     protected boolean doObjects = true;
  72.     private Font originalFont = new Font("serif", Font.PLAIN, 12);
  73.     private Font font;
  74.     private GradientPaint gradient;
  75.     private int strX, strY;
  76.     private int dukeX, dukeY;
  77.  
  78.  
  79.     public ClipAnim() {
  80.         cimg = getImage("clouds.jpg");
  81.         dimg = getImage("duke.gif");
  82.         setBackground(Color.white);
  83.         animval[0] = new AnimVal(true);
  84.         animval[1] = new AnimVal(false);
  85.         animval[2] = new AnimVal(false);
  86.         setControls(new Component[] { new DemoControls(this) });
  87.     }
  88.  
  89.  
  90.     public void reset(int w, int h) {
  91.         for (int i = 0; i < animval.length; i++) {
  92.             animval[i].reset(w, h);
  93.         }
  94.         gradient = new GradientPaint(0,h/2,Color.red,w*.4f,h*.9f,Color.yellow);
  95.         dukeX = (int) (w*.25-dimg.getWidth(this)/2);
  96.         dukeY = (int) (h*.25-dimg.getHeight(this)/2);
  97.         FontMetrics fm = getFontMetrics(originalFont);
  98.         double sw = fm.stringWidth("CLIPPING");
  99.         double sh = fm.getAscent()+fm.getDescent();
  100.         double sx = (w/2-30)/sw;
  101.         double sy = (h/2-30)/sh;
  102.         AffineTransform Tx = AffineTransform.getScaleInstance(sx, sy);
  103.         font = originalFont.deriveFont(Tx);
  104.         fm = getFontMetrics(font);
  105.         strX = (int) (w*.75 - fm.stringWidth("CLIPPING")/2);
  106.         strY = (int) (h*.72 + fm.getAscent()/2);
  107.     }
  108.  
  109.  
  110.     public void step(int w, int h) {
  111.         for (int i = 0; i < animval.length; i++) {
  112.             if (animval[i].isSelected) {
  113.                 animval[i].step(w, h);
  114.             }
  115.         }
  116.     }
  117.  
  118.  
  119.     public void render(int w, int h, Graphics2D g2) {
  120.  
  121.         GeneralPath p1 = new GeneralPath();
  122.         GeneralPath p2 = new GeneralPath();
  123.  
  124.         for (int i = 0; i < animval.length; i++) {
  125.             if (animval[i].isSelected) {
  126.                 double x = animval[i].x; double y = animval[i].y;
  127.                 double ew = animval[i].ew; double eh = animval[i].eh;
  128.                 p1.append(new Ellipse2D.Double(x, y, ew, eh), false);
  129.                 p2.append(new Rectangle2D.Double(x+5, y+5, ew-10, eh-10),false);
  130.             }
  131.         }
  132.         if (animval[0].isSelected || animval[1].isSelected || 
  133.             animval[2].isSelected) 
  134.         {
  135.             g2.setClip(p1);
  136.             g2.clip(p2);
  137.         }
  138.  
  139.         if (doObjects) {
  140.             int w2 = w/2;
  141.             int h2 = h/2;
  142.             g2.drawImage(cimg, 0, 0, w2, h2, null);
  143.             g2.drawImage(dimg, dukeX, dukeY, null);
  144.  
  145.             g2.setPaint(texture);
  146.             g2.fillRect(w2, 0, w2, h2);
  147.  
  148.             g2.setPaint(gradient);
  149.             g2.fillRect(0, h2, w2, h2);
  150.  
  151.             g2.setColor(Color.lightGray);
  152.             g2.fillRect(w2, h2, w2, h2);
  153.             g2.setColor(Color.red);
  154.             g2.drawOval(w2, h2, w2-1, h2-1);
  155.             g2.setFont(font);
  156.             g2.drawString("CLIPPING", strX, strY);
  157.         } else {
  158.             g2.setColor(Color.lightGray);
  159.             g2.fillRect(0, 0, w, h);
  160.         }
  161.     }
  162.  
  163.  
  164.     public static void main(String argv[]) {
  165.         createDemoFrame(new ClipAnim());
  166.     }
  167.  
  168.  
  169.     public class AnimVal {
  170.         double ix = 5.0;
  171.         double iy = 3.0;
  172.         double iw = 5.0;
  173.         double ih = 3.0;
  174.         double x, y;
  175.         double ew, eh;   // ellipse width & height
  176.         boolean isSelected;
  177.  
  178.         public AnimVal(boolean isSelected) {
  179.             this.isSelected = isSelected;
  180.         }
  181.  
  182.  
  183.         public void step(int w, int h) {
  184.             x += ix;
  185.             y += iy;
  186.             ew += iw;
  187.             eh += ih;
  188.             if (ew > w/2) {
  189.                 ew = w/2;
  190.                 iw = Math.random() * -w/16 - 1;
  191.             }
  192.             if (ew < w/8) {
  193.                 ew = w/8;
  194.                 iw = Math.random() * w/16 + 1;
  195.             }
  196.             if (eh > h/2) {
  197.                 eh = h/2;
  198.                 ih = Math.random() * -h/16 - 1;
  199.             }
  200.             if (eh < h/8) {
  201.                 eh = h/8;
  202.                 ih = Math.random() * h/16 + 1;
  203.             }
  204.             if ((x+ew) > w) {
  205.                 x = (w - ew)-1;
  206.                 ix = Math.random() * -w/32 - 1;
  207.             }
  208.             if (x < 0) {
  209.                 x = 2;
  210.                 ix = Math.random() * w/32 + 1;
  211.             }
  212.             if ((y+eh) > h) {
  213.                 y = (h - eh)-2;
  214.                 iy = Math.random() * -h/32 - 1;
  215.             }
  216.             if (y < 0) {
  217.                 y = 2;
  218.                 iy = Math.random() * h/32 + 1;
  219.             }
  220.         }
  221.  
  222.  
  223.         public void reset(int w, int h) {
  224.             x = Math.random()*w;
  225.             y = Math.random()*h;
  226.             ew = (Math.random()*w)/2;
  227.             eh = (Math.random()*h)/2;
  228.         }
  229.     }
  230.  
  231.  
  232.     static class DemoControls extends CustomControls implements ActionListener {
  233.  
  234.         ClipAnim demo;
  235.         JToolBar toolbar;
  236.  
  237.         public DemoControls(ClipAnim demo) {
  238.             super(demo.name);
  239.             this.demo = demo;
  240.             setBackground(Color.gray);
  241.             add(toolbar = new JToolBar());
  242.             toolbar.setFloatable(false);
  243.             addTool("Objects", true);
  244.             addTool("Clip1", true);
  245.             addTool("Clip2", false);
  246.             addTool("Clip3", false);
  247.         }
  248.  
  249.  
  250.         public void addTool(String str, boolean state) {
  251.             JButton b = (JButton) toolbar.add(new JButton(str));
  252.             b.setBackground(state ? Color.green : Color.lightGray);
  253.             b.setSelected(state);
  254.             b.addActionListener(this);
  255.         }
  256.  
  257.  
  258.         public void actionPerformed(ActionEvent e) {
  259.             JButton b = (JButton) e.getSource();
  260.             b.setSelected(!b.isSelected());
  261.             b.setBackground(b.isSelected() ? Color.green : Color.lightGray);
  262.             if (b.getText().equals("Objects")) {
  263.                 demo.doObjects = b.isSelected();
  264.             } else if (b.getText().equals("Clip1")) {
  265.                 demo.animval[0].isSelected = b.isSelected();
  266.             } else if (b.getText().equals("Clip2")) {
  267.                 demo.animval[1].isSelected = b.isSelected();
  268.             } else if (b.getText().equals("Clip3")) {
  269.                 demo.animval[2].isSelected = b.isSelected();
  270.             }
  271.             if (demo.animating.thread == null) {
  272.                 demo.repaint();
  273.             }
  274.         }
  275.  
  276.         public Dimension getPreferredSize() {
  277.             return new Dimension(200,37);
  278.         }
  279.  
  280.  
  281.         public void run() {
  282.             try { 
  283.                 thread.sleep(5000);
  284.             } catch (InterruptedException e) { return; }
  285.             ((JButton) toolbar.getComponentAtIndex(2)).doClick();
  286.             try { 
  287.                 thread.sleep(5000);
  288.             } catch (InterruptedException e) { return; }
  289.             if (getSize().width > 400) {
  290.                 ((JButton) toolbar.getComponentAtIndex(3)).doClick();
  291.             }
  292.             thread = null;
  293.         }
  294.     } // End DemoControls
  295. } // End ClipAnim
  296.