正多角形とその対角線を描写するプログラムをJavaで作ってみた。
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Diagonal extends JFrame{ public static void main(String[] args){ new Diagonal(); } public Diagonal(){ setSize(500,500); setTitle("Diagonal"); setDefaultCloseOperation(EXIT_ON_CLOSE); MyJPanel myJPanel= new MyJPanel(); Container c = getContentPane(); c.add(myJPanel); setVisible(true); } public class MyJPanel extends JPanel{ public static final int SIZE = 9; //正九角形 public MyJPanel(){ setBackground(Color.white); } public void paintComponent(Graphics g){ int x1,y1,x2,y2; double step; step = Math.PI*2/SIZE; for(double i=0; i<2 *Math.PI; i+=step){ x1 = (int)(200*Math.cos(i)); y1 = (int)(200*Math.sin(i)); for(double j=i+step; j<2*Math.PI; j+=step){ x2 = (int)(200*Math.cos(j)); y2 = (int)(200*Math.sin(j)); g.drawLine(250-y1,250-x1,250-y2,250-x2); } } } } }
0 件のコメント:
コメントを投稿