1 36 37 40 41 package java2d; 42 43 import java.awt.*; 44 import java.awt.event.*; 45 import javax.swing.*; 46 import javax.swing.border.EtchedBorder ; 47 48 54 public abstract class CustomControls extends JPanel implements Runnable { 55 56 57 protected Thread thread; 58 protected boolean doNotifier; 59 private CCNotifierThread ccnt; 60 private String name = "foo.bar Demo"; 61 private static final Color blue = new Color(204, 204, 255); 62 63 64 public CustomControls() { 65 setBorder(new EtchedBorder ()); 66 addMouseListener(new MouseAdapter() { 67 public void mouseClicked(MouseEvent e) { 68 if (thread == null) { start(); } else { stop(); } 69 } 70 }); 71 } 72 73 public CustomControls(String name) { 74 this(); 75 this.name = name + " Demo"; 76 } 77 78 public void paintComponent(Graphics g) { 79 super.paintComponent(g); 80 g.setColor(doNotifier ? blue : Color.gray); 81 g.fillRect(getSize().width-2, 0, 2, 2); 82 } 83 84 public void start() { 85 if (thread == null) { 86 thread = new Thread (this); 87 thread.setPriority(Thread.MIN_PRIORITY); 88 thread.setName(name + " ccthread"); 89 thread.start(); 90 (ccnt = new CCNotifierThread()).start(); 91 ccnt.setName(name + " ccthread notifier"); 92 } 93 } 94 95 public synchronized void stop() { 96 if (thread != null) { 97 thread.interrupt(); 98 if (ccnt != null) { 99 ccnt.interrupt(); 100 } 101 } 102 thread = null; 103 } 104 105 106 public void run() { } 108 109 110 113 class CCNotifierThread extends Thread { 114 public void run() { 115 while (thread != null) { 116 doNotifier = !doNotifier; 117 repaint(); 118 try { Thread.sleep(444); } catch (Exception ex) { break; } 119 } 120 doNotifier = false; repaint(); 121 } 122 } 123 } 124 | Popular Tags |