Tue Nov 11, 2008 8:42 pm
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
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();
}
}
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.