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 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.Arc2D; 37 import java.awt.geom.AffineTransform; 38 import java2d.AnimatingSurface; 39 import static java.awt.Color.*; 40 41 42 /** 43 * Arc2D Open, Chord & Pie arcs; Animated Pie Arc. 44 */ 45 @SuppressWarnings("serial") 46 public class Arcs extends AnimatingSurface { 47 48 private static String[] types = { "Arc2D.OPEN", "Arc2D.CHORD", "Arc2D.PIE" }; 49 private static final int CLOSE = 0; 50 private static final int OPEN = 1; 51 private static final int FORWARD = 0; 52 private static final int BACKWARD = 1; 53 private static final int DOWN = 2; 54 private static final int UP = 3; 55 private int aw, ah; // animated arc width & height 56 private int x, y; 57 private int angleStart = 45; 58 private int angleExtent = 270; 59 private int mouth = CLOSE; 60 private int direction = FORWARD; 61 62 public Arcs() { 63 setBackground(WHITE); 64 } 65 66 @Override 67 public void reset(int w, int h) { 68 x = 0; 69 y = 0; 70 aw = w / 12; 71 ah = h / 12; 72 } 73 74 @Override 75 public void step(int w, int h) { 76 // Compute direction 77 if (x + aw >= w - 5 && direction == FORWARD) { 78 direction = DOWN; 79 } 80 if (y + ah >= h - 5 && direction == DOWN) { 81 direction = BACKWARD; 82 } 83 if (x - aw <= 5 && direction == BACKWARD) { 84 direction = UP; 85 } 86 if (y - ah <= 5 && direction == UP) { 87 direction = FORWARD; 88 } 89 90 // compute angle start & extent 91 if (mouth == CLOSE) { 92 angleStart -= 5; 93 angleExtent += 10; 94 } 95 if (mouth == OPEN) { 96 angleStart += 5; 97 angleExtent -= 10; 98 } 99 if (direction == FORWARD) { 100 x += 5; 101 y = 0; 102 } 103 if (direction == DOWN) { 104 x = w; 105 y += 5; 106 } 107 if (direction == BACKWARD) { 108 x -= 5; 109 y = h; 110 } 111 if (direction == UP) { 112 x = 0; 113 y -= 5; 114 } 115 if (angleStart == 0) { 116 mouth = OPEN; 117 } 118 if (angleStart > 45) { 119 mouth = CLOSE; 120 } 121 } 122 123 @Override 124 public void render(int w, int h, Graphics2D g2) { 125 126 // Draw Arcs 127 g2.setStroke(new BasicStroke(5.0f)); 128 for (int i = 0; i < types.length; i++) { 129 Arc2D arc = new Arc2D.Float(i); 130 arc.setFrame((i + 1) * w * .2, (i + 1) * h * .2, w * .17, h * .17); 131 arc.setAngleStart(45); 132 arc.setAngleExtent(270); 133 g2.setColor(BLUE); 134 g2.draw(arc); 135 g2.setColor(GRAY); 136 g2.fill(arc); 137 g2.setColor(BLACK); 138 g2.drawString(types[i], (int) ((i + 1) * w * .2), (int) ((i + 1) * h 139 * .2 - 3)); 140 } 141 142 // Draw Animated Pie Arc 143 Arc2D pieArc = new Arc2D.Float(Arc2D.PIE); 144 pieArc.setFrame(0, 0, aw, ah); 145 pieArc.setAngleStart(angleStart); 146 pieArc.setAngleExtent(angleExtent); 147 AffineTransform at = AffineTransform.getTranslateInstance(x, y); 148 switch (direction) { 149 case DOWN: 150 at.rotate(Math.toRadians(90)); 151 break; 152 case BACKWARD: 153 at.rotate(Math.toRadians(180)); 154 break; 155 case UP: 156 at.rotate(Math.toRadians(270)); 157 } 158 g2.setColor(BLUE); 159 g2.fill(at.createTransformedShape(pieArc)); 160 } 161 162 public static void main(String[] argv) { 163 createDemoFrame(new Arcs()); 164 } 165 }