home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / WDESAMPL.BIN / ExampleApplet.java < prev    next >
Text File  |  1997-06-23  |  4KB  |  158 lines

  1. /*
  2.  * @(#)ExampleApplet.java    1.2 97/01/14 Jeff Dinkins
  3.  *
  4.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  */
  7.  
  8. package actual;
  9.  
  10. import java.applet.*;
  11. import java.util.*;
  12. import java.awt.*;
  13.  
  14. /**
  15.  * This applet creates a window that has a pretty background 
  16.  * picture, and adds several lightweight Gauge objects.
  17.  *
  18.  * Notice how parts of the lightweight Gauges are "transparent" 
  19.  * and you can see the image behind them! Cool!
  20.  */
  21. public class ExampleApplet extends Applet {
  22.  
  23.   public void init() {
  24.     
  25.     setLayout(new BorderLayout());
  26.     
  27.     // create a double buffer panel and add it to the 
  28.     // center of the frame
  29.     DoubleBufferPanel dbp = new DoubleBufferPanel();
  30.     dbp.setLayout(new BorderLayout());
  31.     add("Center", dbp);
  32.     
  33.     // create a pretty panel and add it to the
  34.     // double buffer panel
  35.     
  36.     //needed because ExampleApplet is running under Switcher
  37.     Applet parentApplet;
  38.     
  39.     //Get the parent Applet object. 
  40.     try {
  41.       parentApplet = (Applet)getParent();
  42.       PrettyPanel pp = new PrettyPanel(parentApplet);
  43.       pp.setLayout(new FlowLayout());
  44.       dbp.add("Center", pp);
  45.       
  46.       // *** Create Gauges
  47.       Gauge gauge1  = new Gauge(Color.green.darker());
  48.       pp.add(gauge1);
  49.       
  50.       Gauge gauge2  = new Gauge(Color.red.darker());
  51.       pp.add(gauge2);
  52.       
  53.       Gauge gauge3 = new Gauge(Color.cyan);
  54.       pp.add(gauge3);
  55.       
  56.       Gauge gauge4 = new Gauge();
  57.       pp.add(gauge4);
  58.       
  59.       Gauge gauge5 = new Gauge(Color.blue.darker());
  60.       pp.add(gauge5);
  61.       
  62.       Gauge gauge6 = new Gauge(Color.pink.darker());
  63.       pp.add(gauge6);
  64.       
  65.       Gauge gauge7 = new Gauge(Color.yellow);
  66.       pp.add(gauge7);
  67.       
  68.       // *** Create threads to drive the gauges
  69.       GaugeThread gaugeThread1 = new GaugeThread(gauge1);
  70.       gaugeThread1.start();
  71.       
  72.       GaugeThread gaugeThread2 = new GaugeThread(gauge2);
  73.       gaugeThread2.start();
  74.       
  75.       GaugeThread gaugeThread3 = new GaugeThread(gauge3);
  76.       gaugeThread3.start();
  77.       
  78.       GaugeThread gaugeThread4 = new GaugeThread(gauge4);
  79.       gaugeThread4.start();
  80.       
  81.       GaugeThread gaugeThread5 = new GaugeThread(gauge5);
  82.       gaugeThread5.start();
  83.       
  84.       GaugeThread gaugeThread6 = new GaugeThread(gauge6);
  85.       gaugeThread6.start();
  86.       
  87.       GaugeThread gaugeThread7 = new GaugeThread(gauge7);
  88.       gaugeThread7.start();
  89.       
  90.     } catch (ClassCastException e) {
  91.       System.err.println("Parent isn't an Applet!");
  92.       throw(e);
  93.     }
  94.   }
  95. }
  96.  
  97.  
  98. class PrettyPanel extends Container {
  99.   Image background;
  100.  
  101.   PrettyPanel(Applet applet) {
  102.       background = applet.getImage(applet.getCodeBase(),
  103.                    "images/mandrill.jpg");
  104.   }
  105.  
  106.   /**
  107.    * override update to *not* erase the background before painting
  108.    */
  109.   public void update(Graphics g) {
  110.       paint(g);
  111.   }
  112.  
  113.   /**
  114.    * paint the background picture, then call super.paint which
  115.    * will paint all contained components 
  116.    *
  117.    * Note: You MUST call super.paint(g) or the lightweight
  118.    * component(s) won't get painted.
  119.    */
  120.   public void paint(Graphics g) {
  121.       g.drawImage(background, 0, 0, getSize().width, getSize().height,
  122.                   getBackground(), this);
  123.        super.paint(g); 
  124.   }
  125. }
  126.  
  127. class GaugeThread extends Thread {
  128.  
  129.   Gauge gauge;
  130.   static int seed = 1;
  131.  
  132.   GaugeThread(Gauge gauge) {
  133.       super("Gauge thread");
  134.       this.gauge = gauge;
  135.   }
  136.  
  137.   public void run () {
  138.       Random rand = new Random(seed++);
  139.       int i = gauge.getTotalAmount()/2;
  140.       while(true) {
  141.           float r = rand.nextFloat();
  142.           if(r > .5) {
  143.           if(i < gauge.getTotalAmount())
  144.                 i+=2;
  145.           } else {
  146.           if(i > 0)
  147.         i-=2;
  148.           }
  149.       
  150.           gauge.setCurrentAmount(i);
  151.           try {
  152.               sleep(100);
  153.           } catch (java.lang.InterruptedException e) {
  154.           }
  155.       }
  156.   }
  157. }
  158.