home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / java / threads / example / PictureFrame.java < prev    next >
Encoding:
Java Source  |  1997-07-13  |  2.2 KB  |  76 lines

  1. /*
  2.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. import java.awt.*;
  18.  
  19. public class PictureFrame extends Frame {
  20.     boolean inAnApplet = true;
  21.     Image image;
  22.  
  23.     PictureFrame(Image image, int width, int height) {
  24.     super("PictureFrame");
  25.  
  26.     GridBagLayout gridbag = new GridBagLayout();
  27.     GridBagConstraints c = new GridBagConstraints();
  28.     setLayout(gridbag);
  29.  
  30.     //Create the image and start downloading it.
  31.     MediaTracker tracker = new MediaTracker(this);
  32.     this.image = image;
  33.     tracker.addImage(image, 0);
  34.     tracker.checkAll(true);
  35.  
  36.     c.gridwidth = GridBagConstraints.REMAINDER;
  37.     c.weightx = 1.0;
  38.     Label label1 = new Label("Here's what you'd see if you were using "
  39.                          + "a 1.1-compatible browser:");
  40.     gridbag.setConstraints(label1, c);
  41.     add(label1);
  42.  
  43.     c.weighty = 1.0;
  44.     ImageDisplayer imageDisplayer = new ImageDisplayer(image,
  45.                                width,
  46.                                height);
  47.     gridbag.setConstraints(imageDisplayer, c);
  48.     add(imageDisplayer);
  49.  
  50.     c.weighty = 0.0;
  51.     Label label2 = new Label("Remember, this is just a picture!");
  52.     label2.setForeground(Color.red);
  53.     gridbag.setConstraints(label2, c);
  54.     add(label2);
  55.     }
  56.  
  57.     public boolean handleEvent(Event e) {
  58.     if (e.id == Event.WINDOW_DESTROY) {
  59.         if (inAnApplet) {
  60.         dispose();
  61.         } else {
  62.         System.exit(0);
  63.         }
  64.     }
  65.     return super.handleEvent(e);
  66.     }
  67.  
  68.     public static void main(String[] args) {
  69.     PictureFrame frame = 
  70.         new PictureFrame(Toolkit.getDefaultToolkit().getImage("Beeper.gif"), 200, 200);
  71.     frame.inAnApplet = false;
  72.         frame.pack();
  73.         frame.show();
  74.     }
  75. }
  76.