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

2D Graphic

Development Platform:

Java

  1. /*
  2.  * @(#)BezierAnim.java 1.13 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.Arcs_Curves;
  31. import java.awt.*;
  32. import java.awt.event.*;
  33. import java.awt.image.BufferedImage;
  34. import java.awt.geom.GeneralPath;
  35. import javax.swing.*;
  36. import AnimatingContext;
  37. import DemoPanel;
  38. import DemoSurface;
  39. import CustomControls;
  40. /**
  41.  * Animated Bezier Curve with controls for different draw & fill paints.
  42.  */
  43. public class BezierAnim extends DemoSurface implements AnimatingContext, CustomControls {
  44.     private static final int NUMPTS = 6;
  45.     protected BasicStroke solid = new BasicStroke(10.0f, 
  46.                         BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND);
  47.     protected BasicStroke dashed = new BasicStroke(10.0f, 
  48.        BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND, 10, new float[] {5}, 0);
  49.     private float animpts[] = new float[NUMPTS * 2];
  50.     private float deltas[] = new float[NUMPTS * 2];
  51.     protected Paint fillPaint, drawPaint;
  52.     protected boolean doFill = true;
  53.     protected boolean doDraw = true;
  54.     protected GradientPaint gradient;
  55.     protected BasicStroke stroke;
  56.     private DemoControls controls;
  57.     public BezierAnim() {
  58.         setBackground(Color.white);
  59.         gradient = new GradientPaint(0,0,Color.red,200,200,Color.yellow);
  60.         fillPaint = gradient;
  61.         drawPaint = Color.blue;
  62.         stroke = solid;
  63.         controls = new DemoControls(this);
  64.     }
  65.     public String[] getCustomControlsConstraints() {
  66.         return new String[] { BorderLayout.NORTH };
  67.     }
  68.     public Component[] getCustomControls() {
  69.         return new Component[] { (Component) controls };
  70.     }
  71.     public void customControlsThread(int state) {
  72.         if (state == CustomControls.START) {
  73.             controls.start();
  74.         } else if (state == CustomControls.STOP) {
  75.             controls.stop();
  76.         }
  77.     }
  78.     public void animate(float[] pts, float[] deltas, int index, int limit) {
  79.         float newpt = pts[index] + deltas[index];
  80.         if (newpt <= 0) {
  81.             newpt = -newpt;
  82.             deltas[index] = (float) (Math.random() * 4.0 + 2.0);
  83.         } else if (newpt >= (float) limit) {
  84.             newpt = 2.0f * limit - newpt;
  85.             deltas[index] = - (float) (Math.random() * 4.0 + 2.0);
  86.         }
  87.         pts[index] = newpt;
  88.     }
  89.     public void reset(int w, int h) {
  90.         for (int i = 0; i < animpts.length; i += 2) {
  91.             animpts[i + 0] = (float) (Math.random() * w);
  92.             animpts[i + 1] = (float) (Math.random() * h);
  93.             deltas[i + 0] = (float) (Math.random() * 6.0 + 4.0);
  94.             deltas[i + 1] = (float) (Math.random() * 6.0 + 4.0);
  95.             if (animpts[i + 0] > w / 2.0f) {
  96.                 deltas[i + 0] = -deltas[i + 0];
  97.             }
  98.             if (animpts[i + 1] > h / 2.0f) {
  99.                 deltas[i + 1] = -deltas[i + 1];
  100.             }
  101.         }
  102.         gradient = new GradientPaint(0,0,Color.red,w*.7f,h*.7f,Color.yellow);
  103.     }
  104.     public void step(int w, int h) {
  105.         for (int i = 0; i < animpts.length; i += 2) {
  106.             animate(animpts, deltas, i + 0, w);
  107.             animate(animpts, deltas, i + 1, h);
  108.         }
  109.     }
  110.     public void drawDemo(int w, int h, Graphics2D g2) {
  111.         float[] ctrlpts = animpts;
  112.         int len = ctrlpts.length;
  113.         float prevx = ctrlpts[len - 2];
  114.         float prevy = ctrlpts[len - 1];
  115.         float curx = ctrlpts[0];
  116.         float cury = ctrlpts[1];
  117.         float midx = (curx + prevx) / 2.0f;
  118.         float midy = (cury + prevy) / 2.0f;
  119.         GeneralPath gp = new GeneralPath(GeneralPath.WIND_NON_ZERO);
  120.         gp.moveTo(midx, midy);
  121.         for (int i = 2; i <= ctrlpts.length; i += 2) {
  122.             float x1 = (midx + curx) / 2.0f;
  123.             float y1 = (midy + cury) / 2.0f;
  124.             prevx = curx;
  125.             prevy = cury;
  126.             if (i < ctrlpts.length) {
  127.                 curx = ctrlpts[i + 0];
  128.                 cury = ctrlpts[i + 1];
  129.             } else {
  130.                 curx = ctrlpts[0];
  131.                 cury = ctrlpts[1];
  132.             }
  133.             midx = (curx + prevx) / 2.0f;
  134.             midy = (cury + prevy) / 2.0f;
  135.             float x2 = (prevx + midx) / 2.0f;
  136.             float y2 = (prevy + midy) / 2.0f;
  137.             gp.curveTo(x1, y1, x2, y2, midx, midy);
  138.         }
  139.         gp.closePath();
  140.         if (doDraw) {
  141.             g2.setPaint(drawPaint);
  142.             g2.setStroke(stroke);
  143.             g2.draw(gp);
  144.         }
  145.         if (doFill) {
  146.             if (fillPaint instanceof GradientPaint) {
  147.                 fillPaint = gradient;
  148.             }
  149.             g2.setPaint(fillPaint);
  150.             g2.fill(gp);
  151.         }
  152.     }
  153.     public static void main(String argv[]) {
  154.         final DemoPanel dp = new DemoPanel(new BezierAnim());
  155.         JFrame f = new JFrame("Java2D Demo - BezierAnim");
  156.         f.addWindowListener(new WindowAdapter() {
  157.             public void windowClosing(WindowEvent e) {System.exit(0);}
  158.             public void windowDeiconified(WindowEvent e) { 
  159.                 dp.surface.start(); 
  160.             }
  161.             public void windowIconified(WindowEvent e) { 
  162.                 dp.surface.stop(); 
  163.             }
  164.         });
  165.         f.getContentPane().add("Center", dp);
  166.         f.pack();
  167.         f.setSize(new Dimension(400,300));
  168.         f.show();
  169.         dp.surface.start();
  170.     }
  171.     static class DemoControls extends JPanel implements ActionListener, Runnable {
  172.         static TexturePaint tp1, tp2;
  173.         static {
  174.             BufferedImage bi = new BufferedImage(2,1,BufferedImage.TYPE_INT_RGB);
  175.             bi.setRGB(0, 0, 0xff00ff00); bi.setRGB(1, 0, 0xffff0000);
  176.             tp1 = new TexturePaint(bi,new Rectangle(0,0,2,1));
  177.             bi = new BufferedImage(2,1,BufferedImage.TYPE_INT_RGB);
  178.             bi.setRGB(0, 0, 0xff0000ff); bi.setRGB(1, 0, 0xffff0000);
  179.             tp2 = new TexturePaint(bi,new Rectangle(0,0,2,1));
  180.         }
  181.         BezierAnim demo;
  182.         static Paint drawPaints[] = 
  183.                 {new Color(0,0,0,0), Color.blue, new Color(0, 0, 255, 126), 
  184.                   Color.blue, tp2 };
  185.         static String drawName[] =
  186.                 {"No Draw", "Blue", "Blue w/ Alpha", "Blue Dash", "Texture" }; 
  187.         static Paint fillPaints[] = 
  188.                 {new Color(0,0,0,0), Color.green, new Color(0, 255, 0, 126), 
  189.                   tp1, new GradientPaint(0,0,Color.red,30,30,Color.yellow) };
  190.         String fillName[] =
  191.                 {"No Fill", "Green", "Green w/ Alpha", "Texture", "Gradient"}; 
  192.         
  193.         JMenu fillMenu, drawMenu;
  194.         JMenuItem fillMI[] = new JMenuItem[fillPaints.length];
  195.         JMenuItem drawMI[] = new JMenuItem[drawPaints.length];
  196.         PaintedIcon fillIcons[] = new PaintedIcon[fillPaints.length];
  197.         PaintedIcon drawIcons[] = new PaintedIcon[drawPaints.length];
  198.         Font font = new Font("serif", Font.PLAIN, 10);
  199.         Thread thread;
  200.         public DemoControls(BezierAnim demo) {
  201.             this.demo = demo;
  202.             setBackground(Color.gray);
  203.             JMenuBar drawMenuBar = new JMenuBar();
  204.             add(drawMenuBar);
  205.             JMenuBar fillMenuBar = new JMenuBar();
  206.             add(fillMenuBar);
  207.             drawMenu = (JMenu) drawMenuBar.add(new JMenu("Draw Choice"));
  208.             drawMenu.setFont(font);
  209.             for (int i = 0; i < drawPaints.length; i++) {
  210.                 drawIcons[i]= new PaintedIcon(drawPaints[i]);
  211.                 drawMI[i] = drawMenu.add(new JMenuItem(drawName[i]));
  212.                 drawMI[i].setFont(font);
  213.                 drawMI[i].setIcon(drawIcons[i]);
  214.                 drawMI[i].addActionListener(this);
  215.             } 
  216.             drawMenu.setIcon(drawIcons[1]);
  217.             fillMenu = (JMenu) fillMenuBar.add(new JMenu("Fill Choice"));
  218.             fillMenu.setFont(font);
  219.             for (int i = 0; i < fillPaints.length; i++) {
  220.                 fillIcons[i]= new PaintedIcon(fillPaints[i]);
  221.                 fillMI[i] = fillMenu.add(new JMenuItem(fillName[i]));
  222.                 fillMI[i].setFont(font);
  223.                 fillMI[i].setIcon(fillIcons[i]);
  224.                 fillMI[i].addActionListener(this);
  225.             } 
  226.             fillMenu.setIcon(fillIcons[fillPaints.length-1]);
  227.             addMouseListener(new MouseAdapter() {
  228.                 public void mouseClicked(MouseEvent e) {
  229.                     if (thread == null) start(); else stop();
  230.                 }
  231.             });
  232.         }
  233.         public void actionPerformed(ActionEvent e) {
  234.             Object obj = e.getSource();
  235.             for (int i = 0; i < fillPaints.length; i++) {
  236.                 if (obj.equals(fillMI[i])) {
  237.                     demo.doFill = true;
  238.                     demo.fillPaint = fillPaints[i];
  239.                     fillMenu.setIcon(fillIcons[i]);
  240.                     break;
  241.                 } 
  242.             }
  243.             for (int i = 0; i < drawPaints.length; i++) {
  244.                 if (obj.equals(drawMI[i])) {
  245.                     demo.doDraw = true;
  246.                     demo.drawPaint = drawPaints[i];
  247.                     if (((JMenuItem) obj).getText().endsWith("Dash")) {
  248.                         demo.stroke = demo.dashed;
  249.                     } else {
  250.                         demo.stroke = demo.solid;
  251.                     }
  252.                     drawMenu.setIcon(drawIcons[i]);
  253.                     break;
  254.                 } 
  255.             }
  256.             if (obj.equals(fillMI[0])) {
  257.                 demo.doFill = false;
  258.             } else if (obj.equals(drawMI[0])) {
  259.                 demo.doDraw = false;
  260.             }
  261.             if (demo.thread == null) {
  262.                 demo.repaint();
  263.             }
  264.         }
  265.         public Dimension getPreferredSize() {
  266.             return new Dimension(200,32);
  267.         }
  268.         public void start() {
  269.             if (thread != null) {
  270.                 return;
  271.             }
  272.             thread = new Thread(this);
  273.             thread.setPriority(Thread.MIN_PRIORITY);
  274.             thread.setName("Arcs_Curves.BezierAnim DemoControls Thread");
  275.             thread.start();
  276.         }
  277.         public synchronized void stop() {
  278.             if (thread != null) {
  279.                 thread.interrupt();
  280.             }
  281.             thread = null;
  282.             notifyAll();
  283.         }
  284.         public void run() {
  285.             Thread me = Thread.currentThread();
  286.             while (thread == me) {
  287.                 for (int i = 1; i < drawMI.length; i++) {
  288.                     drawMI[i].doClick();
  289.                     for (int j = 0; j < fillMI.length; j++) {
  290.                         fillMI[j].doClick();
  291.                         try {
  292.                             thread.sleep(3000 + (long) (Math.random() * 3000));
  293.                         } catch (InterruptedException e) { return; }
  294.                     }
  295.                 }
  296.             }
  297.             thread = null;
  298.         }
  299.         static class PaintedIcon implements Icon {
  300.             Paint paint;
  301.             public PaintedIcon(Paint p) {
  302.                 this.paint = p;
  303.             }
  304.     
  305.             public void paintIcon(Component c, Graphics g, int x, int y) {
  306.                 Graphics2D g2 = (Graphics2D) g;
  307.                 g2.setPaint(paint);
  308.         g2.fillRect(x,y,getIconWidth(), getIconHeight());
  309.         g2.setColor(Color.gray);
  310.         g2.draw3DRect(x, y, getIconWidth()-1, getIconHeight()-1, true);
  311.             }
  312.             public int getIconWidth() { return 12; }
  313.             public int getIconHeight() { return 12; }
  314.         } // End PaintedIcon class
  315.     } // End DemoControls class
  316. } // End BezierAnim class