package com.lym.UI.StarComponents;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.lym.Util.StarToolkit;
/**
 * 璇ョ被鏄樉绀轰竴涓复鏃剁殑鎻愮ず淇℃伅锛岄渶瑕佽嚜宸辫皟鐢╯etVisable鏂规硶
 * @author Administrator
 *
 */
public class StarOptionFrame extends JFrame{
	//鐢ㄤ簬鐩存帴娴嬭瘯锛屽彲浠ュ垹闄?
	public static void main(String[] args) {
		new StarOptionFrame("娴嬭瘯",StarOptionFrame.ErrorMessage).setVisible(true);
	}
	
	private static final long serialVersionUID = 1L;

	static final int DefaultCloseTime = 4000;//榛樿鍏抽棴鐨勭獥鍙ｇ殑鏃堕棿
	public static final int NormalMessage = 1;
	public static final int WarnMessage = 2;
	public static final int ErrorMessage = 3;
	public static final int OKMessage = 4;
	
	static final int defaultWidth = 1200;
	static final int defaultHeight = 100;
	
	static final String HappyExpression = "( ^_^ ) ";
	static final String CryExpression = "o(鈺ワ箯鈺?)o ";
	static final String NotHappyExpression = "(鈯檁鈯?) ";
	int Color_R = 0;
	int Color_G = 0;
	int Color_B = 0;
	int PanelAlpha =  200;//鍒濆鐨勯?忔槑搴?
	Color PanelColor = new Color(Color_R,Color_G,Color_B,PanelAlpha);//榛樿鐨勯鑹?
	
	int MessageType = NormalMessage;
	
	//璁剧疆涓虹被鍙橀噺锛屾柟渚夸互鍚庡璇ョ獥浣撶殑鎵╁睍
	JPanel DefaultBackGroudPanel;
	JLabel MessageLabel;
	
	public StarOptionFrame(String message,int MessageType) {
		this(message);
		switch(MessageType) {
			case NormalMessage:
				break;
			case WarnMessage:
				Color_R = 255;
				Color_G = 255;
				Color_B = 30;
				DefaultBackGroudPanel.setBackground(new Color(Color_R,Color_G,Color_B,PanelAlpha));
				MessageLabel.setText(NotHappyExpression + MessageLabel.getText());
				break;
			case ErrorMessage:
				Color_R = 255;
				DefaultBackGroudPanel.setBackground(new Color(Color_R,Color_G,Color_B,PanelAlpha));
				MessageLabel.setText(CryExpression + MessageLabel.getText());
				break;
			case OKMessage:
				Color_R = 50;
				Color_G = 200;
				Color_B = 50;
				DefaultBackGroudPanel.setBackground(new Color(Color_R,Color_G,Color_B,PanelAlpha));
				MessageLabel.setText(HappyExpression + MessageLabel.getText());
				break;
		}
	}
	//鑷畾涔夐暱搴?
	public StarOptionFrame(String message,boolean DesignMessageLength,int length) {
		this(message);
		
	}
	
	public StarOptionFrame(String message){
		
		this.setSize(defaultWidth, defaultHeight);
		StarToolkit.setJFrameOnCenter(this);//璁剧疆灞呬腑鏄剧ず
		
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setResizable(false);//璁剧疆涓嶅彲鏀瑰彉澶у皬
		this.setUndecorated(true);//鍘绘帀杈规
		this.setBackground(new Color(0,0,0,0));//璁剧疆frame涓洪?忔槑
		this.setIconImage((new ImageIcon("imge/StarICON.png")).getImage());//璇ヨ蒋浠剁粺涓?鐨勫浘鏍囷紝灏界涓嶅睍绀?
		this.setType(JFrame.Type.UTILITY);//涓嶅皢璇frame鍦ㄤ换鍔℃爮涓婂睍绀?
		this.setAlwaysOnTop(true);//鎬绘槸鍦ㄦ渶鍓?
		this.addMouseListener(new MouseAdapter() {//鐐瑰嚮鍗冲彲鍏抽棴
			public void mouseClicked(MouseEvent e) {
				close();
			}
		});
		

		
		DefaultBackGroudPanel = new JPanel(new BorderLayout());//鍗婇?忔槑闈㈡澘
		DefaultBackGroudPanel.setBackground(new Color(0,0,0,PanelAlpha));
		this.add(DefaultBackGroudPanel,BorderLayout.CENTER);//灏嗛潰鏉挎斁缃簬jframe鐨勬渶涓棿锛岄粯璁ゅ崰婊rame鐨勫ぇ灏?
		
		MessageLabel = new JLabel(message);//鎻愮ず淇℃伅
		MessageLabel.setFont(new Font("榛戜綋", Font.BOLD, 40));
		MessageLabel.setForeground(Color.white);
		MessageLabel.setHorizontalAlignment(JTextField.CENTER);//瀛椾綋灞呬腑鏄剧ず
		DefaultBackGroudPanel.add(MessageLabel,BorderLayout.CENTER);//灏哃abel鍔犲埌Panel涓苟灞呬腑鏄剧ず
		
	}
	/**
	 * 榛樿鐨勫叧闂柟寮忥細娣″嚭
	 */
	private void close() {//鑷畾涔夌獥鍙ｅ叧闂殑鏂瑰紡(鎱㈡參娣″嚭)
		new Thread(new Runnable() {
			public void run() {
				try {
					int alp = PanelAlpha;
						for(;alp>0;alp -= 5) {
							Thread.sleep(15);
							DefaultBackGroudPanel.setBackground(new Color(Color_R,Color_G,Color_B,alp));
							DefaultBackGroudPanel.setVisible(false);
							DefaultBackGroudPanel.setVisible(true);
					}
				}catch(Exception e) {}
				StarOptionFrame.this.dispose();//鍏抽棴绐楀彛
			}
		}).start();
	}
	

	@Override
	public void setVisible(boolean visable) {
		super.setVisible(visable);
		if(visable)ColseByTime();
	}
	public void setVisible(boolean visable,int showTime) {
		this.setVisible(visable);
		if(visable)ColseByTime(showTime);
		else this.dispose();//濡傛灉涓嶆樉绀哄垯鐩存帴閲婃斁璧勬簮
	}
	

	private void ColseByTime() {
		ColseByTime(DefaultCloseTime);
	}
	private void ColseByTime(int closeTime) {
		new Thread(new Runnable() {//榛樿鍦?4绉掗挓鍚庡叧闂?
			public void run() {
				try {
					Thread.sleep(closeTime);
				} catch (InterruptedException e) {
				}
				close();
			}
		}).start();
	}
}
