本帖最后由 stargc 于 2011-11-21 11:27:07 编辑 让一个圆形 按照一个椭圆的轨迹运动!类似地球公转的样子!
最好是代码加解释!我是新手 直接说理论听不懂的!只求会做!
多谢 分享 先试试看我也想实现轨迹回放的功能。。。该回复于2011-11-22 15:49:05被版主删除额!没人回呢!还好我弄出来了!自己给自己回答吧!大家共同进步!
package Test;
import java.awt.*;
import java.awt.event.*;
public class smp91 extends Frame
{
double a=0;
int x,y;
Myca ca1;
Myca ca2;
public smp91()
{
setSize(800,600);
setBackground(Color.white);
setVisible(true);
cashape cas=new cashape();
ca1=new Myca(new Point(400,300),30,Color.yellow);
ca2=new Myca(new Point(300,200),20,Color.blue);
add(ca1);
add(ca2);
cas.start();
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
}
class cashape extends Thread
{
public void run()
{
while(true)
{
x=(int)(300*Math.cos(a)+400);
y=(int)(200*Math.sin(a)+300);
ca2.setLocation(x, y);
a+=0.00001;
}
}
}
public static void main(String[] args)
{
Frame frm=new smp91();
}
}
class Myca extends Canvas
{
Point p=new Point(0,0);
int r;
Color c=Color.blue;
Myca(Point pt,int ra,Color col)
{
p=pt;
r=ra;
c=col;
setBackground(Color.white);
setBounds(pt.x-r,pt.y-r,2*r,2*r);
}
public void paint(Graphics g)
{
int ra=r-1*r/10;
int rb=r-1*r/5;
g.setColor(c);
g.fillOval(0,0,2*ra,2*rb);
}
}