欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

java实现的简单的月亮绕地球,地球绕太阳转动,java地球,import java.

来源: javaer 分享于  点击 32374 次 点评:203

java实现的简单的月亮绕地球,地球绕太阳转动,java地球,import 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)%360;    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)%360;    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 class ThreadRotateMainClass{public static void main(String args[]){    new HaveThreadFrame();}}//该片段来自于http://byrx.net
相关栏目:

用户点评