home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / JBuilder8.iso / Solaris / resource / jre / demo / plugin / jfc / Java2D / src / java2d / CustomControls.java < prev    next >
Encoding:
Java Source  |  2002-09-06  |  4.0 KB  |  122 lines

  1. /*
  2.  * Copyright (c) 2002 Sun Microsystems, Inc. All  Rights Reserved.
  3.  * 
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  * 
  8.  * -Redistributions of source code must retain the above copyright
  9.  *  notice, this list of conditions and the following disclaimer.
  10.  * 
  11.  * -Redistribution in binary form must reproduct the above copyright
  12.  *  notice, this list of conditions and the following disclaimer in
  13.  *  the documentation and/or other materials provided with the distribution.
  14.  * 
  15.  * Neither the name of Sun Microsystems, Inc. or the names of contributors
  16.  * may be used to endorse or promote products derived from this software
  17.  * without specific prior written permission.
  18.  * 
  19.  * This software is provided "AS IS," without a warranty of any kind. ALL
  20.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
  21.  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
  22.  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT
  23.  * BE LIABLE FOR ANY DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT
  24.  * OF OR RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THE SOFTWARE OR ITS
  25.  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
  26.  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
  27.  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
  28.  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN
  29.  * IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  30.  * 
  31.  * You acknowledge that Software is not designed, licensed or intended for
  32.  * use in the design, construction, operation or maintenance of any nuclear
  33.  * facility.
  34.  */
  35.  
  36. /*
  37.  * @(#)CustomControls.java    1.5 02/06/13
  38.  */
  39.  
  40. package java2d;
  41.  
  42. import java.awt.*;
  43. import java.awt.event.*;
  44. import javax.swing.*;
  45.  
  46. /**
  47.  * A convenience class for demos that use Custom Controls.  This class
  48.  * sets up the thread for running the custom control.  A notifier thread
  49.  * is started as well, a flashing 2x2 rect is drawn in the upper right corner
  50.  * while the custom control thread continues to run.
  51.  */
  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.         setBackground(Color.gray);
  64.         addMouseListener(new MouseAdapter() {
  65.             public void mouseClicked(MouseEvent e) {
  66.                 if (thread == null) { start(); } else { stop(); }
  67.             }
  68.         });
  69.     }
  70.  
  71.     public CustomControls(String name) {
  72.         this();
  73.         this.name = name + " Demo";
  74.     }
  75.  
  76.     public void paintComponent(Graphics g) {
  77.         super.paintComponent(g);
  78.         g.setColor(doNotifier ? blue : Color.gray);
  79.         g.fillRect(getSize().width-2, 0, 2, 2);
  80.     }
  81.  
  82.     public void start() {
  83.         if (thread == null) {
  84.             thread = new Thread(this);
  85.             thread.setPriority(Thread.MIN_PRIORITY);
  86.             thread.setName(name + " ccthread");
  87.             thread.start();
  88.             (ccnt = new CCNotifierThread()).start();
  89.             ccnt.setName(name + " ccthread notifier");
  90.         }
  91.     }
  92.  
  93.     public synchronized void stop() {
  94.         if (thread != null) {
  95.             thread.interrupt();
  96.             if (ccnt != null) {
  97.                 ccnt.interrupt();
  98.             }
  99.         }
  100.         thread = null;
  101.     }
  102.  
  103.  
  104.     // Custom Controls override the run method
  105.     public void run() { }
  106.  
  107.  
  108.     /**
  109.      * Notifier that the custom control thread is running.
  110.      */
  111.     class CCNotifierThread extends Thread {
  112.         public void run() {
  113.             while (thread != null) {
  114.                 doNotifier = !doNotifier;
  115.                 repaint();
  116.                 try { Thread.sleep(444); } catch (Exception ex) { break; }
  117.             }
  118.             doNotifier = false; repaint();
  119.         }
  120.     }
  121. }
  122.