/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * JavaWorld Library, Copyright 2011 Bryan Chadwick * * * * FILE: ./image/Star.java * * * * This file is part of JavaWorld. * * * * JavaWorld is free software: you can redistribute it and/or * * modify it under the terms of the GNU General Public License * * as published by the Free Software Foundation, either version * * 3 of the License, or (at your option) any later version. * * * * JavaWorld is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with JavaWorld. If not, see . * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ package image; import java.awt.Color; import java.awt.Polygon; /** * * * Represents an Image of a Star. There are several constructors to support customization * with number of points, and inner/outer radius. * *
 *    new Star(40, 5, "solid", "gray")
* *
* *
 *    new Star(30, 7, "outline", "red")
* *
* *
 *    new Star(40, 30, 10, "solid", "cornflowerblue")
* *
* */ public class Star extends RegularPolygon{ private static double RadScale = 0.4; /** Create a Star Image with (double) radius, mode and color */ public Star(double radius, String mode, String color){ this(radius,radius*RadScale,5,mode,color); } /** Create a Star Image with (int) radius, mode and color */ public Star(int radius, String mode, String color){ this(radius,radius*RadScale,5,mode,color); } /** Create a Star Image with (double) radius, sides, mode and color */ public Star(double radius, int sides, String mode, String color){ this(radius,radius*RadScale,sides,mode,color); } /** Create a Star Image with (int) radius, sides, mode and color */ public Star(int radius, int sides, String mode, String color){ this(radius,radius*RadScale,sides,mode,color); } /** Create a Star Image with (double) radius, inner-radius, sides, mode and color */ public Star(double radius, double innerRad, int sides, String mode, String color){ this(round(radius),round(innerRad),sides,mode,color); } /** Create a Star Image with (int) radius, inner-radius, sides, mode and color */ public Star(int radius, int innerRad, int sides, String mode, String color){ this(radius,innerRad,sides,mode(mode),color(color)); } /** Create a Star with converted mode and color */ protected Star(int radius, int innerRad, int sides, int mode, Color color){ super(make(radius, innerRad, sides), mode, color); } private static Polygon make(int radius, double inrad, int sides){ Polygon poly = new Polygon(); double first = 3*Math.PI/2, mult = Math.PI/sides; for(int i = 0; i < sides*2; i+=2){ poly.addPoint(round(radius*Math.cos(first+i*mult)), round(radius*Math.sin(first+i*mult))); poly.addPoint(round(inrad*Math.cos(first+(i+1)*mult)), round(inrad*Math.sin(first+(i+1)*mult))); } return poly; } }