/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * AndroidWorld Library, Copyright 2011 Bryan Chadwick * * * * FILE: ./android/image/RegularPolygon.java * * * * This file is part of AndroidWorld. * * * * AndroidWorld 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. * * * * AndroidWorld 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 AndroidWorld. If not, see . * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ package android.image; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.Path; import android.graphics.RectF; /** * * * Represents an Image of a Regular Polygon. * *
 *    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 Paint paint; protected Path path; /** 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, int color){ this(make(radius, sides), mode, color); } private static Path make(int radius, int sides){ Path poly = new Path(); double first = (sides%2==0)?Math.PI/sides:3*Math.PI/2, mult = 2*Math.PI/sides; poly.moveTo((int)(radius*Math.cos(first)), (int)(radius*Math.sin(first))); for(int i = 1; i < sides; i++){ poly.lineTo((int)(radius*Math.cos(first+i*mult)), (int)(radius*Math.sin(first+i*mult))); } poly.close(); return poly; } /** Create a Regular Polygon Star with converted mode and color */ protected RegularPolygon(Path poly, int mode, int color){ super(0,0); this.mode = mode; this.paint = painter(color,mode); this.path = poly; RectF r = new RectF(); poly.computeBounds(r, false); width = (int)(r.right-r.left); height = (int)(r.bottom-r.top); pinholeX = width/2; pinholeY = height/2; path.offset(-(r.left+(width+r.left))/2, -(r.top+(height+r.top))/2); } /** Paint this Polygon into the given graphics */ public void paint(Canvas c, int x, int y){ Path t = new Path(path); t.offset(x, y); c.drawPath(t, paint); } /** Return the Width of this Image */ public int width(){ return width; } /** Return the Height of this Image */ public int height(){ return height; } }