home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / jdk114 / demo / awt-11 / lightwei / Spinner / actual / ExampleA.jav next >
Encoding:
Text File  |  1997-09-11  |  2.5 KB  |  105 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.lang.*;
  12. import java.util.*;
  13. import java.awt.*;
  14.  
  15. /**
  16.  * ExampleApplet: Applet that demonstrates the
  17.  * Spinner component.
  18.  *
  19.  * The applet creates a window that has a pretty 
  20.  * background picture in it, and throws some lightweight
  21.  * Spinner components in. Notice how the lightweight
  22.  * Spinner component has "transparent" areas that let you see
  23.  * the image behind it! Cool!
  24.  */
  25. public class ExampleApplet extends Applet {
  26.  
  27.   Image orb;
  28.  
  29.   GridBagLayout gridbag = new GridBagLayout();
  30.   GridBagConstraints c = new GridBagConstraints();
  31.  
  32.   public void init() {
  33.       setLayout(gridbag);
  34.  
  35.       loadBackgroundImage();
  36.       initializeGridbag();
  37.  
  38.       // ************* Create Spinners
  39.       Spinner  spinner1  = new Spinner();
  40.       Spinner  spinner2  = new Spinner();
  41.  
  42.       // Set the color of the two spinners
  43.       spinner1.setBackground(Color.yellow);
  44.       spinner1.setForeground(Color.red);
  45.  
  46.       spinner2.setBackground(new Color(100, 0, 255));
  47.       spinner2.setForeground(new Color(50, 255, 255));
  48.  
  49.       // ************* add components
  50.       // add spinner1 
  51.       c.gridx = 0;     c.gridy = 0;
  52.       gridbag.setConstraints(spinner1, c);
  53.       add(spinner1);
  54.  
  55.       // add spinner2
  56.       c.gridx = 1;     c.gridy = 1;
  57.       gridbag.setConstraints(spinner2, c);
  58.       add(spinner2);
  59.  
  60.       spinner1.startSpinning();
  61.       spinner2.startSpinning();
  62.   }
  63.  
  64.   public void initializeGridbag() {
  65.       c.gridwidth = 1; c.gridheight = 1;
  66.       c.weightx = 1;   c.weighty = 1;
  67.       c.fill = GridBagConstraints.BOTH;
  68.       c.anchor= GridBagConstraints.NORTHWEST;
  69.   }
  70.  
  71.   public void loadBackgroundImage() {
  72.     //needed because ExampleApplet is running under Switcher
  73.     Applet parentApplet;
  74.     
  75.     //Get the parent Applet object. 
  76.     try {
  77.       parentApplet = (Applet)getParent();
  78.       orb = parentApplet.getImage(parentApplet.getCodeBase(), 
  79.                   "actual/images/orb.gif"); 
  80.     } catch (ClassCastException e) {
  81.       System.err.println("Parent isn't an Applet!");
  82.       throw(e);
  83.     }
  84.   }
  85.   
  86.   /**
  87.    * override update to *not* erase the background before painting
  88.    */
  89.   public void update(Graphics g) {
  90.       paint(g);
  91.   }
  92.  
  93.   /**
  94.    * paint the background picture, then call super.paint which
  95.    * will paint all contained components 
  96.    */
  97.   public void paint(Graphics g) {
  98.       g.drawImage(orb, 0, 0, getSize().width, getSize().height, 
  99.           getBackground(), this);
  100.       super.paint(g);
  101.   }
  102.  
  103. }
  104.  
  105.