home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / JBuilder8.iso / Solaris / resource / jre / demo / jfc / Java2D / src / java2d / demos / Mix / BezierScroller.java < prev    next >
Encoding:
Java Source  |  2002-09-06  |  11.8 KB  |  350 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.  * @(#)BezierScroller.java    1.33 02/06/13
  38.  */
  39.  
  40. package java2d.demos.Mix;
  41.  
  42. import java.awt.*;
  43. import java.awt.event.*;
  44. import java.awt.geom.GeneralPath;
  45. import java.awt.geom.PathIterator;
  46. import java.awt.font.FontRenderContext;
  47. import java.awt.font.TextLayout;
  48. import java.awt.image.BufferedImage;
  49. import java.io.File;
  50. import java.io.FileReader;
  51. import java.io.BufferedReader;
  52. import java.util.Vector;
  53. import javax.swing.*;
  54. import java2d.AnimatingControlsSurface;
  55. import java2d.CustomControls;
  56.  
  57.  
  58. /**
  59.  * Animated Bezier Curve shape with images at the control points.
  60.  * README.txt file scrolling up. Composited Image fading in and out.
  61.  */
  62. public class BezierScroller extends AnimatingControlsSurface {
  63.  
  64.     private static String appletStrs[] = 
  65.         {  " ", "Java2Demo",  
  66.            "BezierScroller - Animated Bezier Curve shape with images", 
  67.            "For README.txt file scrolling run in application mode", " " };
  68.     private static final int NUMPTS = 6;
  69.     private static Color greenBlend = new Color(0, 255, 0, 100);
  70.     private static Font font = new Font("serif", Font.PLAIN, 12);
  71.     private static Color blueBlend = new Color(0, 0, 255, 100);
  72.     private static BasicStroke bs = new BasicStroke(3.0f);
  73.     private static Image hotj_img;
  74.     private static BufferedImage img;
  75.     private static final int UP = 0;
  76.     private static final int DOWN = 1;
  77.  
  78.     private float animpts[] = new float[NUMPTS * 2];
  79.     private float deltas[] = new float[NUMPTS * 2];
  80.     private BufferedReader reader;      
  81.     private int nStrs;          
  82.     private int strH;
  83.     private int yy, ix, iy, imgX;
  84.     private Vector vector, appletVector;
  85.     private float alpha = 0.2f;
  86.     private int alphaDirection;
  87.     protected boolean doImage, doShape, doText;
  88.     protected boolean buttonToggle;
  89.  
  90.  
  91.     public BezierScroller() {
  92.         setBackground(Color.white);
  93.         doShape = doText = true;
  94.         hotj_img = getImage("HotJava-16.gif");
  95.         Image image = getImage("jumptojavastrip.png");
  96.         int iw = image.getWidth(this);
  97.         int ih = image.getHeight(this);
  98.         img = new BufferedImage(iw, ih, BufferedImage.TYPE_INT_RGB);
  99.         img.createGraphics().drawImage(image, 0, 0, this);
  100.         setControls(new Component[] { new DemoControls(this) });
  101.     }
  102.  
  103.  
  104.     public void animate(float[] pts, float[] deltas, int index, int limit) {
  105.         float newpt = pts[index] + deltas[index];
  106.         if (newpt <= 0) {
  107.             newpt = -newpt;
  108.             deltas[index] = (float) (Math.random() * 4.0 + 2.0);
  109.         } else if (newpt >= (float) limit) {
  110.             newpt = 2.0f * limit - newpt;
  111.             deltas[index] = - (float) (Math.random() * 4.0 + 2.0);
  112.         }
  113.         pts[index] = newpt;
  114.     }
  115.  
  116.  
  117.     public void getFile() {
  118.         try {
  119.             String fName = "README.txt";
  120.             if ((reader = new BufferedReader(new FileReader(fName))) != null) {
  121.                 getLine();
  122.             }
  123.         } catch (Exception e) { reader = null; }
  124.         if (reader == null) {
  125.             appletVector = new Vector(100);
  126.             for (int i = 0; i < 100; i++) {
  127.                 appletVector.addElement(appletStrs[i%appletStrs.length]);
  128.             }
  129.             getLine();
  130.         }
  131.         buttonToggle = true;
  132.     }
  133.  
  134.  
  135.     public String getLine() {
  136.         String str = null;
  137.         if (reader != null) {
  138.             try {
  139.                 if ((str = reader.readLine()) != null) {
  140.                     if (str.length() == 0) {
  141.                         str = " ";
  142.                     }
  143.                     vector.addElement(str);
  144.                 }
  145.             } catch (Exception e) { e.printStackTrace(); reader = null; }
  146.         } else {
  147.             if (appletVector.size() != 0) {
  148.                 vector.addElement(str = (String) appletVector.remove(0));
  149.             }
  150.         }
  151.         return str;
  152.     }
  153.  
  154.  
  155.     public void reset(int w, int h) {
  156.         for (int i = 0; i < animpts.length; i += 2) {
  157.             animpts[i + 0] = (float) (Math.random() * w);
  158.             animpts[i + 1] = (float) (Math.random() * h);
  159.             deltas[i + 0] = (float) (Math.random() * 6.0 + 4.0);
  160.             deltas[i + 1] = (float) (Math.random() * 6.0 + 4.0);
  161.             if (animpts[i + 0] > w / 2.0f) {
  162.                 deltas[i + 0] = -deltas[i + 0];
  163.             }
  164.             if (animpts[i + 1] > h / 2.0f) {
  165.                 deltas[i + 1] = -deltas[i + 1];
  166.             }
  167.         }
  168.         FontMetrics fm = getFontMetrics(font);
  169.         strH = fm.getAscent()+fm.getDescent();
  170.         nStrs = h/strH+2;
  171.         vector = new Vector(nStrs);
  172.         ix = (int) (Math.random() * (w - 80));
  173.         iy = (int) (Math.random() * (h - 80));
  174.     }
  175.  
  176.  
  177.     public void step(int w, int h) {
  178.         if (doText && vector.size() == 0) {
  179.             getFile();
  180.         }
  181.         if (doText) {
  182.             String s = getLine();
  183.             if (s == null || vector.size() == nStrs && vector.size() != 0) {
  184.                 vector.removeElementAt(0);
  185.             }
  186.             yy = (s == null) ? 0 : h - vector.size() * strH;
  187.         }
  188.        
  189.         for (int i = 0; i < animpts.length && doShape; i += 2) {
  190.             animate(animpts, deltas, i + 0, w);
  191.             animate(animpts, deltas, i + 1, h);
  192.         }
  193.         if (doImage && alphaDirection == UP) {
  194.             if ((alpha += 0.025) > .99) {
  195.                 alphaDirection = DOWN;
  196.                 alpha = 1.0f;
  197.             }
  198.         } else if (doImage && alphaDirection == DOWN) {
  199.             if ((alpha -= .02) < 0.01) {
  200.                 alphaDirection = UP;
  201.                 alpha = 0;
  202.                 ix = (int) (Math.random() * (w - 80));
  203.                 iy = (int) (Math.random() * (h - 80));
  204.             }
  205.         }
  206.         if (doImage) {
  207.             if ((imgX += 80) == 800) {
  208.                 imgX = 0;
  209.             }
  210.         }
  211.     }
  212.  
  213.  
  214.  
  215.     public void render(int w, int h, Graphics2D g2) {
  216.  
  217.         if (doText) {
  218.             g2.setColor(Color.lightGray);
  219.             g2.setFont(font);
  220.             float y = yy;
  221.             for (int i = 0; i < vector.size(); i++) {
  222.                 g2.drawString((String)vector.get(i), 1, y += strH);
  223.             }
  224.         }
  225.  
  226.         if (doShape) {
  227.             float[] ctrlpts = animpts;
  228.             int len = ctrlpts.length;
  229.             float prevx = ctrlpts[len - 2];
  230.             float prevy = ctrlpts[len - 1];
  231.             float curx = ctrlpts[0];
  232.             float cury = ctrlpts[1];
  233.             float midx = (curx + prevx) / 2.0f;
  234.             float midy = (cury + prevy) / 2.0f;
  235.             GeneralPath gp = new GeneralPath(GeneralPath.WIND_NON_ZERO);
  236.             gp.moveTo(midx, midy);
  237.             for (int i = 2; i <= ctrlpts.length; i += 2) {
  238.                 float x1 = (midx + curx) / 2.0f;
  239.                 float y1 = (midy + cury) / 2.0f;
  240.                 prevx = curx;
  241.                 prevy = cury;
  242.                 if (i < ctrlpts.length) {
  243.                     curx = ctrlpts[i + 0];
  244.                     cury = ctrlpts[i + 1];
  245.                 } else {
  246.                     curx = ctrlpts[0];
  247.                     cury = ctrlpts[1];
  248.                 }
  249.                 midx = (curx + prevx) / 2.0f;
  250.                 midy = (cury + prevy) / 2.0f;
  251.                 float x2 = (prevx + midx) / 2.0f;
  252.                 float y2 = (prevy + midy) / 2.0f;
  253.                 gp.curveTo(x1, y1, x2, y2, midx, midy);
  254.             }
  255.             gp.closePath();
  256.  
  257.             g2.setColor(blueBlend);
  258.             g2.setStroke(bs);
  259.             g2.draw(gp);
  260.             g2.setColor(greenBlend);
  261.             g2.fill(gp);
  262.  
  263.             PathIterator pi = gp.getPathIterator(null);
  264.             float pts[] = new float[6];
  265.             while ( !pi.isDone() ) {
  266.                 if (pi.currentSegment(pts) == pi.SEG_CUBICTO) {
  267.                     g2.drawImage(hotj_img, (int) pts[0], (int) pts[1], this);
  268.                 }
  269.                 pi.next();
  270.             }
  271.         }
  272.  
  273.         if (doImage) {
  274.             AlphaComposite ac = AlphaComposite.getInstance(
  275.                                    AlphaComposite.SRC_OVER, alpha);
  276.             g2.setComposite(ac);
  277.             g2.drawImage(img.getSubimage(imgX,0,80,80), ix, iy, this);
  278.         }
  279.     }
  280.  
  281.  
  282.     public static void main(String argv[]) {
  283.         createDemoFrame(new BezierScroller());
  284.     }
  285.  
  286.  
  287.     static class DemoControls extends CustomControls implements ActionListener {
  288.  
  289.         BezierScroller demo;
  290.         JToolBar toolbar;
  291.         JComboBox combo;
  292.  
  293.         public DemoControls(BezierScroller demo) {
  294.             super(demo.name);
  295.             this.demo = demo;
  296.             setBackground(Color.gray);
  297.             add(toolbar = new JToolBar());
  298.             toolbar.setFloatable(false);
  299.             addTool("Image", false);
  300.             addTool("Shape", true);
  301.             addTool("Text", true);
  302.         }
  303.  
  304.  
  305.         public void addTool(String str, boolean state) {
  306.             JButton b = (JButton) toolbar.add(new JButton(str));
  307.             b.setBackground(state ? Color.green : Color.lightGray);
  308.             b.setSelected(state);
  309.             b.addActionListener(this);
  310.         }
  311.  
  312.  
  313.         public void actionPerformed(ActionEvent e) {
  314.             JButton b = (JButton) e.getSource();
  315.             b.setSelected(!b.isSelected());
  316.             b.setBackground(b.isSelected() ? Color.green : Color.lightGray);
  317.             if (b.getText().equals("Image")) {
  318.                 demo.doImage = b.isSelected();
  319.             } else if (b.getText().equals("Shape")) {
  320.                 demo.doShape = b.isSelected();
  321.             } else {
  322.                 demo.doText = b.isSelected();
  323.             }
  324.             if (demo.animating.thread == null) {
  325.                 demo.repaint();
  326.             }
  327.         }
  328.  
  329.         public Dimension getPreferredSize() {
  330.             return new Dimension(200,37);
  331.         }
  332.  
  333.  
  334.         public void run() {
  335.             Thread me = Thread.currentThread();
  336.             int i = 0;
  337.             while (thread == me) {
  338.                 try {
  339.                     thread.sleep(250);
  340.                 } catch (InterruptedException e) { return; }
  341.                 if (demo.buttonToggle) {
  342.                     ((JButton) toolbar.getComponentAtIndex(i++%2)).doClick();
  343.                     demo.buttonToggle = false;
  344.                 }
  345.             }
  346.             thread = null;
  347.         }
  348.     } // End DemoControls
  349. } // End BezierScroller
  350.