public class DrawPolygon extends JPanel { public void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(Color.red); Polygon polygon1= new Polygon(); for (int i = 0; i < 3; i++){ polygon1.addPoint((int) (40 + 50 * Math.cos(i * 2 * Math.PI / 3)), (int) (150 + 50 * Math.sin(i * 2 * Math.PI / 3))); } g.drawPolygon(polygon1);
Polygon polygon2= new Polygon(); for (int i = 0; i < 6; i++){ polygon2.addPoint((int) (160 + 50 * Math.cos(i * 2 * Math.PI / 6)), (int) (150 + 50 * Math.sin(i * 2 * Math.PI / 6))); } g.drawPolygon(polygon2);
Polygon polygon3 = new Polygon();
for (int i = 0; i < 360; i++) { double value = i / 360.0; polygon3.addPoint((int) (290 + 50 * value * Math.cos(8 * value * Math.PI)), (int) (150 + 50 * value * Math.sin(8 * value * Math.PI))); } g.drawPolygon(polygon3); }
public static void main(String[] args) { JFrame frame = new JFrame(); frame.setTitle("Show Different Polygons"); frame.setSize(350, 250); Container contentPane = frame.getContentPane(); contentPane.add(new DrawPolygon()); frame.show(); } }
Attachments:
File comment: draw polygon in Java polygon.gif [ 6.24 KiB | Viewed 7535 times ]
_________________ Currenlty programming with : java , html , php , and javascript . (OCJP-6 certified )