home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / jdk114 / demo / awt-11 / lightwei / Openlook / actual / ExampleA.jav next >
Encoding:
Text File  |  1997-09-11  |  2.4 KB  |  99 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. import java.awt.event.*;
  15.  
  16. /**
  17.  * ExampleApplet: Applet that demonstrates 
  18.  * OpenlookButtons.
  19.  *
  20.  * The applet creates a window that has a pretty background 
  21.  * picture, and adds an OpenlookButton.
  22.  *
  23.  * Notice how the lightweight Openlook Button component has "transparent" 
  24.  * corners 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 buttons
  35.       OpenlookButton button1  = new OpenlookButton("Motif sucks");
  36.       add(button1);
  37.  
  38.       OpenlookButton button2  = new OpenlookButton("I miss Openlook!");
  39.       add(button2);
  40.  
  41.       OpenlookButton button3  = new OpenlookButton("Java is Cool!");
  42.       add(button3);
  43.  
  44.       // *** Create button 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.       
  53.     //needed because this is running under Switcher
  54.     Applet parentApplet;
  55.     
  56.     /* Get the parent Applet object. */
  57.     try {
  58.       parentApplet = (Applet)getParent();
  59.       background = parentApplet.getImage(parentApplet.getCodeBase(), 
  60.                      "actual/images/scott.jpg");
  61.     } catch (ClassCastException e) {
  62.       System.err.println("Parent isn't an Applet!");
  63.       throw(e);
  64.     }  
  65.   }
  66.  
  67.   /**
  68.    * override update to *not* erase the background before painting
  69.    */
  70.   public void update(Graphics g) {
  71.       paint(g);
  72.   }
  73.  
  74.   /**
  75.    * paint the background picture, then call super.paint which
  76.    * will paint all contained components 
  77.    *
  78.    * NOTE: You MUST call super.paint(g) or the lightweight 
  79.    * component(s) won't get painted.
  80.    */
  81.   public void paint(Graphics g) {
  82.       g.drawImage(background, 0, 0, getSize().width, getSize().height,
  83.                   getBackground(), this);
  84.       super.paint(g);
  85.   }
  86.  
  87. }
  88.  
  89. class ExampleActionListener implements ActionListener {
  90.  
  91.     public ExampleActionListener() {
  92.     }
  93.  
  94.     public void actionPerformed(ActionEvent e) {
  95.         System.out.println("Button Pressed: " + e.getActionCommand());
  96.     }
  97. }
  98.  
  99.