Path: blob/master/src/demo/share/jfc/J2Ddemo/java2d/demos/Lines/LineAnim.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.Lines;323334import static java.awt.Color.BLACK;35import static java.awt.Color.GRAY;36import static java.awt.Color.LIGHT_GRAY;37import static java.awt.Color.PINK;38import static java.awt.Color.WHITE;39import java.awt.BasicStroke;40import java.awt.Color;41import java.awt.Graphics2D;42import java.awt.geom.AffineTransform;43import java.awt.geom.Ellipse2D;44import java.awt.geom.GeneralPath;45import java.awt.geom.Line2D;46import java.awt.geom.PathIterator;47import java.awt.geom.Point2D;48import java2d.AnimatingSurface;495051/**52* Lines & Paths animation illustrating BasicStroke attributes.53*/54@SuppressWarnings("serial")55public class LineAnim extends AnimatingSurface {5657private static int[] caps = { BasicStroke.CAP_BUTT,58BasicStroke.CAP_SQUARE, BasicStroke.CAP_ROUND };59private static int[] joins = { BasicStroke.JOIN_MITER,60BasicStroke.JOIN_BEVEL, BasicStroke.JOIN_ROUND };61private static Color[] colors = { GRAY, PINK, LIGHT_GRAY };62private static BasicStroke bs1 = new BasicStroke(1.0f);63private static final int CLOCKWISE = 0;64private Line2D[] lines = new Line2D[3];65private int[] rAmt = new int[lines.length];66private int[] direction = new int[lines.length];67private int[] speed = new int[lines.length];68private BasicStroke[] strokes = new BasicStroke[lines.length];69private GeneralPath path;70private Point2D[] pts;71private float size;72private Ellipse2D ellipse = new Ellipse2D.Double();7374public LineAnim() {75setBackground(WHITE);76}7778@Override79public void reset(int w, int h) {80size = (w > h) ? h / 6f : w / 6f;81for (int i = 0; i < lines.length; i++) {82lines[i] = new Line2D.Float(0, 0, size, 0);83strokes[i] = new BasicStroke(size / 3, caps[i], joins[i]);84rAmt[i] = i * 360 / lines.length;85direction[i] = i % 2;86speed[i] = i + 1;87}8889path = new GeneralPath();90path.moveTo(size, -size / 2);91path.lineTo(size + size / 2, 0);92path.lineTo(size, +size / 2);9394ellipse.setFrame(w / 2 - size * 2 - 4.5f, h / 2 - size * 2 - 4.5f, size95* 4, size * 4);96PathIterator pi = ellipse.getPathIterator(null, 0.9);97Point2D[] points = new Point2D[100];98int num_pts = 0;99while (!pi.isDone()) {100float[] pt = new float[6];101switch (pi.currentSegment(pt)) {102case PathIterator.SEG_MOVETO:103case PathIterator.SEG_LINETO:104points[num_pts] = new Point2D.Float(pt[0], pt[1]);105num_pts++;106}107pi.next();108}109pts = new Point2D[num_pts];110System.arraycopy(points, 0, pts, 0, num_pts);111}112113@Override114public void step(int w, int h) {115for (int i = 0; i < lines.length; i++) {116if (direction[i] == CLOCKWISE) {117rAmt[i] += speed[i];118if (rAmt[i] == 360) {119rAmt[i] = 0;120}121} else {122rAmt[i] -= speed[i];123if (rAmt[i] == 0) {124rAmt[i] = 360;125}126}127}128}129130@Override131public void render(int w, int h, Graphics2D g2) {132133ellipse.setFrame(w / 2 - size, h / 2 - size, size * 2, size * 2);134g2.setColor(BLACK);135g2.draw(ellipse);136137for (int i = 0; i < lines.length; i++) {138AffineTransform at = AffineTransform.getTranslateInstance(w / 2, h139/ 2);140at.rotate(Math.toRadians(rAmt[i]));141g2.setStroke(strokes[i]);142g2.setColor(colors[i]);143g2.draw(at.createTransformedShape(lines[i]));144g2.draw(at.createTransformedShape(path));145146int j = (int) ((double) rAmt[i] / 360 * pts.length);147j = (j == pts.length) ? pts.length - 1 : j;148ellipse.setFrame(pts[j].getX(), pts[j].getY(), 9, 9);149g2.fill(ellipse);150}151152g2.setStroke(bs1);153g2.setColor(BLACK);154for (int i = 0; i < pts.length; i++) {155ellipse.setFrame(pts[i].getX(), pts[i].getY(), 9, 9);156g2.draw(ellipse);157}158}159160public static void main(String[] argv) {161createDemoFrame(new LineAnim());162}163}164165166