前提
javaで正多角形の描画のプログラムをやっています。正n角形のnの部分を入力するところでつまずいています。
実現したいこと
static Scanner stdIn = new Scanner(System.in);
System.out.println("正何角形?:");
static int n = stdIn.nextInt();
の部分で正多角形のnを入力してから、描画に移るプログラミングがしたいです。
n=9;などと置いてから実行した場合はできました。
発生している問題・エラーメッセージ
Exception in thread "main" java.lang.NoSuchMethodError: 'void DrawCanvas.<init>(int)' at Test2.main(Test1.java:29)
該当のソースコード
該当の言語
Java
1import javax.swing.*; 2import java.awt.Color; 3import java.awt.Graphics; 4import java.util.Scanner; 5 6public class Test1 { 7 8 public static void main(String[] args){ 9 Scanner stdIn = new Scanner(System.in); 10 int n; 11 12 while (true) { 13 System.out.println("正何角形?(3-999):"); 14 try { 15 n = stdIn.nextInt(); 16 if (3 <= n && n <= 999) break; 17 } catch (Exception ignored) { stdIn.next(); } 18 } 19 20 21 double a = n; 22 double b = 180/a; 23 double m = Math.toRadians(b); 24 double l = a*(Math.sin(m)); 25 System.out.println("正"+n+"角形の円周率の近似:"+l); 26 27 28 GameWindow gw = new GameWindow("円と正多角形",1000,1000); 29 gw.add(new DrawCanvas(n)); 30 gw.setVisible(true); 31 32 } 33} 34 35class DrawCanvas extends JPanel{ 36 private int n; 37 public DrawCanvas(int n) { 38 this.n = n; 39 SpinnerNumberModel model = new SpinnerNumberModel(n, 3,999, 1); 40 JSpinner spinner = new JSpinner(model); 41 spinner.addChangeListener(e -> { 42 this.n = (int) spinner.getValue(); 43 repaint(); 44 }); 45 add(spinner); 46 } 47 48 49 50 @Override public void paintComponent(Graphics g) { 51 super.paintComponent(g); 52 53 int x0 = 500; 54 int y0 = 500; 55 int r = 400; 56 int x[] = new int[n]; 57 int y[] = new int[n]; 58 59 double delta = 2 * ( Math.PI /(double)n ); 60 61 for(int i= 0; i<n; i++) { 62 x[i] = (int)(Math.cos(-Math.PI/2.0 + delta * i)*r) + x0; 63 y[i] = (int)(Math.sin(-Math.PI/2.0 + delta * i)*r) + y0; 64 } 65 g.setColor(Color.red); 66 g.drawPolygon(x, y, n); 67 68 } 69 public void paint(Graphics g) { 70 super.paint(g); 71 g.setColor(Color.blue); 72 g.drawOval(100, 100, 800, 800); 73 } 74 } 75 76class GameWindow extends JFrame{ 77 public GameWindow(String title, int width, int height) { 78 super(title); 79 setDefaultCloseOperation(EXIT_ON_CLOSE); 80 setSize(width,height); 81 setLocationRelativeTo(null); 82 setResizable(false); 83 84 } 85}
