home *** CD-ROM | disk | FTP | other *** search
/ Late Night VRML 2.0 with Java CD-ROM / code.zip / Ch12 / ui / ImageLabel.java < prev    next >
Text File  |  1996-12-30  |  9KB  |  326 lines

  1. /**                                                                     */
  2. /*              Copyright (C) 1996 ADI Ltd, Systems Group               */
  3. /*                      Engineering & Development                       */
  4. /*                          All rights reserved                         */
  5. /*                                                                      */
  6. /*              Written by      : Cameron Gillies                       */
  7. /*              Last Modified   : 21/10/96                              */
  8. /*                                                                      */
  9.  
  10. package    ui;
  11.  
  12. import  java.awt.*;
  13. import  java.awt.image.*;
  14. import  java.net.URL;
  15. import  java.net.MalformedURLException;
  16. import  java.io.*;
  17.  
  18. public class ImageLabel extends Canvas implements ImageObserver
  19. {
  20.     protected Image   img = null;
  21.     protected URL     url = null;
  22.     protected String  filename = null;
  23.     protected int     imgHeight = -1;
  24.     protected int     imgWidth = -1;
  25.     protected int     defaultWidth = 40;
  26.     protected int     defaultHeight = 40;
  27.     protected int     xPos;
  28.     protected int     yPos;
  29.     protected int     padWidth = 2;
  30.     private boolean   selected = false;
  31.  
  32.     /**
  33.     *   Constructs an image label using the image passed to it.
  34.     *   Image path must be direct pathname i.e c:\\....
  35.     *   If no position is given then the default is 0,0.
  36.     */
  37.     public ImageLabel(URL fname)
  38.     {
  39.             this(fname, 0, 0);
  40.     }
  41.  
  42.     public ImageLabel(URL fname, int x, int y)
  43.     {
  44.             setUpImages(fname);
  45.             xPos = x;
  46.             yPos = y;
  47.             // move to point requested
  48.             this.move(xPos, yPos);
  49.     }
  50.  
  51.     private URL stringToURL(String path)
  52.     {
  53.         String  urlPrefix = "file:";
  54.         File f = new File(path);
  55.         if (f.exists())
  56.         {
  57.             try
  58.             {
  59.                     //System.out.println("URL = " + urlPrefix + path);
  60.                     return new URL(urlPrefix + path);
  61.             }
  62.             catch(MalformedURLException ex)
  63.             {
  64.                     System.out.println("Malformed URL");
  65.                     return null;
  66.             }
  67.         }
  68.         else
  69.             return null;
  70.     }
  71.  
  72.     public void setSelected()
  73.     {
  74.         if(!selected)
  75.         {
  76.             selected = !selected;
  77.             repaint();
  78.         }
  79.     }
  80.  
  81.     public void resetSelected()
  82.     {
  83.         if(selected)
  84.         {
  85.             selected = !selected;
  86.             repaint();
  87.         }
  88.     }
  89.  
  90.     public boolean getSelected()
  91.     {
  92.         return selected;
  93.     }
  94.  
  95.     public void setPadWidth(int w)
  96.     {
  97.             padWidth = w;
  98.             repaint();
  99.     }
  100.  
  101.     public int getPadWidth()
  102.     {
  103.             return(padWidth);
  104.     }
  105.  
  106.     protected void setUpImages(URL fname)
  107.     {
  108.             url = fname;
  109.             if (url == null)
  110.             {
  111.                 System.out.println("Unable to open file - wrong path");
  112.                 return;
  113.             }
  114.             else
  115.             {
  116.                 img = Toolkit.getDefaultToolkit().getImage(url);
  117.                 prepareImage(img, this);
  118.             }
  119.  
  120.             //width and height might be immediately available
  121.             if (img != null)
  122.             {
  123.                 imgHeight = img.getHeight(this) ;
  124.                 imgWidth = img.getWidth(this) ;
  125.                 this.resize(imgWidth + padWidth * 2 , imgHeight + padWidth * 2 );
  126.             }
  127.     }
  128.  
  129.     public Image getImage()
  130.     {
  131.             return(img);
  132.     }
  133.  
  134.     public Dimension minimumSize()
  135.     {
  136.             if (img != null)
  137.             {
  138.                 if (imgWidth == -1 || imgHeight == -1)
  139.                 {
  140.                         imgWidth = img.getWidth(this);
  141.                         imgHeight = img.getHeight(this);
  142.                 }
  143.                 if (imgWidth == -1 || imgHeight == -1)
  144.                         return(new Dimension(defaultWidth + padWidth * 2,
  145.                                 defaultHeight + padWidth * 2));
  146.                 else
  147.                         return(new Dimension(imgWidth + padWidth * 2,
  148.                                     imgHeight + padWidth * 2));
  149.             }
  150.             else
  151.             {
  152.                 return(new Dimension(0, 0));
  153.             }
  154.     }
  155.  
  156.     public int getWidth()
  157.     {
  158.         return(size().width);
  159.     }
  160.  
  161.     public int getHeight()
  162.     {
  163.         return(size().height);
  164.     }
  165.  
  166.     public Dimension preferredSize()
  167.     {
  168.             return minimumSize();
  169.     }
  170.  
  171.     /* Invalidate all of a component's containers and then validate the
  172.     * Window at the top.  Call this when the size of a component
  173.     * changes and you wish to make the window that contains it resize
  174.     * to accomodate the new size.
  175.     */
  176.     protected void updateWindow(Component c)
  177.     {
  178.             while (c != null)
  179.             {
  180.                 c.invalidate();
  181.                 if (c instanceof Window)
  182.                 {
  183.                         c.validate();
  184.                         break;
  185.                 }
  186.                 c = c.getParent();
  187.             }
  188.     }
  189.  
  190.     /**
  191.     * Figures out if this component needs to be resized.
  192.     */
  193.     protected void updateSize(int w, int h)
  194.     {
  195.             if (w >= 0 && h >= 0)
  196.             {
  197.                 Dimension d = size();
  198.                 if (d.width != w + padWidth * 2 || d.height != h + padWidth * 2)
  199.                 {
  200.                         resize(w + padWidth * 2 , h + padWidth * 2 );
  201.                         updateWindow(this);
  202.                 }
  203.             }
  204.     }
  205.  
  206.     /**
  207.     * By overriding update we insure that this component won't be
  208.     * completely cleared with the background color each time it's
  209.     * updated (while loading.)  We'd like less flickering than that.
  210.     */
  211.     public void update(Graphics g)
  212.     {
  213.             g.setColor(getBackground());
  214.             Dimension d = size();
  215.             if (img != null && (imgWidth >= 0 && imgHeight >= 0))
  216.             {
  217.                 // clear only the areas around the image (to avoid having the image
  218.                 // flicker as it is loaded scanline-by-scanline)
  219.                     int x = (d.width - imgWidth) / 2;
  220.                     int y = (d.height - imgHeight) / 2;
  221.                     if (x > 0)
  222.                         g.fillRect(0, 0, x, d.height);
  223.                     if (y > 0)
  224.                         g.fillRect(0, 0, d.width, y);
  225.                     if (d.width > imgWidth)
  226.                         g.fillRect(x + imgWidth, 0, d.width - (x + imgWidth), d.height);
  227.                     if (d.height > imgHeight)
  228.                         g.fillRect(0, y + imgHeight, d.width, d.height - (y + imgHeight));
  229.             }
  230.             else
  231.             {
  232.                 // there is no image, so clear the whole area
  233.                 g.fillRect(0, 0, d.width, d.height);
  234.             }
  235.             g.setColor(getForeground());
  236.             paint(g);
  237.     }
  238.  
  239.     public void paint(Graphics g)
  240.     {
  241.  
  242.         if (img != null && (imgWidth == -1 || imgHeight == -1))
  243.             {
  244.                 imgHeight = img.getHeight(this) ;
  245.                 imgWidth = img.getWidth(this) ;
  246.                 this.resize(imgWidth + padWidth * 2 , imgHeight + padWidth * 2 );
  247.             }
  248.  
  249.         if (imgWidth >= 0 && imgHeight >= 0)
  250.         {
  251.                 Dimension d = size();
  252.                 int x = (d.width - imgWidth) / 2;
  253.                 int y = (d.height - imgHeight) / 2;
  254.  
  255.                 // draw the image with no border
  256.                 g.drawImage(img, x , y , getBackground(), this);
  257.                 g.setColor(Color.black);
  258.                 if (selected)
  259.                     g.drawRect(0,0,d.width-1,d.height-1);
  260.         }
  261.     }
  262.  
  263.     public boolean imageUpdate(Image img, int flags, int x, int y, int w, int h)
  264.         {
  265.  
  266.             if ((flags & ERROR) == 0)
  267.             {
  268.                 boolean updateSize = false;
  269.  
  270.                 if ((flags & WIDTH) != 0)
  271.                 {
  272.                         imgWidth = w;
  273.                         updateSize = true;
  274.                 }
  275.                 if ((flags & HEIGHT) != 0)
  276.                 {
  277.                         imgHeight = h;
  278.                         updateSize = true;
  279.                 }
  280.  
  281.                 if (updateSize && imgWidth >= 0 && imgHeight >= 0)
  282.                 {
  283.                 //As soon as the size is known this component needs to resize itself.
  284.                         updateSize(imgWidth, imgHeight);
  285.  
  286.                 // This repaint is needed for images that are already loaded, and
  287.                 // are being loaded a second time.  In this situation, the
  288.                 // update for the width and height comes in after all the other
  289.                 // updates.  The call to super.imageUpdate does not do a repaint
  290.                 // when the size changes, so we need to do one here.
  291.                     repaint();
  292.                 }
  293.             }
  294.         return super.imageUpdate(img, flags, x, y, w, h);
  295.     }
  296.  
  297.     public synchronized void addNotify()
  298.     {
  299.         super.addNotify();
  300.         repaint();
  301.     }
  302.  
  303.     public boolean mouseDown(Event e, int x, int y)
  304.     {
  305.         selected = !selected;
  306.         repaint();
  307.         return true;
  308.     }
  309.  
  310.     public boolean mouseUp(Event evt, int x, int y)
  311.     {
  312.         if (selected)
  313.         {
  314.         action();
  315.         //depressed = false;
  316.         //repaint();
  317.         }
  318.         return true;
  319.     }
  320.  
  321.     public void action()
  322.     {
  323.         postEvent(new Event(this, Event.ACTION_EVENT, null));
  324.     }
  325. }
  326.