/* * @(#)LineAnim.java 1.10 98/06/29 * * Copyright 1998 by Sun Microsystems, Inc., * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A. * All rights reserved. * * This software is the confidential and proprietary information * of Sun Microsystems, Inc. ("Confidential Information"). You * shall not disclose such Confidential Information and shall use * it only in accordance with the terms of the license agreement * you entered into with Sun. */ package demos.Lines; import java.awt.*; import java.awt.geom.*; import java.awt.font.TextLayout; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; import java.awt.event.WindowAdapter; import J2DCanvas; /** * Lines & Paths animation illustrating BasicStroke attributes. */ public class LineAnim extends J2DCanvas { private int caps[] = { BasicStroke.CAP_BUTT, BasicStroke.CAP_SQUARE, BasicStroke.CAP_ROUND}; private int joins[] = { BasicStroke.JOIN_MITER, BasicStroke.JOIN_BEVEL, BasicStroke.JOIN_ROUND}; private int rAmt[], direction[], speed[]; private Line2D lines[]; private Color colors[] = { Color.gray, Color.pink, Color.lightGray }; private BasicStroke strokes[]; private final int CLOCKWISE = 0; private final int COUNTERCW = 1; private GeneralPath path; private Point2D[] pts; private float size; public LineAnim() { setBackground(Color.white); runnable = true; } public void drawDemo(Graphics2D g2) { if (newBufferedImage) { size = (w > h) ? h/6f : w/6f; lines = new Line2D[3]; strokes = new BasicStroke[lines.length]; rAmt = new int[lines.length]; direction = new int[lines.length]; speed = new int[lines.length]; for (int i = 0; i < lines.length; i++) { lines[i] = new Line2D.Float(0,0,size,0); strokes[i] = new BasicStroke(size/3, caps[i], joins[i]); rAmt[i] = i * 360/lines.length; direction[i] = i%2; speed[i] = i + 1; } path = new GeneralPath(); path.moveTo(size, -size/2); path.lineTo(size+size/2, 0); path.lineTo(size, +size/2); Ellipse2D e = new Ellipse2D.Float(w/2-size*2-4.5f,h/2-size*2-4.5f,size*4,size*4); PathIterator pi = e.getPathIterator(null); FlatteningPathIterator f = new FlatteningPathIterator(pi,0.9); Point2D[] points = new Point2D[100]; int num_pts = 0; while ( !f.isDone() ) { float[] pt = new float[6]; switch ( f.currentSegment(pt) ) { case FlatteningPathIterator.SEG_MOVETO: case FlatteningPathIterator.SEG_LINETO: points[num_pts] = new Point2D.Float(pt[0], pt[1]); num_pts++; } f.next(); } pts = new Point2D[num_pts]; System.arraycopy(points, 0, pts, 0, num_pts); } Ellipse2D ellipse = new Ellipse2D.Float(w/2-size,h/2-size,size*2,size*2); g2.setColor(Color.black); g2.draw(ellipse); for (int i = 0; i < lines.length; i++) { AffineTransform at = new AffineTransform(); at.translate(w/2,h/2); at.rotate(rAmt[i]*java.lang.Math.PI/180); g2.setStroke(strokes[i]); g2.setColor(colors[i]); g2.draw(at.createTransformedShape(lines[i])); g2.draw(at.createTransformedShape(path)); int j = (int) ((double) rAmt[i]/360 * pts.length); j = (j == pts.length) ? pts.length-1 : j; g2.fill(new Ellipse2D.Double(pts[j].getX(), pts[j].getY(), 9, 9)); if (direction[i] == CLOCKWISE) { rAmt[i] += speed[i]; if (rAmt[i] == 360) rAmt[i] = 0; } else { rAmt[i] -= speed[i]; if (rAmt[i] == 0) rAmt[i] = 360; } } g2.setStroke(new BasicStroke(1)); g2.setColor(Color.black); for (int i = 0; i < pts.length; i++) g2.draw(new Ellipse2D.Double(pts[i].getX(), pts[i].getY(), 9, 9)); } public static void main(String argv[]) { final LineAnim demo = new LineAnim(); WindowListener l = new WindowAdapter() { public void windowClosing(WindowEvent e) {System.exit(0);} public void windowDeiconified(WindowEvent e) { demo.start(); } public void windowIconified(WindowEvent e) { demo.stop(); } }; Frame f = new Frame("Java2D Demo - LineAnim"); f.addWindowListener(l); f.add("Center", demo); f.pack(); f.setSize(new Dimension(400,300)); f.show(); demo.start(); } }