home *** CD-ROM | disk | FTP | other *** search
/ Freesoft 1997 May / Freesoft_1997-05_cd.bin / recenz / PROGRAM / JAVADRAW / iavadraw301_inst.exe / data.z / PictureCanvas.java < prev    next >
Text File  |  1997-05-20  |  2KB  |  73 lines

  1. /*
  2.  * Copyright (c) 1995, 1996 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. class PictureCanvas extends Canvas {
  20.     Container pappy;
  21.     Image image;
  22.     boolean trueSizeKnown = false;
  23.     Dimension minSize;
  24.     int w, h;
  25.  
  26.     public PictureCanvas(Image image, Container parent, 
  27.                       int initialWidth, int initialHeight) {
  28.     if (image == null) {
  29.         System.err.println("Canvas got invalid image object!");
  30.         return;
  31.     }
  32.  
  33.     this.image = image;
  34.         pappy = parent;
  35.  
  36.     w = initialWidth;
  37.     h = initialHeight;
  38.  
  39.     minSize = new Dimension(w,h);
  40.     }
  41.  
  42.     public Dimension preferredSize() {
  43.      return minimumSize();
  44.     }
  45.  
  46.     public synchronized Dimension minimumSize() {
  47.     return minSize;
  48.     }
  49.  
  50.     public void paint (Graphics g) {
  51.     if (image != null) {
  52.         if (!trueSizeKnown) {
  53.             int imageWidth = image.getWidth(this);
  54.             int imageHeight = image.getHeight(this);
  55.  
  56.             if ((imageWidth > 0) && (imageHeight > 0)) {
  57.             trueSizeKnown = true;
  58.  
  59.             //Component-initiated resizing.
  60.             w = imageWidth;
  61.             h = imageHeight;
  62.             minSize = new Dimension(w,h);
  63.             setSize(w, h);
  64.             pappy.validate();
  65.             }
  66.         }
  67.  
  68.         g.drawImage(image, 0, 0, this);
  69.     }
  70.     }
  71.  
  72. }
  73.