home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / JBuilder8.iso / Solaris / resource / jre / demo / jfc / Java2D / src / java2d / demos / Fonts / Tree.java < prev   
Encoding:
Java Source  |  2002-09-06  |  4.5 KB  |  133 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.  * @(#)Tree.java    1.23 02/06/13
  38.  */
  39.  
  40. package java2d.demos.Fonts;
  41.  
  42. import java.awt.*;
  43. import java.awt.font.FontRenderContext;
  44. import java.awt.font.TextLayout;
  45. import java.awt.geom.AffineTransform;
  46. import java2d.AnimatingSurface;
  47.  
  48.  
  49. /**
  50.  * Transformation of characters.
  51.  */
  52. public class Tree extends AnimatingSurface {
  53.  
  54.     private char theC = 'A';
  55.     private Character theT = new Character(theC);
  56.     private Character theR = new Character((char) ((int) theC + 1));
  57.  
  58.  
  59.     public Tree() {
  60.         setBackground(Color.white);
  61.     }
  62.  
  63.  
  64.     public void reset(int w, int h) { }
  65.  
  66.  
  67.     public void step(int w, int h) {
  68.         setSleepAmount(4000);
  69.         theT = new Character(theC = ((char) ((int) theC + 1)));
  70.         theR = new Character((char) ((int) theC + 1));
  71.         if (theR.compareTo(new Character('z')) == 0) {
  72.             theC = 'A';
  73.         }
  74.     }
  75.  
  76.  
  77.     public void render(int w, int h, Graphics2D g2) {
  78.         int mindim = Math.min(w, h);
  79.         AffineTransform at = new AffineTransform();
  80.         at.translate((w - mindim) / 2.0,
  81.                      (h - mindim) / 2.0);
  82.         at.scale(mindim, mindim);
  83.         at.translate(0.5, 0.5);
  84.         at.scale(0.3, 0.3);
  85.         at.translate(-(Twidth + Rwidth), FontHeight / 4.0);
  86.         g2.transform(at);
  87.         tree(g2, mindim * 0.3, 0);
  88.  
  89.     }
  90.  
  91.  
  92.     static Font theFont = new Font("serif", Font.PLAIN, 1);
  93.     static double Twidth = 0.6;
  94.     static double Rwidth = 0.6;
  95.     static double FontHeight = 0.75;
  96.     static Color colors[] = {Color.blue,
  97.                              Color.red.darker(),
  98.                              Color.green.darker()};
  99.  
  100.  
  101.     public void tree(Graphics2D g2d, double size, int phase) {
  102.         g2d.setColor(colors[phase % 3]);
  103.         new TextLayout(theT.toString(),theFont,g2d.getFontRenderContext()).draw(g2d, 0.0f, 0.0f);
  104.         if (size > 10.0) {
  105.             AffineTransform at = new AffineTransform();
  106.             at.setToTranslation(Twidth, -0.1);
  107.             at.scale(0.6, 0.6);
  108.             g2d.transform(at);
  109.             size *= 0.6;
  110.             new TextLayout(theR.toString(),theFont, g2d.getFontRenderContext()).draw(g2d, 0.0f, 0.0f);
  111.             at.setToTranslation(Rwidth+0.75, 0);
  112.             g2d.transform(at);
  113.             Graphics2D g2dt = (Graphics2D) g2d.create();
  114.             at.setToRotation(-Math.PI / 2.0);
  115.             g2dt.transform(at);
  116.             tree(g2dt, size, phase + 1);
  117.             g2dt.dispose();
  118.             at.setToTranslation(.75, 0);
  119.             at.rotate(-Math.PI / 2.0);
  120.             at.scale(-1.0, 1.0);
  121.             at.translate(-Twidth, 0);
  122.             g2d.transform(at);
  123.             tree(g2d, size, phase);
  124.         }
  125.         g2d.setTransform(new AffineTransform());
  126.     }
  127.  
  128.  
  129.     public static void main(String argv[]) {
  130.         createDemoFrame(new Tree());
  131.     }
  132. }
  133.