1 /*
2 *
3 * Copyright (c) 2007, 2011, 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
38 import static java.awt.Color.PINK;
39 import static java.awt.Color.WHITE;
40 import java.awt.BasicStroke;
41 import java.awt.Color;
42 import java.awt.Graphics2D;
43 import java.awt.geom.AffineTransform;
44 import java.awt.geom.Ellipse2D;
45 import java.awt.geom.GeneralPath;
46 import java.awt.geom.Line2D;
47 import java.awt.geom.PathIterator;
48 import java.awt.geom.Point2D;
49 import java2d.AnimatingSurface;
50
51
52 /**
53 * Lines & Paths animation illustrating BasicStroke attributes.
54 */
55 @SuppressWarnings("serial")
56 public class LineAnim extends AnimatingSurface {
57
58 private static int caps[] = { BasicStroke.CAP_BUTT,
59 BasicStroke.CAP_SQUARE, BasicStroke.CAP_ROUND };
60 private static int joins[] = { BasicStroke.JOIN_MITER,
61 BasicStroke.JOIN_BEVEL, BasicStroke.JOIN_ROUND };
62 private static Color colors[] = { GRAY, PINK, LIGHT_GRAY };
63 private static BasicStroke bs1 = new BasicStroke(1.0f);
64 private static final int CLOCKWISE = 0;
65 private Line2D lines[] = new Line2D[3];
66 private int rAmt[] = new int[lines.length];
67 private int direction[] = new int[lines.length];
68 private int speed[] = new int[lines.length];
69 private BasicStroke strokes[] = new BasicStroke[lines.length];
70 private GeneralPath path;
71 private Point2D[] pts;
72 private float size;
73 private Ellipse2D ellipse = new Ellipse2D.Double();
74
75 public LineAnim() {
76 setBackground(WHITE);
77 }
78
79 @Override
80 public void reset(int w, int h) {
81 size = (w > h) ? h / 6f : w / 6f;
82 for (int i = 0; i < lines.length; i++) {
83 lines[i] = new Line2D.Float(0, 0, size, 0);
84 strokes[i] = new BasicStroke(size / 3, caps[i], joins[i]);
85 rAmt[i] = i * 360 / lines.length;
86 direction[i] = i % 2;
87 speed[i] = i + 1;
88 }
89
141 at.rotate(Math.toRadians(rAmt[i]));
142 g2.setStroke(strokes[i]);
143 g2.setColor(colors[i]);
144 g2.draw(at.createTransformedShape(lines[i]));
145 g2.draw(at.createTransformedShape(path));
146
147 int j = (int) ((double) rAmt[i] / 360 * pts.length);
148 j = (j == pts.length) ? pts.length - 1 : j;
149 ellipse.setFrame(pts[j].getX(), pts[j].getY(), 9, 9);
150 g2.fill(ellipse);
151 }
152
153 g2.setStroke(bs1);
154 g2.setColor(BLACK);
155 for (int i = 0; i < pts.length; i++) {
156 ellipse.setFrame(pts[i].getX(), pts[i].getY(), 9, 9);
157 g2.draw(ellipse);
158 }
159 }
160
161 public static void main(String argv[]) {
162 createDemoFrame(new LineAnim());
163 }
164 }
|
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
38 import static java.awt.Color.PINK;
39 import static java.awt.Color.WHITE;
40 import java.awt.BasicStroke;
41 import java.awt.Color;
42 import java.awt.Graphics2D;
43 import java.awt.geom.AffineTransform;
44 import java.awt.geom.Ellipse2D;
45 import java.awt.geom.GeneralPath;
46 import java.awt.geom.Line2D;
47 import java.awt.geom.PathIterator;
48 import java.awt.geom.Point2D;
49 import java2d.AnimatingSurface;
50
51
52 /**
53 * Lines & Paths animation illustrating BasicStroke attributes.
54 */
55 @SuppressWarnings("serial")
56 public class LineAnim extends AnimatingSurface {
57
58 private static int[] caps = { BasicStroke.CAP_BUTT,
59 BasicStroke.CAP_SQUARE, BasicStroke.CAP_ROUND };
60 private static int[] joins = { BasicStroke.JOIN_MITER,
61 BasicStroke.JOIN_BEVEL, BasicStroke.JOIN_ROUND };
62 private static Color[] colors = { GRAY, PINK, LIGHT_GRAY };
63 private static BasicStroke bs1 = new BasicStroke(1.0f);
64 private static final int CLOCKWISE = 0;
65 private Line2D[] lines = new Line2D[3];
66 private int[] rAmt = new int[lines.length];
67 private int[] direction = new int[lines.length];
68 private int[] speed = new int[lines.length];
69 private BasicStroke[] strokes = new BasicStroke[lines.length];
70 private GeneralPath path;
71 private Point2D[] pts;
72 private float size;
73 private Ellipse2D ellipse = new Ellipse2D.Double();
74
75 public LineAnim() {
76 setBackground(WHITE);
77 }
78
79 @Override
80 public void reset(int w, int h) {
81 size = (w > h) ? h / 6f : w / 6f;
82 for (int i = 0; i < lines.length; i++) {
83 lines[i] = new Line2D.Float(0, 0, size, 0);
84 strokes[i] = new BasicStroke(size / 3, caps[i], joins[i]);
85 rAmt[i] = i * 360 / lines.length;
86 direction[i] = i % 2;
87 speed[i] = i + 1;
88 }
89
141 at.rotate(Math.toRadians(rAmt[i]));
142 g2.setStroke(strokes[i]);
143 g2.setColor(colors[i]);
144 g2.draw(at.createTransformedShape(lines[i]));
145 g2.draw(at.createTransformedShape(path));
146
147 int j = (int) ((double) rAmt[i] / 360 * pts.length);
148 j = (j == pts.length) ? pts.length - 1 : j;
149 ellipse.setFrame(pts[j].getX(), pts[j].getY(), 9, 9);
150 g2.fill(ellipse);
151 }
152
153 g2.setStroke(bs1);
154 g2.setColor(BLACK);
155 for (int i = 0; i < pts.length; i++) {
156 ellipse.setFrame(pts[i].getX(), pts[i].getY(), 9, 9);
157 g2.draw(ellipse);
158 }
159 }
160
161 public static void main(String[] argv) {
162 createDemoFrame(new LineAnim());
163 }
164 }
|