SKY外语计算机学习
标题:
公转,自转
[打印本页]
作者:
流氓
时间:
2012-6-9 21:32
标题:
公转,自转
本帖最后由 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();
}
}
复制代码
效果图:
欢迎光临 SKY外语计算机学习 (http://join.skywj.com/)
Powered by Discuz! X2.5