home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / ui / components / example / ImageApplet.java < prev    next >
Encoding:
Java Source  |  1997-07-13  |  2.4 KB  |  92 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. import java.applet.Applet;
  19.  
  20. class ImageCanvas extends Canvas {
  21.     Container pappy;
  22.     Image image;
  23.     boolean trueSizeKnown = false;
  24.     Dimension minSize;
  25.     int w, h;
  26.  
  27.     public ImageCanvas(Image image, Container parent, 
  28.                       int initialWidth, int initialHeight) {
  29.     if (image == null) {
  30.         System.err.println("Canvas got invalid image object!");
  31.         return;
  32.     }
  33.  
  34.     this.image = image;
  35.         pappy = parent;
  36.  
  37.     w = initialWidth;
  38.     h = initialHeight;
  39.  
  40.     minSize = new Dimension(w,h);
  41.     }
  42.  
  43.     public Dimension preferredSize() {
  44.      return minimumSize();
  45.     }
  46.  
  47.     public synchronized Dimension minimumSize() {
  48.     return minSize;
  49.     }
  50.  
  51.     public void paint (Graphics g) {
  52.     if (image != null) {
  53.         if (!trueSizeKnown) {
  54.             int imageWidth = image.getWidth(this);
  55.             int imageHeight = image.getHeight(this);
  56.  
  57.             if ((imageWidth > 0) && (imageHeight > 0)) {
  58.             trueSizeKnown = true;
  59.  
  60.             //Component-initiated resizing.
  61.             w = imageWidth;
  62.             h = imageHeight;
  63.             minSize = new Dimension(w,h);
  64.             resize(w, h);
  65.             pappy.validate();
  66.             }
  67.         }
  68.  
  69.         g.drawImage(image, 0, 0, this);
  70.         g.drawRect(0, 0, w - 1, h - 1);
  71.     }
  72.     }
  73.  
  74. }
  75.  
  76.  
  77. public class ImageApplet extends Applet {
  78.     public void init() { 
  79.         Image image1 = getImage(getCodeBase(), "../images/kwalrath.gif");
  80.         Image image2 = getImage(getCodeBase(), "../images/innocence_small.gif");
  81.  
  82.         ImageCanvas ic1 = new ImageCanvas(image1, this, 50, 50);
  83.         ImageCanvas ic2 = new ImageCanvas(image2, this, 100, 100);
  84.  
  85.         add(ic1);
  86.         add(ic2);
  87.     
  88.     validate();
  89.     }
  90.  
  91. }
  92.