解答編13
「イベント処理5」
 
← 前  :  次 →

1つのプログラムで まとめてみました. 「リスト・セレクション・イベント」は,これといって難しくないので 特にポイントもありません.

「ColorBarFrame2.java」

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;

public class ColorBarFrame2 extends JFrame implements ListSelectionListener {
	private JPanel routePanel;
	private JList srcColorList;
	private JList dstColorList;
	private JPanel srcColorPanel;
	private JPanel dstColorPanel;
	private JPanel totalColorPanel;
	public static final String[] COLOR_NAME_DATA={
		"原色の黒 Primary Black",
		"原色の青 Primary Blue",
		"インディゴ Indigo",
		"瑠璃 Lapis Lazuli",
		"ビリヤード Billiard",
		"マリンブルー Marine Blue",
		"コバルトブルー Cobalt Blue",
		"セルリアンブルー Cerulean Blue",
		"原色の緑 Primary Green",
		"原色のシアン Primary Cyan",
		"アイヴォリーブラック Ivory Black",
		"トープ Taup",
		"フリント Flint",
		"ネイヴィブルー Navy Blue",
		"ヴィリディアン Viridian",
		"サファイアブルー Sapphire Blue",
		"ガーターブルー Garter Blue",
		"ボルドー Bourdeaux",
		"栗 Chestnut",
		"江戸紫 Yedo Purple",
		"ビスタ Bistre",
		"煤竹 Dirty Bambo",
		"憲房 Kenpo\'s Gray",
		"二藍 Indigo Twice",
		"サップグリーン Sap Green",
		"花 Flower Blue",
		"アクアマリン Aquamarine",
		"エンペラーグリーン Emperor Green",
		"ターコイズブルー Turquoise Blue",
		"クリムゾン Crimson",
		"ピアニー Peony",
		"海老茶 Prawn Brown",
		"ロイヤルパープル Royal Purple",
		"茶 Brown",
		"ヘーゼル Hazel",
		"ココア Cocoa Brown",
		"減紫 Discolored Purple",
		"鶯 Bush Warbler Green",
		"梅幸茶 Baiko\'s Brown",
		"竜胆 Gentian Blue",
		"ゲイグリーン Gay Green",
		"草 Grass",
		"老竹 Old Bamboo",
		"ポースリンブルー Porcelain Blue",
		"若草 Young Grass",
		"エメラルドグリーン Emerald Green",
		"コバルトグリーン Cobalt Green",
		"ターコイズグリーン Turquoise Green",
		"アザーブルー Azure Blue",
		"紅 Safflower Red",
		"ヘンナ Henna",
		"チリアンパープル Tyrian Purple",
		"樺 Cherry Bark",
		"フォイーユモルト Feuille Morte",
		"本紫 Real Purple",
		"路考茶 Roko\'s Brown",
		"タン Tan",
		"琥珀 Amber Brown",
		"ラセット Russet",
		"オールドローズ Old Rose",
		"菜種油 Canola Oil",
		"黄蘗 Amur Cork Yellow",
		"シャルトルーズ Chartreuse",
		"ウィローグリーン Willow Green",
		"シルヴァーグレイ Silver Gray",
		"藤 Wistaria",
		"グラスグリーン Grass Green",
		"浅緑 Light Green",
		"水 Water",
		"原色の赤 Primary Red",
		"カーマイン Carmine",
		"原色のマゼンタ Primary Magenta",
		"赤 Red",
		"ヴァーミリオン Vermilion",
		"朱 Oriental Vermilion",
		"躑躅 Azalea",
		"クロームオレンジ Chrome Orange",
		"タンジャリン Tangerine",
		"黄丹 Prince Yellow",
		"桃 Peach Blossom",
		"サンフラワー Sunflower",
		"山吹 Kerria",
		"雌黄 Gamboge",
		"雄黄 Poison Yellow",
		"肌 Flesh",
		"撫子 Chinese Pink",
		"原色の黄 Primary Yellow",
		"金糸雀 Canary",
		"サルファーイエロー Sulphar Yellow",
		"ミモザ Mimosa",
		"クリーム Cream",
		"原色の白 Primary White"
	};
	public static final Color[] COLOR_DATA={
		new Color(0x00,0x00,0x00),
		new Color(0x00,0x00,0xff),
		new Color(0x00,0x33,0x66),
		new Color(0x00,0x66,0xcc),
		new Color(0x00,0x99,0x66),
		new Color(0x00,0x99,0xcc),
		new Color(0x00,0x99,0xff),
		new Color(0x00,0xcc,0xff),
		new Color(0x00,0xff,0x00),
		new Color(0x00,0xff,0xff),
		new Color(0x33,0x00,0x00),
		new Color(0x33,0x33,0x00),
		new Color(0x33,0x33,0x33),
		new Color(0x33,0x66,0xcc),
		new Color(0x33,0x99,0x66),
		new Color(0x33,0x99,0xcc),
		new Color(0x33,0x99,0xff),
		new Color(0x66,0x00,0x66),
		new Color(0x66,0x33,0x00),
		new Color(0x66,0x33,0x99),
		new Color(0x66,0x66,0x00),
		new Color(0x66,0x66,0x33),
		new Color(0x66,0x66,0x66),
		new Color(0x66,0x66,0x99),
		new Color(0x66,0x99,0x66),
		new Color(0x66,0x99,0xff),
		new Color(0x66,0xcc,0xcc),
		new Color(0x66,0xff,0x99),
		new Color(0x66,0xff,0xcc),
		new Color(0x99,0x00,0x99),
		new Color(0x99,0x00,0xcc),
		new Color(0x99,0x33,0x00),
		new Color(0x99,0x33,0xcc),
		new Color(0x99,0x66,0x00),
		new Color(0x99,0x66,0x33),
		new Color(0x99,0x66,0x66),
		new Color(0x99,0x66,0x99),
		new Color(0x99,0x99,0x33),
		new Color(0x99,0x99,0x66),
		new Color(0x99,0x99,0xcc),
		new Color(0x99,0xcc,0x33),
		new Color(0x99,0xcc,0x66),
		new Color(0x99,0xcc,0x99),
		new Color(0x99,0xcc,0xcc),
		new Color(0x99,0xff,0x00),
		new Color(0x99,0xff,0x66),
		new Color(0x99,0xff,0x99),
		new Color(0x99,0xff,0xcc),
		new Color(0x99,0xff,0xff),
		new Color(0xcc,0x00,0x99),
		new Color(0xcc,0x33,0x00),
		new Color(0xcc,0x33,0xcc),
		new Color(0xcc,0x66,0x33),
		new Color(0xcc,0x66,0x66),
		new Color(0xcc,0x66,0xff),
		new Color(0xcc,0x99,0x00),
		new Color(0xcc,0x99,0x33),
		new Color(0xcc,0x99,0x66),
		new Color(0xcc,0x99,0x99),
		new Color(0xcc,0x99,0xcc),
		new Color(0xcc,0xcc,0x00),
		new Color(0xcc,0xcc,0x33),
		new Color(0xcc,0xcc,0x66),
		new Color(0xcc,0xcc,0x99),
		new Color(0xcc,0xcc,0xcc),
		new Color(0xcc,0xcc,0xff),
		new Color(0xcc,0xff,0x99),
		new Color(0xcc,0xff,0xcc),
		new Color(0xcc,0xff,0xff),
		new Color(0xff,0x00,0x00),
		new Color(0xff,0x00,0x99),
		new Color(0xff,0x00,0xff),
		new Color(0xff,0x33,0x00),
		new Color(0xff,0x66,0x66),
		new Color(0xff,0x66,0x99),
		new Color(0xff,0x66,0xff),
		new Color(0xff,0x99,0x00),
		new Color(0xff,0x99,0x66),
		new Color(0xff,0x99,0x99),
		new Color(0xff,0x99,0xcc),
		new Color(0xff,0xcc,0x00),
		new Color(0xff,0xcc,0x33),
		new Color(0xff,0xcc,0x66),
		new Color(0xff,0xcc,0x99),
		new Color(0xff,0xcc,0xcc),
		new Color(0xff,0xcc,0xff),
		new Color(0xff,0xff,0x00),
		new Color(0xff,0xff,0x33),
		new Color(0xff,0xff,0x66),
		new Color(0xff,0xff,0x99),
		new Color(0xff,0xff,0xcc),
		new Color(0xff,0xff,0xff)
	};

	public ColorBarFrame2() {
		setLocation(50,25);
		setSize(480,360);
		setResizable(true);
		setTitle("ColorBar");
		getContentPane().setLayout(new BorderLayout());
		setBackground(SystemColor.menu);
		setForeground(SystemColor.menuText);
		setDefaultCloseOperation(EXIT_ON_CLOSE);

		setRoutePanel();
		getContentPane().add(routePanel,BorderLayout.CENTER);

		srcColorList.setSelectedIndex(0);
		dstColorList.setSelectedIndex(0);
	}

	private void setRoutePanel() {
		JPanel subPanel;
		JPanel subSubPanel;

		routePanel=new JPanel(new GridLayout(1,3,4,4));

		srcColorList=new JList(COLOR_NAME_DATA);
		srcColorList.addListSelectionListener(this);
		routePanel.add(new JScrollPane(srcColorList));

		srcColorPanel=new JPanel();
		// -
		subSubPanel=new JPanel(new BorderLayout());
		subSubPanel.setBorder(BorderFactory.createTitledBorder(
			BorderFactory.createEtchedBorder(),"<<LEFT COLOR"
			,TitledBorder.LEFT,TitledBorder.ABOVE_TOP));
		subSubPanel.add(srcColorPanel,BorderLayout.CENTER);
		subPanel=new JPanel(new GridLayout(3,1,4,4));
		subPanel.add(subSubPanel);
		// -
		dstColorPanel=new JPanel();
		subSubPanel=new JPanel(new BorderLayout());
		subSubPanel.setBorder(BorderFactory.createTitledBorder(
			BorderFactory.createEtchedBorder(),"RIGHT COLOR%gt;>"
			,TitledBorder.RIGHT,TitledBorder.ABOVE_TOP));
		subSubPanel.add(dstColorPanel,BorderLayout.CENTER);
		subPanel.add(subSubPanel);
		// -
		totalColorPanel=new JPanel();
		subSubPanel=new JPanel(new BorderLayout());
		subSubPanel.setBorder(BorderFactory.createTitledBorder(
			BorderFactory.createEtchedBorder(),"< MIXED COLOR >"
			,TitledBorder.CENTER,TitledBorder.ABOVE_TOP));
		subSubPanel.add(totalColorPanel,BorderLayout.CENTER);
		subPanel.add(subSubPanel);
		routePanel.add(subPanel);

		dstColorList=new JList(COLOR_NAME_DATA);
		dstColorList.addListSelectionListener(this);
		routePanel.add(new JScrollPane(dstColorList));
	}

	public void valueChanged(ListSelectionEvent lse) {
		int srcIdx;
		int dstIdx;

		srcIdx=srcColorList.getSelectedIndex();
		dstIdx=dstColorList.getSelectedIndex();
		if(srcIdx<0||dstIdx<0) {
			return;
		}

		srcColorPanel.setBackground(COLOR_DATA[srcIdx]);
		dstColorPanel.setBackground(COLOR_DATA[dstIdx]);
		totalColorPanel.setBackground(new Color(
			(COLOR_DATA[srcIdx].getRed()+COLOR_DATA[dstIdx].getRed())/2
			,(COLOR_DATA[srcIdx].getGreen()+COLOR_DATA[dstIdx].getGreen())/2
			,(COLOR_DATA[srcIdx].getBlue()+COLOR_DATA[dstIdx].getBlue())/2));
	}

	public static void main(String[] args) {
		ColorBarFrame2 cbf;

		cbf=new ColorBarFrame2();
		cbf.setVisible(true);
	}
}


表紙へ戻る