设为首页收藏本站

SKY外语、计算机论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 4401|回复: 0

[其他] 公转,自转

[复制链接]

12

主题

5

好友

425

积分

中级会员

Rank: 3Rank: 3

生肖
星座
摩羯座
性别

最佳新人 活跃会员 灌水之王 论坛元老

发表于 2012-6-9 21:32:17 |显示全部楼层
本帖最后由 sky_yx 于 2015-12-30 14:11 编辑

HaveThreadFrame.java

  1. import java.awt.*;
  2. import java.awt.event.*;
  3. public class HaveThreadFrame extends Frame implements Runnable
  4. {
  5.     Thread rotate;
  6.     Planet earth;
  7.     double pointX[]=new double[360];
  8.     double pointY[]=new double[360];
  9.     int width,height;
  10.     int radius=120;
  11.     HaveThreadFrame()
  12.     {
  13.         rotate=new Thread(this);
  14.         earth=new Planet();
  15.         setBounds(0,0,360,400);
  16.         width=getBounds().width;
  17.         height=getBounds().height;
  18.         pointX[0]=0;
  19.         pointY[0]=-radius;
  20.         double angle=1*Math.PI/180;
  21.         for(int i=0;i<359;i++)
  22.         {
  23.         }
  24.         for(int i=0;i<360;i++)
  25.         { pointX[i]=pointX[i]+width/2;
  26.         pointY[i]=pointY[i]+height/2;
  27.         }
  28.         setLayout(null);
  29.         setVisible(true);
  30.         validate();
  31.         addWindowListener(new WindowAdapter()
  32.         { public void windowClosing(WindowEvent e)
  33.         { System.exit(0);
  34.         }
  35.         }
  36.         );
  37.         add(earth);
  38.         earth.start();
  39.         rotate.start();
  40.     }
  41.     public void run()
  42.     {int i=0;
  43.     while(true)
  44.     {
  45.         i=(i+1)%360;
  46.                 (int)pointY[i]-earth.getSize().height/2);
  47.         try{Thread.sleep(100);
  48.         }
  49.         catch(InterruptedException e){}
  50.     }
  51.     }
  52.     public void paint(Graphics g)
  53.     g.fillOval(width/2-15, height/2-15, 30, 30);
  54.     }
  55. }
复制代码
Mycanvas.java

  1. import java.awt.*;
  2. public class Mycanvas extends Canvas
  3. {
  4.     int r;
  5.     Color c;
  6.     public void setColor (Color c)
  7.     {this.c=c;
  8.     }
  9.     public void setR(int r)
  10.     {this.r=r;
  11.     }
  12.     public void paint(Graphics g)
  13.     g.fillOval(0, 0, 2*r, 2*r);
  14.     }
  15.     public int getR()
  16.     {return r;
  17.     }
  18. }
复制代码
Planet.java

  1. import java.awt.*;
  2. public class Planet extends Panel implements Runnable{
  3.     Thread moon;
  4.     Mycanvas yellowBall;
  5.     double pointX[]=new double[360],
  6.     pointY[]=new double[360];
  7.     int w=100,h=100;
  8.     int radius=30;
  9.     Planet()
  10.     {
  11.         setSize(w,h);
  12.         setLayout(null);
  13.         yellowBall=new Mycanvas();
  14.         add(yellowBall);
  15.         pointX[0]=0;
  16.         pointY[0]=-radius;
  17.         double angle=1*Math.PI/180;
  18.         for(int i=0;i<359;i++)
  19.         {
  20.         }
  21.         for(int i=0;i<360;i++)
  22.         {
  23.             pointX[i]=pointX[i]+w/2;
  24.             pointY[i]=pointY[i]+h/2;
  25.         }
  26.         moon=new Thread(this);
  27.     }
  28.     public void start()
  29.     {
  30.         try{moon.start();
  31.         }
  32.         catch(Exception exe){}
  33.     }
  34.     public void paint(Graphics g)
  35.     g.fillOval(w/2-9, h/2-9, 18, 18);
  36.     }
  37.     public void run()
  38.     {
  39.         int i=0;
  40.         while(true)
  41.         {i=(i+1)%360;
  42.         try{ Thread.sleep(10);
  43.         }
  44.         catch(InterruptedException e){}
  45.         }
  46.     }
  47. }
复制代码
ThreadRotateMainClass.java

  1. public class ThreadRotateMainClass
  2. {
  3.     public static void main(String args[])
  4.     {
  5.         new HaveThreadFrame();
  6.     }
  7. }
复制代码
效果图:

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

您需要登录后才可以回帖 登录 | 立即注册


手机版|SKY外语计算机学习 ( 粤ICP备12031577 )    

GMT+8, 2024-3-29 18:04 , Processed in 0.124127 second(s), 29 queries .

回顶部