build Star by Area - Java 2D Graphics

Java examples for 2D Graphics:Shape

Description

build Star by Area

Demo Code


    //package com.java2s;

    import java.awt.geom.AffineTransform;
    import java.awt.geom.Area;
    import java.awt.geom.Path2D;

    import java.util.stream.IntStream;

    public class Main {
        static final Area star = new Area();

        private static void buildStar() {
    final double ratio = (3 - Math.sqrt(5)) / 2.0;
    final double angle = 2 * Math.PI / 5;
    Path2D.Double tri = new Path2D.Double();
    tri.moveTo(0, -1);/*from w  ww  .jav a 2 s .c o  m*/
    tri.lineTo(Math.cos(Math.PI / 10) * ratio,
            Math.sin(Math.PI / 10) * ratio);
    tri.lineTo(-Math.cos(Math.PI / 10) * ratio,
            Math.sin(Math.PI / 10) * ratio);
    tri.closePath();
    IntStream.range(0, 5)
            .mapToObj((int i)
                    -> AffineTransform.getRotateInstance(i * angle))
            .map((AffineTransform at) -> at.createTransformedShape(tri))
            .map(s -> new Area(s))
            .forEach(star::add);
}
    }

Related Tutorials