jm.sit
Class ImageLabel

java.lang.Object
  |
  +--java.awt.Component
        |
        +--java.awt.Canvas
              |
              +--jm.sit.ImageLabel
All Implemented Interfaces:
javax.accessibility.Accessible, java.awt.image.ImageObserver, java.awt.MenuContainer, java.io.Serializable

public class ImageLabel
extends java.awt.Canvas

A class for displaying images. It places the Image into a canvas so that it can moved around by layout managers, will get repainted automatically, etc. No mouseXXX or action events are defined, so it is most similar to the Label Component.

By default, with FlowLayout the ImageLabel takes its minimum size (just enclosing the image). The default with BorderLayout is to expand to fill the region in width (North/South), height (East/West) or both (Center). This is the same behavior as with the builtin Label class. If you give an explicit resize or reshape call before adding the ImageLabel to the Container, this size will override the defaults.

Here is an example of its use:

 public class ShowImages extends Applet {
 private ImageLabel image1, image2;

 public void init() {
 image1 = new ImageLabel(getCodeBase(),
 "some-image.gif");
 image2 = new ImageLabel(getCodeBase(),
 "other-image.jpg");
 add(image1);
 add(image2);
 }
 }
 
th 02.09.29: Removed Java 1.1 deprecated methods.

See Also:
Serialized Form

Nested Class Summary
 
Nested classes inherited from class java.awt.Canvas
java.awt.Canvas.AccessibleAWTCanvas
 
Nested classes inherited from class java.awt.Component
java.awt.Component.AccessibleAWTComponent, java.awt.Component.BltBufferStrategy, java.awt.Component.FlipBufferStrategy
 
Field Summary
 
Fields inherited from class java.awt.Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Constructor Summary
ImageLabel()
          Create an ImageLabel with the default image.
ImageLabel(java.awt.Image image)
          Create an ImageLabel using the image specified.
ImageLabel(java.lang.String imageURLString)
          Create an ImageLabel using the image at URL specified by the string.
ImageLabel(java.net.URL imageURL)
          Create an ImageLabel using the image at URL specified.
ImageLabel(java.net.URL imageDirectory, java.lang.String file)
          Create an ImageLabel using the image in the file in the specified directory.
 
Method Summary
 void centerAt(int x, int y)
          Moves the image so that it is centered at the specified location, as opposed to the move method of Component which places the top left corner at the specified location.
 boolean contains(int x, int y)
          Determines if the x and y (in the ImageLabel's own coordinate system) is inside the ImageLabel.
protected  void debug(java.lang.String message)
          Calls System.out.println if the debug variable is true; does nothing otherwise.
protected  void drawRect(java.awt.Graphics g, int left, int top, int width, int height, int lineThickness, java.awt.Color rectangleColor)
          Draws a rectangle with the specified OUTSIDE left, top, width, and height.
 int getBorder()
          Gets the border width.
 java.awt.Color getBorderColor()
          Gets the border color.
static java.lang.String getDefaultImageString()
          Returns the string representing the URL that will be used if none is supplied in the constructor.
 int getHeight()
          Gets the height (image height plus 2x border).
 java.awt.Image getImage()
          The Image associated with the ImageLabel.
protected  java.lang.String getImageString()
          Returns the string representing the URL of image.
 java.awt.Dimension getMinimumSize()
          Used by layout managers to calculate the smallest size allocated for the Component.
 java.awt.Dimension getPreferredSize()
          Used by layout managers to calculate the usual size allocated for the Component.
 int getWidth()
          Gets the width (image width plus twice border).
protected  boolean hasExplicitSize()
          Has the ImageLabel been given an explicit size?
 boolean isDebugging()
          Is the debugging flag set?
 void paint(java.awt.Graphics g)
          Draws the image.
 void setBorder(int border)
          Sets the border thickness.
 void setBorderColor(java.awt.Color borderColor)
          Sets the border color.
 void setBounds(int x, int y, int width, int height)
          Resizes the ImageLabel.
static void setDefaultImageString(java.lang.String file)
          Sets the string representing the URL that will be used if none is supplied in the constructor.
 void setImage(java.awt.Image img)
          Sets Image associated with the ImageLabel.
 void setIsDebugging(boolean debug)
          Set the debugging flag.
 void setSize(int width, int height)
          Resizes the ImageLabel.
 void waitForImage(boolean doLayout)
          Makes sure that the Image associated with the Canvas is done loading before returning, since loadImage spins off a separate thread to do the loading.
 
Methods inherited from class java.awt.Canvas
addNotify, createBufferStrategy, createBufferStrategy, getAccessibleContext, getBufferStrategy, update
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, deliverEvent, disable, disableEvents, dispatchEvent, doLayout, enable, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, getAlignmentX, getAlignmentY, getBackground, getBounds, getBounds, getColorModel, getComponentAt, getComponentAt, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeys, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphics, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getListeners, getLocale, getLocation, getLocation, getLocationOnScreen, getMaximumSize, getMouseListeners, getMouseMotionListeners, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getSize, getToolkit, getTreeLock, getX, getY, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, invalidate, isBackgroundSet, isCursorSet, isDisplayable, isDoubleBuffered, isEnabled, isFocusable, isFocusCycleRoot, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isOpaque, isShowing, isValid, isVisible, keyDown, keyUp, layout, list, list, list, list, list, locate, location, lostFocus, minimumSize, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, paramString, postEvent, preferredSize, prepareImage, prepareImage, print, printAll, processComponentEvent, processEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removeNotify, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, reshape, resize, resize, setBackground, setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setFocusable, setFocusTraversalKeys, setFocusTraversalKeysEnabled, setFont, setForeground, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setVisible, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle, validate
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ImageLabel

public ImageLabel()
Create an ImageLabel with the default image.

See Also:
getDefaultImageString(), setDefaultImageString(java.lang.String)

ImageLabel

public ImageLabel(java.lang.String imageURLString)
Create an ImageLabel using the image at URL specified by the string.

Parameters:
imageURLString - A String specifying the URL of the image.

ImageLabel

public ImageLabel(java.net.URL imageURL)
Create an ImageLabel using the image at URL specified.

Parameters:
imageURL - The URL of the image.

ImageLabel

public ImageLabel(java.net.URL imageDirectory,
                  java.lang.String file)
Create an ImageLabel using the image in the file in the specified directory.

Parameters:
imageDirectory - Directory containing image
file - Filename of image

ImageLabel

public ImageLabel(java.awt.Image image)
Create an ImageLabel using the image specified. The other constructors eventually call this one, but you may want to call it directly if you already have an image (e.g. created via createImage).

Parameters:
image - The image
Method Detail

waitForImage

public void waitForImage(boolean doLayout)
Makes sure that the Image associated with the Canvas is done loading before returning, since loadImage spins off a separate thread to do the loading. Once you get around to drawing the image, this will make sure it is loaded, waiting if not. The user does not need to call this at all, but if several ImageLabels are used in the same Container, this can cause several repeated layouts, so users might want to explicitly call this themselves before adding the ImageLabel to the Container. Another alternative is to start asynchronous loading by calling prepareImage on the ImageLabel's image (see getImage).

Parameters:
doLayout - Determines if the Container should be re-laid out after you are finished waiting. This should be true when called from user functions, but is set to false when called from preferredSize to avoid an infinite loop. This is needed when using BorderLayout, which calls preferredSize before calling paint.

centerAt

public void centerAt(int x,
                     int y)
Moves the image so that it is centered at the specified location, as opposed to the move method of Component which places the top left corner at the specified location.

Note: The effects of this could be undone by the LayoutManager of the parent Container, if it is using one. So this is normally only used in conjunction with a null LayoutManager.

Parameters:
x - The X coord of center of the image (in parent's coordinate system)
y - The Y coord of center of the image (in parent's coordinate system)

contains

public boolean contains(int x,
                        int y)
Determines if the x and y (in the ImageLabel's own coordinate system) is inside the ImageLabel. Put here because Netscape 2.02 has a bug in which it doesn't process inside() and locate() tests correctly. th 02.09.29: Renamed: public synchronized boolean inside(int x, int y) {

Overrides:
contains in class java.awt.Component

paint

public void paint(java.awt.Graphics g)
Draws the image. If you override this in a subclass, be sure to call super.paint.

Overrides:
paint in class java.awt.Canvas

getPreferredSize

public java.awt.Dimension getPreferredSize()
Used by layout managers to calculate the usual size allocated for the Component. Since some layout managers (e.g. BorderLayout) may call this before paint is called, you need to make sure that the image is done loading, which will force a resize, which determines the values returned. th 02.09.29: Removed: public Dimension preferredSize() { // depreciated ! return (this.getPreferredSize()); }

Overrides:
getPreferredSize in class java.awt.Component

getMinimumSize

public java.awt.Dimension getMinimumSize()
Used by layout managers to calculate the smallest size allocated for the Component. Since some layout managers (e.g. BorderLayout) may call this before paint is called, you need to make sure that the image is done loading, which will force a resize, which determines the values returned. th 02.09.29: Removed: public Dimension minimumSize() { //depreciated return (this.getMinimumSize()); }

Overrides:
getMinimumSize in class java.awt.Component

setSize

public void setSize(int width,
                    int height)
Resizes the ImageLabel. If you don't resize the label explicitly, then what happens depends on the layout manager. With FlowLayout, as with FlowLayout for Labels, the ImageLabel takes its minimum size, just enclosing the image. With BorderLayout, as with BorderLayout for Labels, the ImageLabel is expanded to fill the section. Stretching GIF/JPG files does not always result in clear looking images. So just as with builtin Labels and Buttons, don't use FlowLayout if you don't want the Buttons to get resized. If you don't use any LayoutManager, then the ImageLabel will also just fit the image.

Note that if you resize explicitly, you must do it before the ImageLabel is added to the Container. In such a case, the explicit size overrides the image dimensions. th 02.09.29: Removed: public void resize(int width, int height) { // depreciated this.setSize(width, height); }

Overrides:
setSize in class java.awt.Component
See Also:
Component.reshape(int, int, int, int)

setBounds

public void setBounds(int x,
                      int y,
                      int width,
                      int height)
Resizes the ImageLabel. If you don't resize the label explicitly, then what happens depends on the layout manager. With FlowLayout, as with FlowLayout for Labels, the ImageLabel takes its minimum size, just enclosing the image. With BorderLayout, as with BorderLayout for Labels, the ImageLabel is expanded to fill the section. Stretching GIF/JPG files does not always result in clear looking images. So just as with builtin Labels and Buttons, don't use FlowLayout if you don't want the Buttons to get resized. If you don't use any LayoutManager, then the ImageLabel will also just fit the image.

Note that if you resize explicitly, you must do it before the ImageLabel is added to the Container. In such a case, the explicit size overrides the image dimensions. th 02.09.29: Removed: public void reshape(int x, int y, int width, int height) { this.setBounds(x, y, width, height); }

Overrides:
setBounds in class java.awt.Component
See Also:
Component.resize(int, int)

drawRect

protected void drawRect(java.awt.Graphics g,
                        int left,
                        int top,
                        int width,
                        int height,
                        int lineThickness,
                        java.awt.Color rectangleColor)
Draws a rectangle with the specified OUTSIDE left, top, width, and height. Used to draw the border.


debug

protected void debug(java.lang.String message)
Calls System.out.println if the debug variable is true; does nothing otherwise.

Parameters:
message - The String to be printed.

getImage

public java.awt.Image getImage()
The Image associated with the ImageLabel.


setImage

public void setImage(java.awt.Image img)
Sets Image associated with the ImageLabel.


getBorder

public int getBorder()
Gets the border width.


setBorder

public void setBorder(int border)
Sets the border thickness.


getBorderColor

public java.awt.Color getBorderColor()
Gets the border color.


setBorderColor

public void setBorderColor(java.awt.Color borderColor)
Sets the border color.


getWidth

public int getWidth()
Gets the width (image width plus twice border).

Overrides:
getWidth in class java.awt.Component

getHeight

public int getHeight()
Gets the height (image height plus 2x border).

Overrides:
getHeight in class java.awt.Component

hasExplicitSize

protected boolean hasExplicitSize()
Has the ImageLabel been given an explicit size? This is used to decide if the image should be stretched or not. This will be true if you call resize or reshape on the ImageLabel before adding it to a Container. It will be false otherwise.


getDefaultImageString

public static java.lang.String getDefaultImageString()
Returns the string representing the URL that will be used if none is supplied in the constructor.


setDefaultImageString

public static void setDefaultImageString(java.lang.String file)
Sets the string representing the URL that will be used if none is supplied in the constructor. Note that this is static, so is shared by all ImageLabels. Using this might be convenient in testing, but "real" applications should avoid it.


getImageString

protected java.lang.String getImageString()
Returns the string representing the URL of image.


isDebugging

public boolean isDebugging()
Is the debugging flag set?


setIsDebugging

public void setIsDebugging(boolean debug)
Set the debugging flag. Verbose messages will be printed to System.out if this is true.