import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Animation extends JFrame{ public Animation(){ //画面の設定 setSize(700,700); setTitle("Java Programing"); setDefaultCloseOperation(EXIT_ON_CLOSE); //パネルを貼り付ける MyJPanel myJPanel= new MyJPanel(); Container c = getContentPane(); //コンテナの取得 c.add(myJPanel); //パネルを貼る setVisible(true); } public static void main(String[] args){ new Animation(); } public class MyJPanel extends JPanel implements ActionListener{ JButton doSmaller,doBigger; double last_x, last_y, x,y, parameter_t; public MyJPanel(){ setBackground(Color.white); //setLayout(new GridLayout(2,10)); doSmaller = new JButton("Small"); doBigger = new JButton("Big"); parameter_t = 1.0; //parameter_tによって、螺旋を変化する   add(doSmaller); add(doBigger); doSmaller.addActionListener(this); doBigger.addActionListener(this); } //x座標の変換 public double x_change(double x, double width, double x_min, double x_max){ double new_x; new_x = (x - x_min)/(x_max - x_min) * width; return new_x; } //y座標の変換 public double y_change(double y, double height, double y_min, double y_max){ double new_y; new_y = (y - y_min)/(y_max - y_min) * height; new_y = height - new_y; return new_y; } public void paintComponent(Graphics g){ super.paintComponent(g); //画面クリアする。 Graphics2D g2 = (Graphics2D) g; g2.setStroke(new BasicStroke(10));//描画の線分の幅を調整する。 Dimension d; d = getSize(); //write axis g.setColor(new Color(255, 0, 0)); g.drawLine(0,d.height/2,d.width,d.height/2); g.drawLine(d.width/2,0,d.width/2,d.height); g.drawString("x",d.width-20,d.height/2+10); g.drawString("y",d.width/2+10,10); //螺旋を描く double theta, theta_step=2*Math.PI/50; double r, r_step = 0.01; double x,y; int px, py, last_px, last_py; Color rand_color; r=0.0;theta=0.0; last_px = (int) x_change(0.0, d.width, -1,1); last_py = (int) y_change(0.0, d.height, -1,1); for(int k=0; k<1000;k+=1){ theta = theta + theta_step; r = r+r_step * parameter_t; //parameter_tによって、螺旋の変化ができる。   x=Math.cos(theta)*r; y=Math.sin(theta)*r; if(Math.abs(x) >1 && Math.abs(y) >1) break; px = (int) x_change(x, d.width, -1,1); py = (int) y_change(y, d.height, -1,1); //色を乱数で設定する。 rand_color = new Color( (int)(Math.random()*256), (int)(Math.random()*256), (int)(Math.random()*256) ); g.setColor(rand_color); g.drawLine(last_px, last_py, px, py); last_px = px; last_py = py; } } public void actionPerformed(ActionEvent e) { if(e.getSource()==doSmaller) { parameter_t = parameter_t - 0.01; repaint(); } if(e.getSource()==doBigger) { parameter_t = parameter_t + 0.01; repaint(); } } } }