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

2D Graphic

Development Platform:

Java

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