01:import java.awt.*;
02:import java.awt.event.*;
03:import javax.swing.* ;
04:
05:class Twinkle3 extends JPanel implements Runnable {
06:    static Color bc = new Color(0, 0, 128);        // 背景色 : Navy Blue
07:    static Color pc = new Color(181, 166,  66);    // 図形の色 : Brass
08:    static Color dc = bc;                          // 表示色
09:    static boolean stop = false;                   // 表示停止フラグ
10:
11:    public Twinkle3() {
12:        setBackground(bc);                         // 背景色を指定
13:    }
14:
15:    public void paintComponent(Graphics g) {
16:        super.paintComponent(g);
17:        int w = getWidth();
18:        int h = getHeight();
19:        int r = (w+h)/8;
20:        int x = w/2-r;
21:        int y = h/2-r;
22:        g.setColor(dc);                            // bc か pc のいずれか
23:        g.fillOval(x,y,2*r,2*r);                   //
24:    }
25:
26:    public void run() {
27:        while(true){
28:            try {                                  //
29:                Thread.sleep(200);                 // 0.2 秒停止
30:            }                                      //
31:            catch(InterruptedException e) {        //
32:                e.printStackTrace();               //
33:            }                                      //
34:            if(!stop) dc = pc;                     //
35:            repaint();                             // 表示開始
36:            try {                                  //
37:                Thread.sleep(1000);                // 1 秒間表示
38:            }                                      //
39:            catch(InterruptedException e) {        //
40:                e.printStackTrace();               //
41:            }                                      //
42:            dc = bc;                               //
43:            repaint();                             // 表示終了
44:        }
45:    }
46:
47:    public static void main(String args[]) {
48:        JFrame frame = new JFrame("Twinkle");              // 外側のフレーム
49:        frame.addWindowListener(new WindowAdapter() {
50:            public void windowClosing(WindowEvent e) { System.exit(0); }
51:        });
52:        Container container = frame.getContentPane();      // コンテントペインの獲得
53:        container.setLayout(new BorderLayout());           // center と south を使用
54:        final JColorChooser chooser = new JColorChooser(); // 表示色の選択用
55:        final JButton button = new JButton("color");       // 選択ボタン
56:        button.addActionListener(new ActionListener() {
57:            public void actionPerformed(ActionEvent e) {
58:                Color c = chooser.showDialog(null, "draw color", pc);
59:                if(c!=null) pc = c;                        // 色が指定されたら変更
60:            }
61:        });
62:        final JButton sbutton = new JButton("stop");       // 表示停止ボタン
63:        sbutton.addActionListener(new ActionListener() {
64:            public void actionPerformed(ActionEvent e) {
65:                if(stop) {
66:                    stop = false;                          //
67:                    sbutton.setText("stop");               //
68:                } else {
69:                    stop = true;                           //
70:                    sbutton.setText("go");                 //
71:                }
72:            }
73:        });
74:        JPanel pan = new JPanel();
75:        pan.add(button);                                   // 色選択ボタンの貼り付け
76:        pan.add(sbutton);                                  // 表示停止ボタンの貼り付け
77:        container.add(pan, BorderLayout.SOUTH);            // 2つのボタンを貼り付け
78:        Twinkle3 panel = new Twinkle3();                   // 表示パネルの生成
79:        container.add(panel, BorderLayout.CENTER);         // パネルの貼り付け
80:        frame.setSize(300,340);                            // 
81:        frame.setVisible(true);                            // フレームの表示
82:        Thread thread = new Thread(panel);                 // スレッドの生成
83:        thread.start();                                    // スレッドの起動
84:    }
85:}