import java.awt.*; public class Polygons { // Some constants to make formulas nicer public static final int CENTER = 300; public static final int PENTAGON_SIZE = 100; public static final int TRIANGLE_SIZE = 250; public static void main(String[] args) { DrawingPanel p = new DrawingPanel(CENTER * 2, CENTER * 2); Graphics g = p.getGraphics(); // Don't get tripped up by the math here. The important part is // creating arbitrary shapes with polygons. // Draws a Triangle int sin = (int)(Math.sin(Math.PI * 2 / 3) * TRIANGLE_SIZE); int cos = (int)(Math.cos(Math.PI * 2 / 3) * TRIANGLE_SIZE); Polygon tri = new Polygon(); tri.addPoint(CENTER - sin, CENTER + cos); tri.addPoint(CENTER, CENTER + TRIANGLE_SIZE); tri.addPoint(CENTER + sin, CENTER + cos); g.drawPolygon(tri); // Draws a Pentagon int s1 = (int)(1.0 / 4 * Math.sqrt(10 + 2 * Math.sqrt(5)) * PENTAGON_SIZE); int s2 = (int)(1.0 / 4 * Math.sqrt(10 - 2 * Math.sqrt(5)) * PENTAGON_SIZE); int c1 = (int)(1.0 / 4 * (Math.sqrt(5) - 1) * PENTAGON_SIZE); int c2 = (int)(1.0 / 4 * (Math.sqrt(5) + 1) * PENTAGON_SIZE); Polygon pent = new Polygon(); pent.addPoint(CENTER, CENTER - PENTAGON_SIZE); pent.addPoint(CENTER + s1, CENTER - c1); pent.addPoint(CENTER + s2, CENTER + c2); pent.addPoint(CENTER - s2, CENTER + c2); pent.addPoint(CENTER - s1, CENTER - c1); g.fillPolygon(pent); } }