- 生肖
- 马
- 星座
- 摩羯座
- 情感状态
- 光棍
- 性别
- 男
- 积分
- 373
- 积分
- 425
- 精华
- 2
- 阅读权限
- 30
- 注册时间
- 2012-4-29
- 最后登录
- 2013-10-11
- 帖子
- 62
- 生肖
- 马
- 星座
- 摩羯座
- 性别
- 男
|
本帖最后由 sky_yx 于 2015-12-30 14:11 编辑
HaveThreadFrame.java
- 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++)
- {
- }
- 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;
- (int)pointY[i]-earth.getSize().height/2);
- try{Thread.sleep(100);
- }
- catch(InterruptedException e){}
- }
- }
- public void paint(Graphics g)
- g.fillOval(width/2-15, height/2-15, 30, 30);
- }
- }
复制代码 Mycanvas.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.fillOval(0, 0, 2*r, 2*r);
- }
- public int getR()
- {return r;
- }
- }
复制代码 Planet.java
- 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();
- add(yellowBall);
- pointX[0]=0;
- pointY[0]=-radius;
- double angle=1*Math.PI/180;
- for(int i=0;i<359;i++)
- {
- }
- for(int i=0;i<360;i++)
- {
- pointX[i]=pointX[i]+w/2;
- pointY[i]=pointY[i]+h/2;
- }
- moon=new Thread(this);
- }
- public void start()
- {
- try{moon.start();
- }
- catch(Exception exe){}
- }
- public void paint(Graphics g)
- g.fillOval(w/2-9, h/2-9, 18, 18);
- }
- public void run()
- {
- int i=0;
- while(true)
- {i=(i+1)%360;
- try{ Thread.sleep(10);
- }
- catch(InterruptedException e){}
- }
- }
- }
复制代码 ThreadRotateMainClass.java
- public class ThreadRotateMainClass
- {
- public static void main(String args[])
- {
- new HaveThreadFrame();
- }
- }
复制代码 效果图:
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?立即注册
|