home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / intl / datamgmt / example-1.1 / FlagCanvas.java < prev    next >
Encoding:
Java Source  |  1997-07-13  |  2.4 KB  |  96 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.awt.event.MouseAdapter;
  19. import java.awt.event.MouseEvent;
  20.  
  21. class FlagCanvas extends Canvas {
  22.  
  23.     private boolean selected = false;
  24.  
  25.     Container pappy;
  26.     Image image;
  27.     boolean trueSizeKnown = false;
  28.     Dimension minSize;
  29.     int w, h;
  30.  
  31.     public FlagCanvas(Image image, IntlWindow parent, 
  32.                       int initialWidth, int initialHeight) {
  33.     if (image == null) {
  34.         System.err.println("Canvas got invalid image object!");
  35.         return;
  36.     }
  37.  
  38.     this.image = image;
  39.         pappy = parent;
  40.  
  41.     w = initialWidth+2;
  42.     h = initialHeight+2;
  43.  
  44.     minSize = new Dimension(w,h);
  45.  
  46.         addMouseListener(new MyAdapter());
  47.     }
  48.  
  49.     class MyAdapter extends MouseAdapter {
  50.         public void mouseClicked(MouseEvent evt) {
  51.         ((IntlWindow)pappy).selectFlag(this$0);
  52.         }
  53.     }
  54.  
  55.     public boolean isSelected() {
  56.     return selected;
  57.     }
  58.  
  59.     public void setSelected(boolean check) {
  60.     selected = check;
  61.     repaint();
  62.     }
  63.  
  64.     public Dimension getPreferredSize() {
  65.      return getMinimumSize();
  66.     }
  67.  
  68.     public synchronized Dimension getMinimumSize() {
  69.     return minSize;
  70.     }
  71.  
  72.     public void paint (Graphics g) {
  73.     if (image != null) {
  74.         if (!trueSizeKnown) {
  75.             int imageWidth = image.getWidth(this);
  76.             int imageHeight = image.getHeight(this);
  77.  
  78.             if ((imageWidth > 0) && (imageHeight > 0)) {
  79.             trueSizeKnown = true;
  80.  
  81.             //Component-initiated resizing.
  82.             w = imageWidth + 2;
  83.             h = imageHeight + 2;
  84.             minSize = new Dimension(w,h);
  85.             setSize(w, h);
  86.             pappy.validate();
  87.             }
  88.         }
  89.  
  90.         g.drawImage(image, 1, 1, this);
  91.         if (selected)
  92.             g.draw3DRect(0, 0, w-1, h-1, true);
  93.     }
  94.     }
  95. }
  96.