1 /*
   2  *
   3  * Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.
   4  *
   5  * Redistribution and use in source and binary forms, with or without
   6  * modification, are permitted provided that the following conditions
   7  * are met:
   8  *
   9  *   - Redistributions of source code must retain the above copyright
  10  *     notice, this list of conditions and the following disclaimer.
  11  *
  12  *   - Redistributions in binary form must reproduce the above copyright
  13  *     notice, this list of conditions and the following disclaimer in the
  14  *     documentation and/or other materials provided with the distribution.
  15  *
  16  *   - Neither the name of Oracle nor the names of its
  17  *     contributors may be used to endorse or promote products derived
  18  *     from this software without specific prior written permission.
  19  *
  20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  21  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31  */
  32 package java2d.demos.Fonts;
  33 
  34 
  35 import static java.awt.Color.BLUE;
  36 import static java.awt.Color.GREEN;
  37 import static java.awt.Color.RED;
  38 import static java.awt.Color.WHITE;
  39 import java.awt.Color;
  40 import java.awt.Font;
  41 import java.awt.Graphics2D;
  42 import java.awt.font.TextLayout;
  43 import java.awt.geom.AffineTransform;
  44 import java2d.AnimatingSurface;
  45 
  46 
  47 /**
  48  * Transformation of characters.
  49  */
  50 @SuppressWarnings("serial")
  51 public class Tree extends AnimatingSurface {
  52 
  53     private char theC = 'A';
  54     private Character theT = Character.valueOf(theC);
  55     private Character theR = Character.valueOf((char) (theC + 1));
  56 
  57     public Tree() {
  58         setBackground(WHITE);
  59     }
  60 
  61     @Override
  62     public void reset(int w, int h) {
  63     }
  64 
  65     @Override
  66     public void step(int w, int h) {
  67         setSleepAmount(4000);
  68         theT = Character.valueOf(theC = ((char) (theC + 1)));
  69         theR = Character.valueOf((char) (theC + 1));
  70         if (theR.compareTo(Character.valueOf('z')) == 0) {
  71             theC = 'A';
  72         }
  73     }
  74 
  75     @Override
  76     public void render(int w, int h, Graphics2D g2) {
  77         int mindim = Math.min(w, h);
  78         AffineTransform at = new AffineTransform();
  79         at.translate((w - mindim) / 2.0,
  80                 (h - mindim) / 2.0);
  81         at.scale(mindim, mindim);
  82         at.translate(0.5, 0.5);
  83         at.scale(0.3, 0.3);
  84         at.translate(-(Twidth + Rwidth), FontHeight / 4.0);
  85         g2.transform(at);
  86         tree(g2, mindim * 0.3, 0);
  87 
  88     }
  89     static Font theFont = new Font(Font.SERIF, Font.PLAIN, 1);
  90     static double Twidth = 0.6;
  91     static double Rwidth = 0.6;
  92     static double FontHeight = 0.75;
  93     static Color[] colors = { BLUE,
  94         RED.darker(),
  95         GREEN.darker() };
  96 
  97     public void tree(Graphics2D g2d, double size, int phase) {
  98         g2d.setColor(colors[phase % 3]);
  99         new TextLayout(theT.toString(), theFont, g2d.getFontRenderContext()).
 100                 draw(g2d, 0.0f, 0.0f);
 101         if (size > 10.0) {
 102             AffineTransform at = new AffineTransform();
 103             at.setToTranslation(Twidth, -0.1);
 104             at.scale(0.6, 0.6);
 105             g2d.transform(at);
 106             size *= 0.6;
 107             new TextLayout(theR.toString(), theFont, g2d.getFontRenderContext()).
 108                     draw(g2d, 0.0f, 0.0f);
 109             at.setToTranslation(Rwidth + 0.75, 0);
 110             g2d.transform(at);
 111             Graphics2D g2dt = (Graphics2D) g2d.create();
 112             at.setToRotation(-Math.PI / 2.0);
 113             g2dt.transform(at);
 114             tree(g2dt, size, phase + 1);
 115             g2dt.dispose();
 116             at.setToTranslation(.75, 0);
 117             at.rotate(-Math.PI / 2.0);
 118             at.scale(-1.0, 1.0);
 119             at.translate(-Twidth, 0);
 120             g2d.transform(at);
 121             tree(g2d, size, phase);
 122         }
 123         g2d.setTransform(new AffineTransform());
 124     }
 125 
 126     public static void main(String[] argv) {
 127         createDemoFrame(new Tree());
 128     }
 129 }