/****************************/ /* LEDの点灯 */ /* coded by Y.Suganuma */ /****************************/ import java.io.*; import java.awt.*; import java.awt.event.*; import java.util.StringTokenizer; public class LED { public static void main (String[] args) throws IOException { LEDControl lc = new LEDControl("LED"); } } class LEDControl extends Frame implements Runnable, ActionListener { Thread th; boolean state = true; Dimension d; Insets insets; int init = 1, led = 0, led_o = 0; Button bt; /************************/ /* コンストラクタ */ /* name : タイトル */ /************************/ LEDControl(String name) { // Frameクラスのコンストラクタの呼び出し super(name); // Windowの大きさの設定 setSize(270, 130); d = getSize(); // ウィンドウを表示 setVisible(true); setBackground(Color.black); insets = getInsets(); // リセットボタンの追加 setLayout(null); Font f = new Font("MS 明朝", Font.PLAIN, 20); bt = new Button("Reset"); bt.setFont(f); bt.setBackground(Color.green); bt.addActionListener(this); // リスナー bt.setLocation(d.width-insets.right-90, insets.top+10); bt.setSize(new Dimension(80, 30)); add(bt); // イベントアダプタ addWindowListener(new WinEnd()); // スレッドの定義と開始 th = new Thread(this); th.start(); } /******************************/ /* ボタンが押されたときの処理 */ /******************************/ public void actionPerformed(ActionEvent e) { led = 0; repaint(); } /******************/ /* スレッドの実行 */ /******************/ public void run() { int i1, draw = 1; String str; StringTokenizer token; while (state) { try { BufferedReader in = new BufferedReader(new FileReader("LED")); str = in.readLine(); token = new StringTokenizer(str, " \n"); draw = Integer.parseInt(token.nextToken()); if (draw == 0) { led = Integer.parseInt(token.nextToken()); in.close(); if (led != led_o) { repaint(); led_o = led; } PrintStream out = new PrintStream(new FileOutputStream("LED")); out.println("1 " + led); out.close(); } else in.close(); } catch (IOException er) {} try { th.sleep(500); } catch (InterruptedException e) {} } } /********/ /* 描画 */ /********/ public void paint (Graphics g) { int i1, x, y, b = 128, k; // LEDボタンの描画 x = insets.left + 10; y = d.height - insets.bottom - 30; k = led; for (i1 = 0; i1 < 8; i1++) { if (k / b > 0) g.setColor(Color.yellow); else g.setColor(Color.lightGray); g.fillOval(x, y, 20, 20); x += 30; k %= b; b /= 2; } } /************/ /* 終了処理 */ /************/ class WinEnd extends WindowAdapter { public void windowClosing(WindowEvent e) { System.exit(0); } } }