Path: blob/master/src/demo/share/jfc/J2Ddemo/java2d/demos/Mix/Stars3D.java
41175 views
/*1*2* Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7*8* - Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10*11* - Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* - Neither the name of Oracle nor the names of its16* contributors may be used to endorse or promote products derived17* from this software without specific prior written permission.18*19* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS20* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,21* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR22* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR23* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,24* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,25* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR26* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF27* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING28* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS29* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.30*/31package java2d.demos.Mix;323334import static java.awt.Color.BLACK;35import static java.awt.Color.BLUE;36import static java.awt.Color.GREEN;37import static java.awt.Color.RED;38import static java.awt.Color.WHITE;39import java.awt.AlphaComposite;40import java.awt.Color;41import java.awt.Component;42import java.awt.Dimension;43import java.awt.Font;44import java.awt.Graphics2D;45import java.awt.Rectangle;46import java.awt.Shape;47import java.awt.event.ActionEvent;48import java.awt.event.ActionListener;49import java.awt.font.FontRenderContext;50import java.awt.geom.AffineTransform;51import java.awt.geom.GeneralPath;52import java.awt.geom.Line2D;53import java.awt.geom.Path2D;54import java.awt.geom.PathIterator;55import java.awt.geom.Rectangle2D;56import java2d.ControlsSurface;57import java2d.CustomControls;58import javax.swing.JLabel;59import javax.swing.JTextField;606162/**63* Generate a 3D text shape with GeneralPath, render a number of small64* multi-colored rectangles and then render the 3D text shape.65*/66@SuppressWarnings("serial")67public class Stars3D extends ControlsSurface {6869private static Color[] colors = { RED, GREEN, WHITE };70private static AffineTransform at = AffineTransform.getTranslateInstance(-5,71-5);72private Shape shape, tshape;73private Shape ribbon;74protected int fontSize = 72;75protected String text = "OpenJDK";76protected int numStars = 300;7778public Stars3D() {79setBackground(BLACK);80setControls(new Component[] { new DemoControls(this) });81}8283@Override84public void render(int w, int h, Graphics2D g2) {8586Rectangle2D rect = new Rectangle2D.Double();87for (int i = 0; i < numStars; i++) {88g2.setColor(colors[i % 3]);89g2.setComposite(AlphaComposite.getInstance(90AlphaComposite.SRC_OVER, (float) Math.random()));91rect.setRect(w * Math.random(), h * Math.random(), 2, 2);92g2.fill(rect);93}9495FontRenderContext frc = g2.getFontRenderContext();96Font font = new Font(Font.SERIF, Font.BOLD|Font.ITALIC, fontSize);97shape = font.createGlyphVector(frc, text).getOutline();98tshape = at.createTransformedShape(shape);99PathIterator pi = shape.getPathIterator(null);100101float[] seg = new float[6];102float[] tseg = new float[6];103104GeneralPath working = new GeneralPath(Path2D.WIND_NON_ZERO);105float x = 0, y = 0; // Current point on the path106float tx = 0, ty = 0; // Transformed path point107float cx = 0, cy = 0; // Last moveTo point, for SEG_CLOSE108float tcx = 0, tcy = 0; // Transformed last moveTo point109110//111// Iterate through the Shape and build the ribbon112// by adding general path objects.113//114while (!pi.isDone()) {115int segType = pi.currentSegment(seg);116switch (segType) {117case PathIterator.SEG_MOVETO:118at.transform(seg, 0, tseg, 0, 1);119x = seg[0];120y = seg[1];121tx = tseg[0];122ty = tseg[1];123cx = x;124cy = y;125tcx = tx;126tcy = ty;127break;128case PathIterator.SEG_LINETO:129at.transform(seg, 0, tseg, 0, 1);130if (Line2D.relativeCCW(x, y, tx, ty, seg[0], seg[1]) < 0) {131working.moveTo(x, y);132working.lineTo(seg[0], seg[1]);133working.lineTo(tseg[0], tseg[1]);134working.lineTo(tx, ty);135working.lineTo(x, y);136} else {137working.moveTo(x, y);138working.lineTo(tx, ty);139working.lineTo(tseg[0], tseg[1]);140working.lineTo(seg[0], seg[1]);141working.lineTo(x, y);142}143144x = seg[0];145y = seg[1];146tx = tseg[0];147ty = tseg[1];148break;149150case PathIterator.SEG_QUADTO:151at.transform(seg, 0, tseg, 0, 2);152if (Line2D.relativeCCW(x, y, tx, ty, seg[2], seg[3]) < 0) {153working.moveTo(x, y);154working.quadTo(seg[0], seg[1],155seg[2], seg[3]);156working.lineTo(tseg[2], tseg[3]);157working.quadTo(tseg[0], tseg[1],158tx, ty);159working.lineTo(x, y);160} else {161working.moveTo(x, y);162working.lineTo(tx, ty);163working.quadTo(tseg[0], tseg[1],164tseg[2], tseg[3]);165working.lineTo(seg[2], seg[3]);166working.quadTo(seg[0], seg[1],167x, y);168}169170x = seg[2];171y = seg[3];172tx = tseg[2];173ty = tseg[3];174break;175176case PathIterator.SEG_CUBICTO:177at.transform(seg, 0, tseg, 0, 3);178if (Line2D.relativeCCW(x, y, tx, ty, seg[4], seg[5]) < 0) {179working.moveTo(x, y);180working.curveTo(seg[0], seg[1],181seg[2], seg[3],182seg[4], seg[5]);183working.lineTo(tseg[4], tseg[5]);184working.curveTo(tseg[2], tseg[3],185tseg[0], tseg[1],186tx, ty);187working.lineTo(x, y);188} else {189working.moveTo(x, y);190working.lineTo(tx, ty);191working.curveTo(tseg[0], tseg[1],192tseg[2], tseg[3],193tseg[4], tseg[5]);194working.lineTo(seg[4], seg[5]);195working.curveTo(seg[2], seg[3],196seg[0], seg[1],197x, y);198}199200x = seg[4];201y = seg[5];202tx = tseg[4];203ty = tseg[5];204break;205206case PathIterator.SEG_CLOSE:207if (Line2D.relativeCCW(x, y, tx, ty, cx, cy) < 0) {208working.moveTo(x, y);209working.lineTo(cx, cy);210working.lineTo(tcx, tcy);211working.lineTo(tx, ty);212working.lineTo(x, y);213} else {214working.moveTo(x, y);215working.lineTo(tx, ty);216working.lineTo(tcx, tcy);217working.lineTo(cx, cy);218working.lineTo(x, y);219}220x = cx;221y = cy;222tx = tcx;223ty = tcy;224}225pi.next();226} // while227ribbon = working;228229if (composite != null) {230g2.setComposite(composite);231} else {232g2.setComposite(AlphaComposite.SrcOver);233}234Rectangle r = shape.getBounds();235g2.translate(w * .5 - r.width * .5, h * .5 + r.height * .5);236237g2.setColor(BLUE);238g2.fill(tshape);239g2.setColor(new Color(255, 255, 255, 200));240g2.fill(ribbon);241242g2.setColor(WHITE);243g2.fill(shape);244245g2.setColor(BLUE);246g2.draw(shape);247}248249public static void main(String[] argv) {250createDemoFrame(new Stars3D());251}252253254static class DemoControls extends CustomControls implements ActionListener {255256Stars3D demo;257JTextField tf1, tf2;258259@SuppressWarnings("LeakingThisInConstructor")260public DemoControls(Stars3D demo) {261super(demo.name);262this.demo = demo;263JLabel l = new JLabel(" Text:");264l.setForeground(BLACK);265add(l);266add(tf1 = new JTextField(demo.text));267tf1.setPreferredSize(new Dimension(60, 20));268tf1.addActionListener(this);269l = new JLabel(" Size:");270l.setForeground(BLACK);271add(l);272add(tf2 = new JTextField(String.valueOf(demo.fontSize)));273tf2.setPreferredSize(new Dimension(30, 20));274tf2.addActionListener(this);275}276277@Override278public void actionPerformed(ActionEvent e) {279try {280if (e.getSource().equals(tf1)) {281demo.text = tf1.getText().trim();282} else if (e.getSource().equals(tf2)) {283demo.fontSize = Integer.parseInt(tf2.getText().trim());284if (demo.fontSize < 10) {285demo.fontSize = 10;286}287}288demo.repaint();289} catch (Exception ignored) {290}291}292293@Override294public Dimension getPreferredSize() {295return new Dimension(200, 37);296}297298@Override299@SuppressWarnings("SleepWhileHoldingLock")300public void run() {301Thread me = Thread.currentThread();302try {303Thread.sleep(999);304} catch (Exception e) {305return;306}307int length = getSize().width / 4;308int[] size = { length, length };309String[] str = { "OpenJDK", "J2D" };310while (thread == me) {311for (int i = 0; i < str.length; i++) {312demo.fontSize = size[i];313tf2.setText(String.valueOf(demo.fontSize));314tf1.setText(demo.text = str[i]);315demo.repaint();316try {317Thread.sleep(5555);318} catch (InterruptedException e) {319return;320}321}322}323thread = null;324}325} // End DemoControls326} // End Stars3D327328329330