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