/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * JavaWorld Library, Copyright 2011 Bryan Chadwick * * * * FILE: ./image/RegularPolygon.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.Graphics; import java.awt.Polygon; import java.util.Arrays; /** * * * Represents an Image of a Regular Polygon, e.g., a Square, Pentagon, Octagon, etc. * *
 *    new RegularPolygon(50, 3, "outline", "red")
* *
* *
 *    new RegularPolygon(40, 4, "outline", "blue")
* *
* *
 *    new RegularPolygon(20, 8, "solid", "red")
* *
* */ public class RegularPolygon extends Image{ protected int width; protected int height; protected int mode; protected Color color; protected Polygon poly; /** Create a Regular Polygon Image with (double) radius, sides, mode and color */ public RegularPolygon(double radius, int sides, String mode, String color){ this(round(radius),sides,mode,color); } /** Create a Regular Polygon Image with (int) radius, sides, skip, mode and color */ public RegularPolygon(int radius, int sides, String mode, String color){ this(radius,sides,mode(mode),color(color)); } /** Create a Regular Polygon Star with converted mode and color */ protected RegularPolygon(int radius, int sides, int mode, Color color){ this(make(radius, sides), mode, color); } private static Polygon make(int radius, int sides){ Polygon poly = new Polygon(); double first = (sides%2==0)?Math.PI/sides:3*Math.PI/2, mult = 2*Math.PI/sides; for(int i = 0; i < sides; i++){ poly.addPoint(round(radius*Math.cos(first+i*mult)), round(radius*Math.sin(first+i*mult))); } return poly; } /** Create a Regular Polygon Star with converted mode and color */ protected RegularPolygon(Polygon poly, int mode, Color color){ super(0,0); this.mode = mode; this.color = color; this.poly = poly; java.awt.Rectangle r = poly.getBounds(); this.width = r.width; this.height = r.height; this.pinholeX = this.width/2; this.pinholeY = this.height/2; poly.translate(-(r.x+(r.width+r.x))/2, -(r.y+(r.height+r.y))/2); } /** Paint this Scene into the given graphics */ public void paint(Graphics g, int x, int y){ g.setColor(this.color); Polygon t = new Polygon(Arrays.copyOf(this.poly.xpoints, this.poly.npoints), Arrays.copyOf(this.poly.ypoints, this.poly.npoints), this.poly.npoints); t.translate(x, y); if(this.mode == OUTLINE)g.drawPolygon(t); else g.fillPolygon(t); } /** Return the Width of this Image */ public int width(){ return this.width; } /** Return the Height of this Image */ public int height(){ return this.height; } }