C语言满天星加月亮

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Stars {
	public static void main(String[] args) {
	JFrame jf=new JFrame("星星");
	jf.setSize(1920,1080);    //屏幕分辨率大小。
	MyPanel mp=new MyPanel();
	//设置背景颜色
	mp.setBackground(Color.BLACK);
	jf.add(mp);
	jf.setVisible(true);

	}

}
class MyPanel extends JPanel{
	public void paint(Graphics g){
		super.paint(g);
		//设置星星颜色
		g.setColor(Color.YELLOW);
		//300个星星
		for (int i=0;i<300;i++){
		g.drawString("*",
				(int)(Math.random()*1920),
				(int)(Math.random()*1080));
		}
		//添加月亮
		g.fillOval(800, 100, 100, 100);
		g.setColor(Color.BLACK);
		g.fillOval(780, 80, 100, 100);
		//星星闪动
		try {
			Thread.sleep(500);        //让星星闪烁
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		repaint();
	}
}

你可能感兴趣的:(java,java)