java 小测试,求高手解决。关于画螺旋线的问题。

DesignaJavaappletfordrawingthespiralforagivenwindingnumber(thatis,thenumberoffullrota... Design a Java applet for drawing the spiral for a given winding number (that is, the number of full rotations) defined by the user. The equations defining a single rotation of the spiral are the following:
x = t·cos(2πt)
y = t·sin(2πt)
as t varies from 0 to 1. Here π = 3.14159... is the constant Math.PI. The applet should draw the spiral as soon as the user clicks the button and enters the winding number in the opened dialog box (must be a positive integer number). A spiral with 5 rotations is shown below. Try how it works.
Be sure to start the drawing of the spiral in the center of the yellowish area and scale the spiral by multiplying x and y by an appropriate value (depending on the number of full rotations) so that the spiral occupies the entire area of the panel.

Name the files Ex2_1.java and Ex2_1.html.
Hint: Divide the range of t, that is the interval [0, 1], with N points ti into equal parts (N should be choosen appropriately), compute the values of xi and yi at each point ti by using the above formulas, and draw the line segments between points (xi-1, yi-1) and (xi, yi) for i=1,2,...,N. For two rotations, t has to vary from 0 to 2, and so on. In this case you will need 2N subdivision points, N per rotation, to make the spiral smoother. Make sure to scale t so that its maximum value after W rotations is not W by the half of the applet width. Set the applet size 300x330 pixels.

(10 extra points) for animating the spiral drawing as it is shown below.
If you do the animated spiral, you do not need to design the static one above.
Click the button to start drawing.
第一个是要我们输入winding number 然后在网页显示螺旋的圈数。而附加题就是说功能是一样的,但是要加入同步时间的部分,也就是要随着系统时间的走动,显示出螺旋线形成的过程。
try {Thread.sleep(30);}
catch(InterruptedException e){}
repaint();
像这样。
用public void paint(Graphics g)
这个方法画图,非常感谢了哈
展开
 我来答
老冯文库
2011-04-22 · 知道合伙人软件行家
老冯文库
知道合伙人软件行家
采纳数:1139 获赞数:8734

向TA提问 私信TA
展开全部
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<num; n+=0.001){
x2 = (int)(n*Math.cos(2*Math.PI*n)*multiple)+150;
y2 = (int)(n*Math.sin(2*Math.PI*n)*multiple)+150;
g.drawLine(x1, y1, x2, y2);
x1 = x2;
y1 = y2;
}
}
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式