问答题
阅读下列程序,请写出该程序的功能。
import java.awt. * ;import javax.swing. *;
public class Test36 extends JFrame {
MyPanel p; double seta=0.0;
Test36(){
Container con=getContentPane();
con.setLayout (new BorderLayout ());
p=new MyPanel(); con.add(p, "Center");
setSize (500,400); setVisible (true);
}
public static void main(String[ ] args) {
new Test36();
}
class MyPanel extends JPanel implements Runnable {
double pi=3.14159, r1=150.0, r2=100.0;
Thread myThread=null;int seta;
MyPanel() {
seta=0;
if(myThread==null) {
myThread=new Thread (this); myThread.start();
}
}
public void run() {
while (myThread!=null){
seta=(seta+2)%360;
repaint();
try{Thread.sleep(20);
} catch(InterruptedException e) { }
}
}
public void paintComponent(Graphics g) {
super.paintComponent (g);
int x0=220+(int)(r1*Math.cos(pi/180.0*seta));
int y0=200+(int)(r2*Math.sin(pi/180.0*seta));
g.setColor(Color.red);
g.fillOval(x0,y0,10,10);
}
}
【参考答案】
一个直径为10个像素的红色圆点按顺时针方向在椭圆轨道上移动。