共回答了26个问题采纳率:100%
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Ex2_1 extends JApplet implements ActionListener {
public static JButton btn;
public static JPanel pnl;
public static JDialog dlg;
public static int num;
public static JTextField txtNum;
public static JButton btnEnter;
public static JButton btnCancel;
public Ex2_1(){
super();
btn = new JButton("draw spiral");
pnl = new JPanel();
pnl.setBackground(Color.YELLOW);
this.setLayout(new BorderLayout());
add("North", btn);
add("Center",pnl);
btn.addActionListener(this);
this.setSize(300, 330);
setVisible(true);
}
public void actionPerformed(ActionEvent event){
JButton btn = (JButton)event.getSource();
if(btn.getText().equals("draw spiral")){
dlg = new JDialog();
txtNum = new JTextField(20);
btnEnter = new JButton("确定");
btnCancel = new JButton("取消");
dlg.setLayout(new FlowLayout());
dlg.setTitle("输入");
dlg.add(new JLabel("Enter the winding number"));
dlg.add(txtNum);
dlg.add(btnEnter);
dlg.add(btnCancel);
btnEnter.addActionListener(this);
btnCancel.addActionListener(this);
dlg.setVisible(true);
dlg.setSize(250, 150);
dlg.setResizable(false);
}
else{
if(btn.getText().equals("确定")){
num = Integer.parseInt(txtNum.getText().trim());
dlg.dispose();
draw();
}
else if(btn.getText().equals("取消")){
dlg.dispose();
}
}
}
public void draw(){
Graphics g = pnl.getGraphics();
pnl.update(g);
int CX = 150, CY = 150; //原点坐标
double multiple; //倍数
double n;
int x1, y1, x2, y2;
multiple = 150.0 / num;
x1 = (int)(0*Math.cos(2*Math.PI*0)*multiple)+150;
y1 = (int)(0*Math.sin(2*Math.PI*0)*multiple)+150;
for(n=0.001; n
1年前
1