黑马程序员java实现的简单的月亮绕地球,地球绕太阳转动_技术成长笔记_新浪博客...

---------------------- android培训java培训、期待与您交流!----------------------
import java.awt.*;
public class Mycanvas extends Canvas
{
    int r;
    Color c;
    public void setColor (Color c)
    {this.c=c;
    }
    public void setR(int r)
    {this.r=r;
    }
    public void paint(Graphics g)
    {g.setColor(c);
    g.fillOval(0, 0, 2*r, 2*r);
    }
    public int getR()
    {return r;
    }

}



=============================================
import java.awt.*;
public class Planet extends Panel implements Runnable{
Thread moon;
Mycanvas yellowBall;
double pointX[]=new double[360],
pointY[]=new double[360];
int w=100,h=100;
int radius=30;
Planet()
{
    setSize(w,h);
    setLayout(null);
yellowBall=new Mycanvas();
yellowBall.setColor(Color.yellow);
add(yellowBall);
yellowBall.setSize(12,12);
yellowBall.setR(12/2);
pointX[0]=0;
pointY[0]=-radius;
double angle=1*Math.PI/180;
for(int i=0;i<359;i++)
{
    pointX[i+1]=pointX[i]*Math.cos(angle)-Math.sin(angle)*pointY[i];
    pointY[i+1]=pointY[i]*Math.cos(angle)+pointX[i]*Math.sin(angle);
}
for(int i=0;i<360;i++)
{
    pointX[i]=pointX[i]+w/2;
    pointY[i]=pointY[i]+h/2;
}
yellowBall.setLocation((int)pointX[0]-yellowBall.getR(),(int)pointY[0]-yellowBall.getR());
moon=new Thread(this);

}
public void start()
{
    try{moon.start();
    }
    catch(Exception exe){}
}
public void paint(Graphics g)
{g.setColor(Color.blue);
g.fillOval(w/2-9, h/2-9, 18, 18);
}
public void run()
{
    int i=0;
    while(true)
    {i=(i+1)60;
    yellowBall.setLocation((int)pointX[i]-yellowBall.getR(),(int)pointY[i]-yellowBall.getR());
    try{ Thread.sleep(10);
    }
    catch(InterruptedException e){}
    }
}

}



=============================
import java.awt.*;
import java.awt.event.*;
public class HaveThreadFrame extends Frame implements Runnable
{
Thread rotate;
Planet earth;
double pointX[]=new double[360];
double pointY[]=new double[360];
int width,height;
int radius=120;
HaveThreadFrame()
{
    rotate=new Thread(this);
earth=new Planet();
setBounds(0,0,360,400);
width=getBounds().width;
height=getBounds().height;
pointX[0]=0;
pointY[0]=-radius;
double angle=1*Math.PI/180;
for(int i=0;i<359;i++)
{
    pointX[i+1]=pointX[i]*Math.cos(angle)-Math.sin(angle)*pointY[i];
    pointY[i+1]=pointY[i]*Math.cos(angle)+pointX[i]*Math.sin(angle);
}
for(int i=0;i<360;i++)
{ pointX[i]=pointX[i]+width/2;
  pointY[i]=pointY[i]+height/2;
}
setLayout(null);
setVisible(true);
validate();
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ System.exit(0);
}
}
);
add(earth);
earth.start();
rotate.start();
}
public void run()
{int i=0;
while(true)
{
    i=(i+1)60;
    earth.setLocation((int)pointX[i]-earth.getSize().width/2,
    (int)pointY[i]-earth.getSize().height/2);
    try{Thread.sleep(100);
    }
    catch(InterruptedException e){}
    }
}
public void paint(Graphics g)
{g.setColor(Color.red);
g.fillOval(width/2-15, height/2-15, 30, 30);
}
public static void main(String args[])
{
    new HaveThreadFrame();
}
}






黑马程序员java实现的简单的月亮绕地球,地球绕太阳转动

 
---------------------- android培训java培训、期待与您交流!----------------------
详情请查看: http://edu.csdn.net/heima

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值