home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-12-30 | 9.2 KB | 326 lines |
- /** */
- /* Copyright (C) 1996 ADI Ltd, Systems Group */
- /* Engineering & Development */
- /* All rights reserved */
- /* */
- /* Written by : Cameron Gillies */
- /* Last Modified : 21/10/96 */
- /* */
-
- package ui;
-
- import java.awt.*;
- import java.awt.image.*;
- import java.net.URL;
- import java.net.MalformedURLException;
- import java.io.*;
-
- public class ImageLabel extends Canvas implements ImageObserver
- {
- protected Image img = null;
- protected URL url = null;
- protected String filename = null;
- protected int imgHeight = -1;
- protected int imgWidth = -1;
- protected int defaultWidth = 40;
- protected int defaultHeight = 40;
- protected int xPos;
- protected int yPos;
- protected int padWidth = 2;
- private boolean selected = false;
-
- /**
- * Constructs an image label using the image passed to it.
- * Image path must be direct pathname i.e c:\\....
- * If no position is given then the default is 0,0.
- */
- public ImageLabel(URL fname)
- {
- this(fname, 0, 0);
- }
-
- public ImageLabel(URL fname, int x, int y)
- {
- setUpImages(fname);
- xPos = x;
- yPos = y;
- // move to point requested
- this.move(xPos, yPos);
- }
-
- private URL stringToURL(String path)
- {
- String urlPrefix = "file:";
- File f = new File(path);
- if (f.exists())
- {
- try
- {
- //System.out.println("URL = " + urlPrefix + path);
- return new URL(urlPrefix + path);
- }
- catch(MalformedURLException ex)
- {
- System.out.println("Malformed URL");
- return null;
- }
- }
- else
- return null;
- }
-
- public void setSelected()
- {
- if(!selected)
- {
- selected = !selected;
- repaint();
- }
- }
-
- public void resetSelected()
- {
- if(selected)
- {
- selected = !selected;
- repaint();
- }
- }
-
- public boolean getSelected()
- {
- return selected;
- }
-
- public void setPadWidth(int w)
- {
- padWidth = w;
- repaint();
- }
-
- public int getPadWidth()
- {
- return(padWidth);
- }
-
- protected void setUpImages(URL fname)
- {
- url = fname;
- if (url == null)
- {
- System.out.println("Unable to open file - wrong path");
- return;
- }
- else
- {
- img = Toolkit.getDefaultToolkit().getImage(url);
- prepareImage(img, this);
- }
-
- //width and height might be immediately available
- if (img != null)
- {
- imgHeight = img.getHeight(this) ;
- imgWidth = img.getWidth(this) ;
- this.resize(imgWidth + padWidth * 2 , imgHeight + padWidth * 2 );
- }
- }
-
- public Image getImage()
- {
- return(img);
- }
-
- public Dimension minimumSize()
- {
- if (img != null)
- {
- if (imgWidth == -1 || imgHeight == -1)
- {
- imgWidth = img.getWidth(this);
- imgHeight = img.getHeight(this);
- }
- if (imgWidth == -1 || imgHeight == -1)
- return(new Dimension(defaultWidth + padWidth * 2,
- defaultHeight + padWidth * 2));
- else
- return(new Dimension(imgWidth + padWidth * 2,
- imgHeight + padWidth * 2));
- }
- else
- {
- return(new Dimension(0, 0));
- }
- }
-
- public int getWidth()
- {
- return(size().width);
- }
-
- public int getHeight()
- {
- return(size().height);
- }
-
- public Dimension preferredSize()
- {
- return minimumSize();
- }
-
- /* Invalidate all of a component's containers and then validate the
- * Window at the top. Call this when the size of a component
- * changes and you wish to make the window that contains it resize
- * to accomodate the new size.
- */
- protected void updateWindow(Component c)
- {
- while (c != null)
- {
- c.invalidate();
- if (c instanceof Window)
- {
- c.validate();
- break;
- }
- c = c.getParent();
- }
- }
-
- /**
- * Figures out if this component needs to be resized.
- */
- protected void updateSize(int w, int h)
- {
- if (w >= 0 && h >= 0)
- {
- Dimension d = size();
- if (d.width != w + padWidth * 2 || d.height != h + padWidth * 2)
- {
- resize(w + padWidth * 2 , h + padWidth * 2 );
- updateWindow(this);
- }
- }
- }
-
- /**
- * By overriding update we insure that this component won't be
- * completely cleared with the background color each time it's
- * updated (while loading.) We'd like less flickering than that.
- */
- public void update(Graphics g)
- {
- g.setColor(getBackground());
- Dimension d = size();
- if (img != null && (imgWidth >= 0 && imgHeight >= 0))
- {
- // clear only the areas around the image (to avoid having the image
- // flicker as it is loaded scanline-by-scanline)
- int x = (d.width - imgWidth) / 2;
- int y = (d.height - imgHeight) / 2;
- if (x > 0)
- g.fillRect(0, 0, x, d.height);
- if (y > 0)
- g.fillRect(0, 0, d.width, y);
- if (d.width > imgWidth)
- g.fillRect(x + imgWidth, 0, d.width - (x + imgWidth), d.height);
- if (d.height > imgHeight)
- g.fillRect(0, y + imgHeight, d.width, d.height - (y + imgHeight));
- }
- else
- {
- // there is no image, so clear the whole area
- g.fillRect(0, 0, d.width, d.height);
- }
- g.setColor(getForeground());
- paint(g);
- }
-
- public void paint(Graphics g)
- {
-
- if (img != null && (imgWidth == -1 || imgHeight == -1))
- {
- imgHeight = img.getHeight(this) ;
- imgWidth = img.getWidth(this) ;
- this.resize(imgWidth + padWidth * 2 , imgHeight + padWidth * 2 );
- }
-
- if (imgWidth >= 0 && imgHeight >= 0)
- {
- Dimension d = size();
- int x = (d.width - imgWidth) / 2;
- int y = (d.height - imgHeight) / 2;
-
- // draw the image with no border
- g.drawImage(img, x , y , getBackground(), this);
- g.setColor(Color.black);
- if (selected)
- g.drawRect(0,0,d.width-1,d.height-1);
- }
- }
-
- public boolean imageUpdate(Image img, int flags, int x, int y, int w, int h)
- {
-
- if ((flags & ERROR) == 0)
- {
- boolean updateSize = false;
-
- if ((flags & WIDTH) != 0)
- {
- imgWidth = w;
- updateSize = true;
- }
- if ((flags & HEIGHT) != 0)
- {
- imgHeight = h;
- updateSize = true;
- }
-
- if (updateSize && imgWidth >= 0 && imgHeight >= 0)
- {
- //As soon as the size is known this component needs to resize itself.
- updateSize(imgWidth, imgHeight);
-
- // This repaint is needed for images that are already loaded, and
- // are being loaded a second time. In this situation, the
- // update for the width and height comes in after all the other
- // updates. The call to super.imageUpdate does not do a repaint
- // when the size changes, so we need to do one here.
- repaint();
- }
- }
- return super.imageUpdate(img, flags, x, y, w, h);
- }
-
- public synchronized void addNotify()
- {
- super.addNotify();
- repaint();
- }
-
- public boolean mouseDown(Event e, int x, int y)
- {
- selected = !selected;
- repaint();
- return true;
- }
-
- public boolean mouseUp(Event evt, int x, int y)
- {
- if (selected)
- {
- action();
- //depressed = false;
- //repaint();
- }
- return true;
- }
-
- public void action()
- {
- postEvent(new Event(this, Event.ACTION_EVENT, null));
- }
- }
-