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

2D Graphic

Development Platform:

Java

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