菜鳥成長第一天

NO.1

星星點燈

package com.qishi.day1;

import java.awt.*;

import javax.swing.*;
/**
 * @function 滿天星
 * @author hj
 * @date 2017年6月27日18:46:59
 *
 */
public class Star {
	/**
	 * 
		2、創建面板  
		3、設置面板的背景顏色
		4、面板上添加一顆星星,畫一個月亮
		5、擴展到300顆
		6、讓月亮動起來
	 */
	public static void main(String[] args) {
		//1、創建窗體
		JFrame jf = new JFrame();
		jf.setTitle("摘下星星送給你~");
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//jf.setBounds(100, 100, 200, 300);
		//jf.setSize(1366, 768);
		Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
		jf.setSize(d);
		jf.setBackground(Color.BLACK);
		
		MyPanel mp = new MyPanel();
		mp.setBackground(Color.BLACK);
		jf.getContentPane().add(mp);
		
		Thread th = new Thread(mp);
		th.start();
		
		jf.setVisible(true);
	
	}
}
class MyPanel extends JPanel implements Runnable{
	int x=700;
	int y=0;
	int n = 300;//星星的數量
	int [] starX = new int[n];
	int [] starY = new int[n];
	int [] starR = new int[n];
	int [] starG = new int[n];
	int [] starB = new int[n];
	
	public void paint(Graphics g){
		super.paint(g);//先擦除再畫
		for(int i =0;i<n;i++){
			//  0.0<=Math.random()*1000<1000.0
			starR[i]=(int) (Math.random()*256);
			starG[i]=(int) (Math.random()*256);
			starB[i]=(int) (Math.random()*256);
		}
		/*g.setColor(Color.red);
		g.setColor(new Color(n, n, n));*/
		for(int i = 0 ; i < n ; i++){
			g.setColor(new Color(starR[i],starG[i],starB[i]));
			g.setFont(new Font("",Font.BOLD,(int) (Math.random()*50)));
			g.drawString("曉靜", starX[i],starY[i]);
		}
		g.setColor(Color.BLUE);
		g.fillOval(800, 100, 100, 100);
		g.setColor(Color.BLACK);
		g.fillOval(x, y, 100, 100);
	}

	@Override
	public void run() {
		for(int i =0;i<n;i++){
			//  0.0<=Math.random()*1000<1000.0
			starX[i] =(int) (Math.random()*Toolkit.getDefaultToolkit().getScreenSize().getWidth());
			starY[i] =(int) (Math.random()*Toolkit.getDefaultToolkit().getScreenSize().getHeight()) ;
			
		}
		while(true){
			 x=x+1;
			 y=y+1;
			//starX[0] ++;
			repaint();
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章