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: // n is the number of points in our shape // int[] x = {x1, x2, x3, ... , xn}; // int[] y = {y1, y2, y3, ... , yn}; // g.fillPolygon(x, y, n); <-- for solid shapes // or // g.drawPolygon(x, y, n); <-- for outlined shapes // 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); int[] triangleX = { CENTER - sin, CENTER, CENTER + sin }; int[] triangleY = { CENTER + cos, CENTER + TRIANGLE_SIZE, CENTER + cos }; g.drawPolygon(triangleX, triangleY, 3); // 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); int[] pentagonX = { CENTER, CENTER + s1, CENTER + s2, CENTER - s2, CENTER - s1 }; int[] pentagonY = { CENTER - PENTAGON_SIZE, CENTER - c1, CENTER + c2, CENTER + c2, CENTER - c1 }; g.fillPolygon(pentagonX, pentagonY, 5); } }