import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.border.BevelBorder;

/***************************************************************
背景色の異なるラベルを縦に並べて設置しクリックされたラベルの背景色を取得。<br>
取得したラベルの背景色にフレームの背景色を変更。再度画面をクリックすると最初の画面に戻る。
****************************************************************/
public class ColorSelect extends JFrame {
	private static final long serialVersionUID = 1L;
	private JPanel cardPanel;
	private JLabel lblCard2;
	
	ColorSelect() {
		// メインフレームの初期化
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setTitle("ColorSelect");
		this.setBounds(200, 200, 480, 600);
		this.setResizable(false);
		this.setLayout(new CardLayout());
		
		// カードレイアウトで切り替える部分
		cardPanel = new JPanel();
		cardPanel.setLayout(new CardLayout());
		
		// 初期状態のカード（card1）
		JPanel card1 = new JPanel();
		card1.setLayout(new BoxLayout(card1, BoxLayout.Y_AXIS));
		//card1.setLayout(new GridLayout(3, 3));
		addLabel(card1);
		card1.setName("card1");
		
		// 切り替え用のカード（card2）
		JPanel card2 = new JPanel();
		lblCard2 = new JLabel("Test fields on Card2", JLabel.CENTER);
		card2.setLayout(new BorderLayout());
		initLabelDefault(lblCard2, Color.BLACK);
		card2.setName("card2");
		card2.add(lblCard2, BorderLayout.CENTER);
		
		JTextArea txtInfo = new JTextArea("下の画面を左クリックすると前の画面へ戻ります。\n右クリックすると＃付きカラーコードをクリップボードへコピーします。");
		txtInfo.setFont(new Font("メイリオ", Font.BOLD, 12));
		txtInfo.setEditable(false);
		card2.add(txtInfo,BorderLayout.NORTH);
		
		cardPanel.add(card1, "card1");
		cardPanel.add(card2, "card2");
		
		// コンテントペインにパネルを追加
		getContentPane().add(cardPanel, BorderLayout.CENTER);
			
	}

	// ラベルの追加処理
	private void addLabel(JPanel pnl) {
		// RGB各カラーの値を格納（赤系色、緑系色、青系色を各3種ずつ）
		int[] intR = {255, 255, 255, 
				0, 153, 255,
				0, 0, 0};	// RGB 赤色
		int[] intG = {0, 0, 0, 
				255, 255, 255,
				0, 153, 255};	// RGB 緑色
		int[] intB = {0, 153, 255,
				0, 0, 0,
				255, 255, 255};	// RGB 青色
		String strHexColor;	// カラーコード（16進数）
		
		for (int i = 0; i < intR.length; i++){
			JLabel lbl = new JLabel("", JLabel.CENTER);
			initLabelDefault(lbl, new Color(intR[i], intG[i], intB[i]));	// ラベルの初期設定
			
			// カラーコード（RGBを各職ごとに16真数に変換。例：255→FF）
			strHexColor =  (Integer.toHexString(intR[i] >>> 4) + Integer.toHexString(intR[i] & 0xF)
					+ Integer.toHexString(intG[i] >>> 4) + Integer.toHexString(intG[i] & 0xF)
					+ Integer.toHexString(intB[i] >>> 4) + Integer.toHexString(intB[i] & 0xF)).toUpperCase();
			
			lbl.setText("#" + strHexColor);
			lbl.setForeground(new Color(~intR[i]&255, ~intG[i]&255, ~intB[i]&255)); // 背景色を反転して文字色に設定
			lbl.setBorder(new BevelBorder(BevelBorder.RAISED, Color.WHITE, Color.BLACK));
			pnl.add(lbl, BorderLayout.CENTER);
		}
	}
	
	// ラベルの初期設定（共通）
	private void initLabelDefault(JLabel lb, Color bgColor) {
		lb.setMaximumSize(new Dimension(Short.MAX_VALUE, Short.MAX_VALUE));
		lb.setFont(new Font("Arial", Font.PLAIN, 30));
		lb.setForeground(Color.black);
		lb.setBackground(bgColor);
		lb.setOpaque(true);
		lb.addMouseListener(new LabelMouseListener());
	}
	
	// マウスイベント処理（ラベル用）
	class LabelMouseListener extends MouseAdapter {	
		@Override
		public void mouseClicked(MouseEvent e) {
			JLabel lbClicked = (JLabel)e.getSource();	// クリックされたラベルを取得
			JPanel jp= (JPanel)lbClicked.getParent();	// クリックされたラベルのペアレントを取得
			
			if (jp.getName() == "card1") {
				// 左クリック
				if(e.getButton() == MouseEvent.BUTTON1 && e.getClickCount() < 2) {
					CardLayout cl = (CardLayout)(cardPanel.getLayout()); // 現在表示されているラベルを取得
					
					// card2のラベルへクリックされたラベルの背景色、文字色、カラーコードを設定
					lblCard2.setBackground(lbClicked.getBackground()); 
					lblCard2.setForeground(lbClicked.getForeground());
					lblCard2.setText(lbClicked.getText());
						
					cl.show(cardPanel, "card2");
				}
			} else if (jp.getName() == "card2") {
				// 左クリック
				if (e.getButton() == MouseEvent.BUTTON1 && e.getClickCount() < 2) {
					CardLayout cl = (CardLayout)(cardPanel.getLayout());
					cl.show(cardPanel, "card1");
				} else // 右クリック 
				if (e.getButton() == MouseEvent.BUTTON3 && e.getClickCount() < 2) {
					Clipboard clipBoard = Toolkit.getDefaultToolkit().getSystemClipboard();
					StringSelection str = new StringSelection(lbClicked.getText());
					clipBoard.setContents(str, str);
				}
			}
		}
	}

	public static void main (String[] args) {
		try {
			JFrame mainFrame = new ColorSelect();
			mainFrame.setVisible(true);
		} catch(Exception e) {
			System.out.println(e.toString());
			System.exit(1);
		}
	}
}