/****************************/ /* 色の見本 */ /* coded by Y.Suganuma */ /****************************/ import java.awt.*; import java.awt.event.*; public class Test { public static void main (String[] args) { Color1 win = new Color1("色の見本"); } } class Color1 extends Frame implements ItemListener { int sel = 0; int r1 = 255, g1 = 255, b1 = 255; int r2 = 0, g2 = 0, b2 = 0; TextArea ta; C_Select pn2; CheckboxGroup cbg = new CheckboxGroup(); Checkbox c1, c2; /******************/ /* コンストラクタ */ /******************/ Color1(String name) { // JFrameクラスのコンストラクタ(Windowのタイトルを引き渡す) super(name); // Windowの大きさ setSize(610, 320); // レイアウトの変更(水平ギャップ,垂直ギャップ) setLayout(new BorderLayout(5, 10)); Font f = new Font("TimesRoman", Font.BOLD, 25); Font f1 = new Font("TimesRoman", Font.BOLD, 20); // タイトルとラジオボタン Panel pn0 = new Panel(); add(pn0, BorderLayout.NORTH); pn0.setLayout(new FlowLayout(FlowLayout.CENTER)); Label lb1 = new Label("色見本("); lb1.setFont(f); pn0.add(lb1); c1 = new Checkbox("背景", cbg, true); c1.setFont(f1); pn0.add(c1); c1.addItemListener(this); Label lb2 = new Label(" "); lb2.setFont(f); pn0.add(lb2); c2 = new Checkbox("文字", cbg, false); c2.setFont(f1); pn0.add(c2); c2.addItemListener(this); Label lb3 = new Label(")"); lb3.setFont(f); pn0.add(lb3); // 色見本表示エリアの設定 Panel pn1 = new Panel(); add(pn1, BorderLayout.CENTER); pn1.setLayout(new FlowLayout(FlowLayout.CENTER)); ta = new TextArea("\n 文字色", 3, 20, TextArea.SCROLLBARS_NONE); ta.setFont(f1); pn1.add(ta); // 色選択エリアの設定 pn2 = new C_Select(this); add(pn2, BorderLayout.SOUTH); // ウィンドウを表示 setVisible(true); // イベントアダプタ addWindowListener(new WinEnd()); } /******************************/ /* 上,左,下,右の余白の設定 */ /******************************/ public Insets getInsets() { return new Insets(50, 10, 10, 10); } /************/ /* 終了処理 */ /************/ class WinEnd extends WindowAdapter { public void windowClosing(WindowEvent e) { System.exit(0); } } /****************************/ /* チェックされたときの処理 */ /****************************/ public void itemStateChanged(ItemEvent e) { if (e.getItemSelectable() == c1) { pn2.tx[0][0].setEnabled(false); pn2.tx[0][1].setEnabled(false); pn2.tx[0][2].setEnabled(false); sel = 0; pn2.tx[0][0].setText(Integer.toString(r1)); pn2.tx[1][0].setText(Integer.toHexString(r1)); pn2.tx[0][1].setText(Integer.toString(g1)); pn2.tx[1][1].setText(Integer.toHexString(g1)); pn2.tx[0][2].setText(Integer.toString(b1)); pn2.tx[1][2].setText(Integer.toHexString(b1)); pn2.tx[0][0].setEnabled(true); pn2.tx[0][1].setEnabled(true); pn2.tx[0][2].setEnabled(true); } else { pn2.tx[0][0].setEnabled(false); pn2.tx[0][1].setEnabled(false); pn2.tx[0][2].setEnabled(false); sel = 1; pn2.tx[0][0].setText(Integer.toString(r2)); pn2.tx[1][0].setText(Integer.toHexString(r2)); pn2.tx[0][1].setText(Integer.toString(g2)); pn2.tx[1][1].setText(Integer.toHexString(g2)); pn2.tx[0][2].setText(Integer.toString(b2)); pn2.tx[1][2].setText(Integer.toHexString(b2)); pn2.tx[0][0].setEnabled(true); pn2.tx[0][1].setEnabled(true); pn2.tx[0][2].setEnabled(true); } } } /************/ /* 色の選択 */ /************/ class C_Select extends Panel implements ActionListener, TextListener { Color1 ss; TextField tx[][] = new TextField [2][3]; Button bt[][] = new Button [2][3]; C_Select(Color1 ss_i) { int i1; ss = ss_i; // レイアウトの変更(行,列,水平ギャップ,垂直ギャップ)とフォント setLayout(new GridLayout(1, 3, 30, 5)); Font f = new Font("TimesRoman", Font.BOLD, 20); setFont(f); // R,G,Bのパネル Panel pn1[] = new Panel [3]; Panel pn2[] = new Panel [3]; Panel pn3[] = new Panel [3]; Panel pn4[][] = new Panel [2][3]; for (i1 = 0; i1 < 3; i1++) { pn1[i1] = new Panel(); pn1[i1].setLayout(new BorderLayout(5, 10)); add(pn1[i1]); // タイトル pn2[i1] = new Panel(); pn1[i1].add(pn2[i1], BorderLayout.NORTH); pn2[i1].setLayout(new FlowLayout(FlowLayout.CENTER)); if (i1 == 0) pn2[i1].add(new Label("赤(R)")); else if (i1 == 1) pn2[i1].add(new Label("緑(G)")); else pn2[i1].add(new Label("青(B)")); // 選択領域 pn3[i1] = new Panel(); pn3[i1].setLayout(new GridLayout(2, 1, 5, 10)); pn1[i1].add(pn3[i1], BorderLayout.CENTER); pn4[0][i1] = new Panel(); pn4[0][i1].setLayout(new BorderLayout(5, 10)); pn3[i1].add(pn4[0][i1]); pn4[0][i1].add(new Label("10進"), BorderLayout.WEST); tx[0][i1] = new TextField("255"); tx[0][i1].addTextListener(this); pn4[0][i1].add(tx[0][i1], BorderLayout.CENTER); bt[0][i1] = new Button("▲"); bt[0][i1].addActionListener(this); pn4[0][i1].add(bt[0][i1], BorderLayout.EAST); pn4[1][i1] = new Panel(); pn4[1][i1].setLayout(new BorderLayout(5, 10)); pn3[i1].add(pn4[1][i1]); pn4[1][i1].add(new Label("16進"), BorderLayout.WEST); tx[1][i1] = new TextField("ff"); tx[1][i1].setEditable(false); pn4[1][i1].add(tx[1][i1], BorderLayout.CENTER); bt[1][i1] = new Button("▼"); bt[1][i1].addActionListener(this); pn4[1][i1].add(bt[1][i1], BorderLayout.EAST); } } /****************************************/ /* TextAreaの内容が変更されたときの処理 */ /****************************************/ public void textValueChanged(TextEvent e) { int k1, k2, k3; String str; str = tx[0][0].getText(); if (str.length() == 0) k1 = 0; else k1 = Integer.parseInt(str); str = tx[0][1].getText(); if (str.length() == 0) k2 = 0; else k2 = Integer.parseInt(str); str = tx[0][2].getText(); if (str.length() == 0) k3 = 0; else k3 = Integer.parseInt(str); if (e.getSource() == tx[0][0]) { if (k1 < 0) { k1 = 0; tx[0][0].setText("0"); } else if (k1 > 255) { k1 = 255; tx[0][0].setText("255"); } tx[1][0].setText(Integer.toHexString(k1)); } else if (e.getSource() == tx[0][1]) { if (k2 < 0) { k2 = 0; tx[0][1].setText("0"); } else if (k2 > 255) { k2 = 255; tx[0][1].setText("255"); } tx[1][1].setText(Integer.toHexString(k2)); } else { if (k3 < 0) { k3 = 0; tx[0][2].setText("0"); } else if (k3 > 255) { k3 = 255; tx[0][2].setText("255"); } tx[1][2].setText(Integer.toHexString(k3)); } if (ss.sel == 0) { ss.r1 = k1; ss.g1 = k2; ss.b1 = k3; ss.ta.setBackground(new Color(k1, k2, k3)); } else { ss.r2 = k1; ss.g2 = k2; ss.b2 = k3; ss.ta.setForeground(new Color(k1, k2, k3)); } } /************************************/ /* ボタンがクリックされたときの処理 */ /************************************/ public void actionPerformed(ActionEvent e) { int k1; String str; if (e.getSource() == bt[0][0]) { str = tx[0][0].getText(); if (str.length() == 0) k1 = 0; else k1 = Integer.parseInt(str); k1++; if (k1 > 255) k1 = 255; tx[0][0].setText(Integer.toString(k1)); } else if (e.getSource() == bt[1][0]) { str = tx[0][0].getText(); if (str.length() == 0) k1 = 0; else k1 = Integer.parseInt(str); k1--; if (k1 < 0) k1 = 0; tx[0][0].setText(Integer.toString(k1)); } if (e.getSource() == bt[0][1]) { str = tx[0][1].getText(); if (str.length() == 0) k1 = 0; else k1 = Integer.parseInt(str); k1++; if (k1 > 255) k1 = 255; tx[0][1].setText(Integer.toString(k1)); } else if (e.getSource() == bt[1][1]) { str = tx[0][1].getText(); if (str.length() == 0) k1 = 0; else k1 = Integer.parseInt(str); k1--; if (k1 < 0) k1 = 0; tx[0][1].setText(Integer.toString(k1)); } if (e.getSource() == bt[0][2]) { str = tx[0][2].getText(); if (str.length() == 0) k1 = 0; else k1 = Integer.parseInt(str); k1++; if (k1 > 255) k1 = 255; tx[0][2].setText(Integer.toString(k1)); } else if (e.getSource() == bt[1][2]) { str = tx[0][2].getText(); if (str.length() == 0) k1 = 0; else k1 = Integer.parseInt(str); k1--; if (k1 < 0) k1 = 0; tx[0][2].setText(Integer.toString(k1)); } } }