home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / jdk114 / demo / awt-11 / lightwei / RoundBut / actual / ExampleA.jav next >
Encoding:
Text File  |  1997-09-11  |  2.4 KB  |  95 lines

  1. /*
  2.  * @(#)ExampleApplet.java    1.6 97/06/19 Jeff Dinkins
  3.  *
  4.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  */
  7. package actual;
  8.  
  9. import java.applet.*;
  10. import java.lang.*;
  11. import java.util.*;
  12. import java.awt.*;
  13. import java.awt.event.*;
  14. import actual.*;
  15.  
  16. /**
  17.  * ExampleApplet: Applet that demonstrates 
  18.  * RoundButtons.
  19.  *
  20.  * The applet creates a window that has a pretty background 
  21.  * picture, and adds an RoundButton.
  22.  *
  23.  * Notice how the corners of the lightweight Round Button component are
  24.  * "transparent" and you can see the image behind them! Cool!
  25.  */
  26. public class ExampleApplet extends Applet {
  27.  
  28.   Image background;
  29.  
  30.   public void init() {
  31.       setLayout(new FlowLayout());
  32.       loadBackgroundImage();
  33.  
  34.       // Create some round buttons, listeners, and add them to the panel
  35.       RoundButton button1  = new RoundButton("Button 1");
  36.       add(button1);
  37.  
  38.       RoundButton button2  = new RoundButton("Button 2");
  39.       add(button2);
  40.  
  41.       RoundButton button3  = new RoundButton("Java is Cool!");
  42.       add(button3);
  43.  
  44.       // Create action listener
  45.       ExampleActionListener listener = new ExampleActionListener();
  46.       button1.addActionListener(listener);
  47.       button2.addActionListener(listener);
  48.       button3.addActionListener(listener);
  49.   }
  50.  
  51.   public void loadBackgroundImage() {
  52.     //needed because ExampleApplet is running under Switcher
  53.     Applet parentApplet;
  54.     
  55.     //Get the parent Applet object. 
  56.     try {
  57.       parentApplet = (Applet)getParent();
  58.       background = parentApplet.getImage(parentApplet.getCodeBase(), "actual/images/scott.jpg");
  59.     } catch (ClassCastException e) {
  60.       System.err.println("Parent isn't an Applet!");
  61.       throw(e);
  62.     }
  63.   }
  64.   
  65.   /**
  66.    * override update to *not* erase the background before painting
  67.    */
  68.   public void update(Graphics g) {
  69.       paint(g);
  70.   }
  71.  
  72.   /**
  73.    * paint the background picture, then call super.paint which
  74.    * will paint all contained components 
  75.    */
  76.   public void paint(Graphics g) {
  77.       // paint the background image
  78.       g.drawImage(background, 0, 0, getSize().width, getSize().height,
  79.                   getBackground(), this);
  80.       super.paint(g);
  81.   }
  82.  
  83. }
  84.  
  85. class ExampleActionListener implements ActionListener {
  86.  
  87.     public ExampleActionListener() {
  88.     }
  89.  
  90.     public void actionPerformed(ActionEvent e) {
  91.         System.out.println("Button Pressed: " + e.getActionCommand());
  92.     }
  93. }
  94.  
  95.