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 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.Arcs_Curves; 33 34 35 import java.awt.*; 36 import java.awt.geom.Ellipse2D; 37 import java2d.AnimatingSurface; 38 import static java.awt.Color.*; 39 40 41 /** 42 * Ellipse2D 25 animated expanding ellipses. 43 */ 44 @SuppressWarnings("serial") 45 public final class Ellipses extends AnimatingSurface { 46 47 private static Color colors[] = { 48 BLUE, CYAN, GREEN, MAGENTA, ORANGE, PINK, RED, 49 YELLOW, LIGHT_GRAY, WHITE }; 50 private Ellipse2D.Float[] ellipses; 51 private double esize[]; 52 private float estroke[]; 53 private double maxSize; 54 55 public Ellipses() { 56 setBackground(BLACK); 57 ellipses = new Ellipse2D.Float[25]; 58 esize = new double[ellipses.length]; 59 estroke = new float[ellipses.length]; 60 for (int i = 0; i < ellipses.length; i++) { 61 ellipses[i] = new Ellipse2D.Float(); 62 getRandomXY(i, 20 * Math.random(), 200, 200); 63 } 64 } 65 66 public void getRandomXY(int i, double size, int w, int h) { 67 esize[i] = size; 68 estroke[i] = 1.0f; 69 double x = Math.random() * (w - (maxSize / 2)); 70 double y = Math.random() * (h - (maxSize / 2)); 71 ellipses[i].setFrame(x, y, size, size); 72 } 73 74 @Override 75 public void reset(int w, int h) { 76 maxSize = w / 10; 77 for (int i = 0; i < ellipses.length; i++) { 78 getRandomXY(i, maxSize * Math.random(), w, h); 79 } 80 } 81 82 @Override 83 public void step(int w, int h) { 84 for (int i = 0; i < ellipses.length; i++) { 85 estroke[i] += 0.025f; 86 esize[i]++; 87 if (esize[i] > maxSize) { 88 getRandomXY(i, 1, w, h); 89 } else { 90 ellipses[i].setFrame(ellipses[i].getX(), ellipses[i].getY(), 91 esize[i], esize[i]); 92 } 93 } 94 } 95 96 @Override 97 public void render(int w, int h, Graphics2D g2) { 98 for (int i = 0; i < ellipses.length; i++) { 99 g2.setColor(colors[i % colors.length]); 100 g2.setStroke(new BasicStroke(estroke[i])); 101 g2.draw(ellipses[i]); 102 } 103 } 104 105 public static void main(String argv[]) { 106 createDemoFrame(new Ellipses()); 107 } 108 }