package proj4; import java.awt.Color; import java.awt.Graphics; import java.awt.Polygon; public class ColoredEquilateralTriangle extends ColoredShape { private int side; private Polygon triangle; public ColoredEquilateralTriangle(int x, int y, Color c, int side) { super(x, y, c); this.side = side; triangle = getPolygon(); } public void draw(Graphics g) { if (isFilled()) g.fillPolygon(triangle); else g.drawPolygon(triangle); } private Polygon getPolygon() { int x = getX(); int y = getY(); int height = (int)(side * Math.sqrt(3)/2.0 + 0.5); int[] xs = new int[3]; int[] ys = new int[3]; xs[0] = x; ys[0] = y + height; xs[1] = x + side/2; ys[1] = y; xs[2] = x + side; ys[2] = y + height; return new Polygon(xs, ys, 3); } public boolean inside(int x, int y) { return triangle.contains(x,y); } }