Java examples for 2D Graphics:Area
perimeter of Area
//package com.java2s; import static java.lang.Double.NaN; import java.awt.geom.*; public class Main { private static final AffineTransform identity = new AffineTransform(); public static double perimeter(GeneralPath gp) { return perimeter(gp.getPathIterator(identity)); }/* w w w .j a va 2s . com*/ public static double perimeter(Area area) { PathIterator i = area.getPathIterator(identity); return perimeter(i); } private static double perimeter(PathIterator i) { double perimeter = 0.0; double[] coords = new double[6]; double startX = NaN, startY = NaN; Line2D segment = new Line2D.Double(NaN, NaN, NaN, NaN); while (!i.isDone()) { int segType = i.currentSegment(coords); double x = coords[0], y = coords[1]; switch (segType) { case PathIterator.SEG_CLOSE: segment.setLine(segment.getX2(), segment.getY2(), startX, startY); perimeter += length(segment); startX = startY = NaN; segment.setLine(NaN, NaN, NaN, NaN); break; case PathIterator.SEG_LINETO: segment.setLine(segment.getX2(), segment.getY2(), x, y); perimeter += length(segment); break; case PathIterator.SEG_MOVETO: startX = x; startY = y; segment.setLine(NaN, NaN, x, y); break; default: throw new IllegalArgumentException( "PathIterator contains curved segments"); } i.next(); } if (Double.isNaN(perimeter)) throw new IllegalArgumentException( "PathIterator contains an open path"); return perimeter; } private static double length(Line2D segment) { double x = segment.getX1() - segment.getX2(); double y = segment.getY1() - segment.getY2(); return Math.sqrt(x * x + y * y); } }