package GraphicsPackage;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Star {
public static void main(String s[]) {
JFrame jframe = new JFrame();
StarPanel panel = new StarPanel();
jframe.setTitle("流星雨");
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.add(panel);
panel.startRun();
// panel.startRun1();
jframe.pack();
jframe.setVisible(true);
}
}
class StarPanel extends JPanel {
int[] x=new int[300];
int[] y=new int[300];
int z=500;
public StarPanel(){
for(int i=0;i<300;i++){
x[i]=(int)(Math.random()*1024);
y[i]=(int)(Math.random()*768);
}
}
public void paint(Graphics g){
super.paint(g);//画出一个空面板
this.setBackground(Color.BLACK);//设置面板背景的颜色
g.setColor(Color.WHITE);//设置画笔颜色
//画小星星
Font f = new Font("宋体",Font.BOLD,10);
g.setFont(f);
for(int i=0;i<100;i++)
g.drawString("☆", x[i],y[i]);
//画大星星
Font k = new Font("宋体",Font.BOLD,30);
g.setFont(k);
for(int i=0;i<30;i++)
g.drawString("☆", x[i], y[i]);
//画月亮
g.setColor(Color.RED);
g.fillArc(700, 120, 200, 200, 0, 360);
g.setColor(Color.BLACK);
// g.fillArc(z, 120,190, 190, 0, 360);
g.fillArc(650, 120,190, 190, 0, 360);
}
public void startRun(){
new Thread(){
public void run(){
while(true){
//流星数量
for(int i=0;i<13;i++){
y[i]++;
x[i]++;
if(y[i]>=768){
y[i]=0;
}
if(x[i]>=1024){
x[i]=0;
}
}
try {
Thread.sleep(1);
} catch (Exception e) {}
repaint();
}
}
}.start();
}
/*
public void startRun1(){
new Thread(){
public void run(){
while(true){
z++;
while(z>=690 && z<=710){
z++;
try {
Thread.sleep(500);
}catch (Exception e) {}
}
if(z>=1000)
z=500;
try {
Thread.sleep(100);
} catch (Exception e){
repaint();
}
}
}
}.start();
}*/
}
评论0