1 /* 2 * 3 * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 9 * - Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * - Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * - Neither the name of Oracle nor the names of its 17 * contributors may be used to endorse or promote products derived 18 * from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 21 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 34 package java2d; 35 36 37 import java.awt.Color; 38 import java.awt.Graphics; 39 import java.awt.event.MouseAdapter; 40 import java.awt.event.MouseEvent; 41 import javax.swing.JPanel; 42 import javax.swing.border.EtchedBorder; 43 44 45 /** 46 * A convenience class for demos that use Custom Controls. This class 47 * sets up the thread for running the custom control. A notifier thread 48 * is started as well, a flashing 2x2 rect is drawn in the upper right corner 49 * while the custom control thread continues to run. 50 */ 51 @SuppressWarnings("serial") 52 public abstract class CustomControls extends JPanel implements Runnable { 53 54 55 protected Thread thread; 56 protected boolean doNotifier; 57 private CCNotifierThread ccnt; 58 private String name = "foo.bar Demo"; 59 private static final Color blue = new Color(204, 204, 255); 60 61 62 public CustomControls() { 63 setBorder(new EtchedBorder()); 64 addMouseListener(new MouseAdapter() { 65 66 @Override 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 @Override 79 public void paintComponent(Graphics g) { 80 super.paintComponent(g); 81 g.setColor(doNotifier ? blue : Color.gray); 82 g.fillRect(getSize().width-2, 0, 2, 2); 83 } 84 85 public void start() { 86 if (thread == null) { 87 thread = new Thread(this); 88 thread.setPriority(Thread.MIN_PRIORITY); 89 thread.setName(name + " ccthread"); 90 thread.start(); 91 (ccnt = new CCNotifierThread()).start(); 92 ccnt.setName(name + " ccthread notifier"); 93 } 94 } 95 96 public synchronized void stop() { 97 if (thread != null) { 98 thread.interrupt(); 99 if (ccnt != null) { 100 ccnt.interrupt(); 101 } 102 } 103 thread = null; 104 } 105 106 107 // Custom Controls override the run method 108 @Override 109 public void run() { 110 } 111 112 113 /** 114 * Notifier that the custom control thread is running. 115 */ 116 class CCNotifierThread extends Thread { 117 118 @Override 119 @SuppressWarnings("SleepWhileHoldingLock") 120 public void run() { 121 while (thread != null) { 122 doNotifier = !doNotifier; 123 repaint(); 124 try { 125 Thread.sleep(444); 126 } catch (Exception ex) { 127 break; 128 } 129 } 130 doNotifier = false; repaint(); 131 } 132 } 133 }