JAVA图形小动画之简单行星运动

//第一个java文件thread_star.java:

package solarsystem;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;

import javax.swing.JPanel;

class thread_star extends Thread
{
int x0;
int y0;
int r0;
int d0;
double angle;
thread_star(int x,int y,int r,double a)
{
x0=x;
y0=y;
r0=r;
d0=x0-960;
angle=a;
}
public void run()
{double an=angle/3;
while(true)
{
x0=(int) (960+d0*Math.cos(angle));
y0=(int) (540+d0*Math.sin(angle));

angle=angle+an/10;
try {
sleep(1);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class draw_star extends JPanel
{
thread_star s[]={new thread_star(1300,540,30,(Math.PI/20)),new thread_star(1700,540,40,(Math.PI/40))
,new thread_star(1400,540,25,(Math.PI/30)),new thread_star(1620,540,30,(Math.PI/36)),
new thread_star(1180,540,18,(Math.PI/10))
};
draw_star()
{
for(int i=0;i<s.length;i++)s[i].start();
this.setBackground(Color.black);
}
public void paint(Graphics g)
{
g.setColor(Color.black);
g.fillRect(0, 0, getWidth(), getHeight());
//this.setBackground(Color.BLACK);
super.paint(g);


}
public void paintComponent(Graphics g0)
{
this.setBackground(Color.black);
Graphics2D g=(Graphics2D)(g0);
g.setColor(Color.yellow);
g.fill(new Ellipse2D.Double(960-80,540-80,160,160));
Color c[]={Color.blue,Color.GRAY,Color.orange,Color.RED,Color.red};
for(int i=0;i<s.length;i++)
{
g=(Graphics2D)(g0);
g.setColor(c[i]);
g.fill(new Ellipse2D.Double(s[i].x0-s[i].r0,s[i].y0-s[i].r0,2*s[i].r0,2*s[i].r0));
}
repaint();
}

}

//第二个java文件J_Frame.java:

package solarsystem;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;

import javax.swing.JFrame;

public class J_Frame extends JFrame
{
J_Frame()
{
super("太阳星系图");
draw_star d=new draw_star();
Container c=getContentPane();
c.setBackground(Color.BLACK);
c.add(d);
this.setBackground(Color.black);
}
public static void main(String args[])
{

J_Frame j=new J_Frame();
j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
j.setSize(1920, 1080);
j.setVisible(true);
}
}

运行结果图示:

  • 3
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值