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