home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / JBuilder8.iso / Solaris / resource / jre / demo / jfc / Java2D / src / java2d / demos / Mix / Stars3D.java < prev   
Encoding:
Java Source  |  2002-09-06  |  11.8 KB  |  308 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.  * @(#)Stars3D.java    1.23 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.*;
  45. import java.awt.font.FontRenderContext;
  46. import javax.swing.*;
  47. import java2d.ControlsSurface;
  48. import java2d.CustomControls;
  49.  
  50.  
  51.  
  52. /**
  53.  * Generate a 3D text shape with GeneralPath, render a number of small
  54.  * multi-colored rectangles and then render the 3D text shape.
  55.  */
  56. public class Stars3D extends ControlsSurface {
  57.  
  58.     private static Color colors[] = { Color.red, Color.green, Color.white };
  59.     private static AffineTransform at = AffineTransform.getTranslateInstance(-5, -5);
  60.     private Shape shape, tshape;
  61.     private Shape ribbon;
  62.     protected int fontSize = 72;
  63.     protected String text = "Java2D";
  64.     protected int numStars = 300;
  65.     private DemoControls controls;
  66.  
  67.  
  68.     public Stars3D() {
  69.         setBackground(Color.black);
  70.         setControls(new Component[] { new DemoControls(this) });
  71.     }
  72.  
  73.  
  74.     public void render(int w, int h, Graphics2D g2) {
  75.  
  76.         Rectangle2D rect = new Rectangle2D.Double();
  77.         for (int i = 0; i < numStars; i++) {
  78.             g2.setColor(colors[i%3]);
  79.             g2.setComposite(AlphaComposite.getInstance(
  80.                          AlphaComposite.SRC_OVER, (float) Math.random()));
  81.             rect.setRect(w*Math.random(), h*Math.random(),2,2);
  82.             g2.fill(rect);
  83.         }
  84.  
  85.         FontRenderContext frc = g2.getFontRenderContext();
  86.         Font font = new Font("serif.bolditalic", Font.PLAIN, fontSize);
  87.         shape = font.createGlyphVector(frc, text).getOutline();
  88.         tshape = at.createTransformedShape(shape);
  89.         PathIterator pi = shape.getPathIterator(null);
  90.         
  91.         float seg[] = new float[6];
  92.         float tseg[] = new float[6];
  93.         
  94.         GeneralPath working = new GeneralPath(GeneralPath.WIND_NON_ZERO);
  95.         float x=0, y=0; // Current point on the path
  96.         float tx=0, ty=0; // Transformed path point
  97.         float cx=0, cy=0; // Last moveTo point, for SEG_CLOSE
  98.         float tcx=0, tcy=0; // Transformed last moveTo point
  99.         
  100.         //
  101.         // Iterate through the Shape and build the ribbon
  102.         // by adding general path objects.
  103.         //
  104.         while(!pi.isDone()) {
  105.             int segType = pi.currentSegment(seg);
  106.             switch(segType) {
  107.                 case PathIterator.SEG_MOVETO:
  108.                         at.transform(seg, 0, tseg, 0, 1);
  109.                         x = seg[0];
  110.                         y = seg[1];
  111.                         tx = tseg[0];
  112.                         ty = tseg[1];
  113.                         cx = x;
  114.                         cy = y;
  115.                         tcx = tx;
  116.                         tcy = ty;
  117.                         break;
  118.                 case PathIterator.SEG_LINETO:
  119.                         at.transform(seg, 0, tseg, 0, 1);
  120.                         if (Line2D.relativeCCW(x, y, tx, ty,
  121.                                                seg[0], seg[1]) < 0) {
  122.                             working.moveTo(x, y);
  123.                             working.lineTo(seg[0], seg[1]);
  124.                             working.lineTo(tseg[0], tseg[1]);
  125.                             working.lineTo(tx, ty);
  126.                             working.lineTo(x, y);
  127.                         } else {
  128.                             working.moveTo(x, y);
  129.                             working.lineTo(tx, ty);
  130.                             working.lineTo(tseg[0], tseg[1]);
  131.                             working.lineTo(seg[0], seg[1]);
  132.                             working.lineTo(x, y);
  133.                         }
  134.                         
  135.                         x = seg[0];
  136.                         y = seg[1];
  137.                         tx = tseg[0];
  138.                         ty = tseg[1];
  139.                         break;
  140.                         
  141.                 case PathIterator.SEG_QUADTO:
  142.                         at.transform(seg, 0, tseg, 0, 2);
  143.                         if (Line2D.relativeCCW(x, y, tx, ty,
  144.                                                seg[2], seg[3]) < 0) {
  145.                             working.moveTo(x, y);
  146.                             working.quadTo(seg[0], seg[1],
  147.                                            seg[2], seg[3]);
  148.                             working.lineTo(tseg[2], tseg[3]);
  149.                             working.quadTo(tseg[0], tseg[1],
  150.                                            tx, ty);
  151.                             working.lineTo(x, y);
  152.                         } else {
  153.                             working.moveTo(x, y);
  154.                             working.lineTo(tx, ty);
  155.                             working.quadTo(tseg[0], tseg[1],
  156.                                            tseg[2], tseg[3]);
  157.                             working.lineTo(seg[2], seg[3]);
  158.                             working.quadTo(seg[0], seg[1],
  159.                                            x, y);
  160.                         }
  161.                 
  162.                         x = seg[2];
  163.                         y = seg[3];
  164.                         tx = tseg[2];
  165.                         ty = tseg[3];
  166.                         break;
  167.         
  168.                 case PathIterator.SEG_CUBICTO:
  169.                         at.transform(seg, 0, tseg, 0, 3);
  170.                         if (Line2D.relativeCCW(x, y, tx, ty,
  171.                                                seg[4], seg[5]) < 0) {
  172.                             working.moveTo(x, y);
  173.                             working.curveTo(seg[0], seg[1],
  174.                                             seg[2], seg[3],
  175.                                             seg[4], seg[5]);
  176.                             working.lineTo(tseg[4], tseg[5]);
  177.                             working.curveTo(tseg[2], tseg[3],
  178.                                             tseg[0], tseg[1],
  179.                                             tx, ty);
  180.                             working.lineTo(x, y);
  181.                         } else {
  182.                             working.moveTo(x, y);
  183.                             working.lineTo(tx, ty);
  184.                             working.curveTo(tseg[0], tseg[1],
  185.                                             tseg[2], tseg[3],
  186.                                             tseg[4], tseg[5]);
  187.                             working.lineTo(seg[4], seg[5]);
  188.                             working.curveTo(seg[2], seg[3],
  189.                                             seg[0], seg[1],
  190.                                             x, y);
  191.                         }
  192.                 
  193.                         x = seg[4];
  194.                         y = seg[5];
  195.                         tx = tseg[4];
  196.                         ty = tseg[5];
  197.                         break;
  198.         
  199.                 case PathIterator.SEG_CLOSE:
  200.                         if (Line2D.relativeCCW(x, y, tx, ty,
  201.                                                cx, cy) < 0) {
  202.                             working.moveTo(x, y);
  203.                             working.lineTo(cx, cy);
  204.                             working.lineTo(tcx, tcy);
  205.                             working.lineTo(tx, ty);
  206.                             working.lineTo(x, y);
  207.                         } else {
  208.                             working.moveTo(x, y);
  209.                             working.lineTo(tx, ty);
  210.                             working.lineTo(tcx, tcy);
  211.                             working.lineTo(cx, cy);
  212.                             working.lineTo(x, y);
  213.                         }
  214.                         x = cx; 
  215.                         y = cy;
  216.                         tx = tcx;
  217.                         ty = tcy;
  218.             }
  219.             pi.next();
  220.         } // while
  221.         ribbon = working;
  222.  
  223.         g2.setComposite(AlphaComposite.SrcOver);
  224.         Rectangle r = shape.getBounds();
  225.         g2.translate(w*.5-r.width*.5,h*.5+r.height*.5);
  226.  
  227.         g2.setColor(Color.blue);
  228.         g2.fill(tshape);
  229.         g2.setColor(new Color(255, 255, 255, 200));
  230.         g2.fill(ribbon);
  231.  
  232.         g2.setColor(Color.white);
  233.         g2.fill(shape);
  234.  
  235.         g2.setColor(Color.blue);
  236.         g2.draw(shape);
  237.     }
  238.  
  239.  
  240.     public static void main(String argv[]) {
  241.         createDemoFrame(new Stars3D());
  242.     }
  243.  
  244.  
  245.     static class DemoControls extends CustomControls implements ActionListener {
  246.  
  247.         Stars3D demo;
  248.         JTextField tf1, tf2;
  249.  
  250.         public DemoControls(Stars3D demo) {
  251.             super(demo.name);
  252.             this.demo = demo;
  253.             setBackground(Color.gray);
  254.             JLabel l = new JLabel("  Text:");
  255.             l.setForeground(Color.black);
  256.             add(l);
  257.             add(tf1 = new JTextField(demo.text));
  258.             tf1.setPreferredSize(new Dimension(60,20));
  259.             tf1.addActionListener(this);
  260.             l = new JLabel("  Size:");
  261.             l.setForeground(Color.black);
  262.             add(l);
  263.             add(tf2 = new JTextField(String.valueOf(demo.fontSize)));
  264.             tf2.setPreferredSize(new Dimension(30,20));
  265.             tf2.addActionListener(this);
  266.         }
  267.  
  268.         public void actionPerformed(ActionEvent e) {
  269.             try { 
  270.                 if (e.getSource().equals(tf1)) {
  271.                     demo.text = tf1.getText().trim();
  272.                 } else if (e.getSource().equals(tf2)) {
  273.                     demo.fontSize = Integer.parseInt(tf2.getText().trim());
  274.                     if (demo.fontSize < 10) {
  275.                         demo.fontSize = 10;
  276.                     }
  277.                 }
  278.                 demo.repaint();
  279.             } catch (Exception ex) {}
  280.         }
  281.  
  282.         public Dimension getPreferredSize() {
  283.             return new Dimension(200,32);
  284.         }
  285.  
  286.  
  287.         public void run() {
  288.             Thread me = Thread.currentThread();
  289.             try { thread.sleep(999); } catch (Exception e) { return; }
  290.             int width = getSize().width;
  291.             int size[] = { (int)(width*.25), (int)(width*.35)};
  292.             String str[] = { "JAVA", "J2D" };
  293.             while (thread == me) {
  294.                 for (int i = 0; i < 2; i++) {
  295.                     demo.fontSize = size[i];
  296.                     tf2.setText(String.valueOf(demo.fontSize));
  297.                     tf1.setText(demo.text = str[i]);
  298.                     demo.repaint();
  299.                     try {
  300.                         thread.sleep(5555);
  301.                     } catch (InterruptedException e) { return; }
  302.                 }
  303.             }
  304.             thread = null;
  305.         }
  306.     } // End DemoControls
  307. } // End Stars3D
  308.