import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SpiralJFrame extends JFrame implements ActionListener {
private SpiralCanvas canvas;
public SpiralJFrame()
{
super("阿基米德螺线");
Dimension dim=getToolkit().getScreenSize();
this.setBounds(dim.width/4,dim.height/4,dim.width/2,dim.height/2);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel jpanel=new JPanel();
this.getContentPane().add(jpanel, "North");
JButton button_color=new JButton("颜色选择");
jpanel.add(button_color);
button_color.addActionListener(this);
this.canvas=new SpiralCanvas(Color.red);
this.getContentPane().add(this.canvas, "Center");
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent arg0) {
Color c=JColorChooser.showDialog(this, "选择颜色", Color.blue);
this.canvas.setColor(c);
this.canvas.repaint();
}
public static void main(String arg[]){
new SpiralJFrame();
}
private SpiralCanvas canvas;
public SpiralJFrame()
{
super("阿基米德螺线");
Dimension dim=getToolkit().getScreenSize();
this.setBounds(dim.width/4,dim.height/4,dim.width/2,dim.height/2);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel jpanel=new JPanel();
this.getContentPane().add(jpanel, "North");
JButton button_color=new JButton("颜色选择");
jpanel.add(button_color);
button_color.addActionListener(this);
this.canvas=new SpiralCanvas(Color.red);
this.getContentPane().add(this.canvas, "Center");
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent arg0) {
Color c=JColorChooser.showDialog(this, "选择颜色", Color.blue);
this.canvas.setColor(c);
this.canvas.repaint();
}
public static void main(String arg[]){
new SpiralJFrame();
}
}
class SpiralCanvas extends Canvas {
private Color color;
public SpiralCanvas(Color color){
this.setColor(color);
}
public void setColor(Color color){
this.color=color;
}
public void paint(Graphics g){
int x0=this.getWidth()/2;
int y0=this.getHeight()/2;
g.setColor(this.color);
g.drawLine(x0, 0, x0, y0*2);
g.drawLine(0, y0, x0*2, y0);
double r;
for(int i=0;i<2000;i++){
r=0.1*i;
int x =(int) ( r * Math.cos(5*r*Math.PI/180));
int y = (int) (r * Math.sin(5*r*Math.PI/180));
g.fillOval(x0+x, y0+y, 2, 2);
}
}
}
private Color color;
public SpiralCanvas(Color color){
this.setColor(color);
}
public void setColor(Color color){
this.color=color;
}
public void paint(Graphics g){
int x0=this.getWidth()/2;
int y0=this.getHeight()/2;
g.setColor(this.color);
g.drawLine(x0, 0, x0, y0*2);
g.drawLine(0, y0, x0*2, y0);
double r;
for(int i=0;i<2000;i++){
r=0.1*i;
int x =(int) ( r * Math.cos(5*r*Math.PI/180));
int y = (int) (r * Math.sin(5*r*Math.PI/180));
g.fillOval(x0+x, y0+y, 2, 2);
}
}
}