home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 1998 February / VPR9802A.ISO / APP_DEMO / VC / SAMPLES.BIN / ExampleApplet.java < prev    next >
Text File  |  1997-10-27  |  3KB  |  145 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. import java.applet.*;
  9. import java.util.*;
  10. import java.awt.*;
  11.  
  12. /**
  13.  * This applet creates a window that has a pretty background 
  14.  * picture, and adds several lightweight Gauge objects.
  15.  *
  16.  * Notice how parts of the lightweight Gauges are "transparent" 
  17.  * and you can see the image behind them! Cool!
  18.  */
  19. public class ExampleApplet extends Applet {
  20.  
  21.   public void init() {
  22.       setLayout(new BorderLayout());
  23.  
  24.       // create a double buffer panel and add it to the 
  25.       // center of the frame
  26.       DoubleBufferPanel dbp = new DoubleBufferPanel();
  27.       dbp.setLayout(new BorderLayout());
  28.       add("Center", dbp);
  29.  
  30.       // create a pretty panel and add it to the
  31.       // double buffer panel
  32.       PrettyPanel pp = new PrettyPanel(this);
  33.       pp.setLayout(new FlowLayout());
  34.       dbp.add("Center", pp);
  35.  
  36.       // *** Create Gauges
  37.       Gauge gauge1  = new Gauge(Color.green.darker());
  38.       pp.add(gauge1);
  39.  
  40.       Gauge gauge2  = new Gauge(Color.red.darker());
  41.       pp.add(gauge2);
  42.  
  43.       Gauge gauge3 = new Gauge(Color.cyan);
  44.       pp.add(gauge3);
  45.  
  46.       Gauge gauge4 = new Gauge();
  47.       pp.add(gauge4);
  48.  
  49.       Gauge gauge5 = new Gauge(Color.blue.darker());
  50.       pp.add(gauge5);
  51.  
  52.       Gauge gauge6 = new Gauge(Color.pink.darker());
  53.       pp.add(gauge6);
  54.  
  55.       Gauge gauge7 = new Gauge(Color.yellow);
  56.       pp.add(gauge7);
  57.  
  58.  
  59.       // *** Create threads to drive the gauges
  60.       GaugeThread gaugeThread1 = new GaugeThread(gauge1);
  61.       gaugeThread1.start();
  62.  
  63.       GaugeThread gaugeThread2 = new GaugeThread(gauge2);
  64.       gaugeThread2.start();
  65.  
  66.       GaugeThread gaugeThread3 = new GaugeThread(gauge3);
  67.       gaugeThread3.start();
  68.  
  69.       GaugeThread gaugeThread4 = new GaugeThread(gauge4);
  70.       gaugeThread4.start();
  71.  
  72.       GaugeThread gaugeThread5 = new GaugeThread(gauge5);
  73.       gaugeThread5.start();
  74.  
  75.       GaugeThread gaugeThread6 = new GaugeThread(gauge6);
  76.       gaugeThread6.start();
  77.  
  78.       GaugeThread gaugeThread7 = new GaugeThread(gauge7);
  79.       gaugeThread7.start();
  80.   }
  81.  
  82. }
  83.  
  84.  
  85. class PrettyPanel extends Container {
  86.   Image background;
  87.  
  88.   PrettyPanel(Applet applet) {
  89.       background = applet.getImage(applet.getCodeBase(),
  90.                    "images/mandrill.jpg");
  91.   }
  92.  
  93.   /**
  94.    * override update to *not* erase the background before painting
  95.    */
  96.   public void update(Graphics g) {
  97.       paint(g);
  98.   }
  99.  
  100.   /**
  101.    * paint the background picture, then call super.paint which
  102.    * will paint all contained components 
  103.    *
  104.    * Note: You MUST call super.paint(g) or the lightweight
  105.    * component(s) won't get painted.
  106.    */
  107.   public void paint(Graphics g) {
  108.       g.drawImage(background, 0, 0, getSize().width, getSize().height,
  109.                   getBackground(), this);
  110.        super.paint(g); 
  111.   }
  112. }
  113.  
  114. class GaugeThread extends Thread {
  115.  
  116.   Gauge gauge;
  117.   static int seed = 1;
  118.  
  119.   GaugeThread(Gauge gauge) {
  120.       super("Gauge thread");
  121.       this.gauge = gauge;
  122.   }
  123.  
  124.   public void run () {
  125.       Random rand = new Random(seed++);
  126.       int i = gauge.getTotalAmount()/2;
  127.       while(true) {
  128.           float r = rand.nextFloat();
  129.           if(r > .5) {
  130.           if(i < gauge.getTotalAmount())
  131.                 i+=2;
  132.           } else {
  133.           if(i > 0)
  134.         i-=2;
  135.           }
  136.       
  137.           gauge.setCurrentAmount(i);
  138.           try {
  139.               sleep(100);
  140.           } catch (java.lang.InterruptedException e) {
  141.           }
  142.       }
  143.   }
  144. }
  145.