Class Icon

Class Icon

java.lang.Object
   |
   +----java.awt.Component
           |
           +----java.awt.Canvas
                   |
                   +----ImageLabel
                           |
                           +----Icon

Note: To see an on-line demo, see the page for IconTest.html. To download the source code, see Icon.java. For more examples, see the Core Web Programming source code archive.
public class Icon
extends ImageLabel
A class that supports Images as Components that can be repositioned with the mouse. As with all Components, the effects may be eventually undone by the layoutManager if it is not off (null) or chosen appropriately.

By default, with FlowLayout the Icon 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 the builtin Label class and of ImageLabel, upon which Icon is built. If you perform an explicit resize or reshape call before adding the ImageLabel to the Container, this size will override the defaults.

This can be used in two ways. In the first, the Component itself watches the mouse events, and allows the user to drag it around. This has one major advantage:

However, given the current state of the AWT, it has two main disadvantages: On the other hand, you can have the Container insert a small amount of code into its handleEvent method. This has two advantages: This has two disadvantages: Resolving these pros/cons depends upon the application, so the Icon class can be used either way. If the ignoreEvents variable is set to true (the default), then it cannot be dragged without adding code to the Container.

If the Container does not already have a custom handleEvent method, insert the following:

   public boolean handleEvent(Event event) {
     if (Icon.handleIconEvent(event, this))
       return(true);
     else
       return(super.handleEvent(event));
   }
 
If the Container does already have a custom handleEvent method, modify it by adding the Icon.handleEvent test at the top, as illustrated below:

   public boolean handleEvent(Event event) {
     if (Icon.handleIconEvent(event, this))
       return(true);
     Original handleEvent code here
   }
 
If instead you want the Icon to handle its own mouse events, simply set the Icon's ignoreEvents variable to false. Do not mix these two modes in a single program.

The original of the Icon source code is at http://www.apl.jhu.edu/~hall/java/ImageLabel/Icon.java, the documentation is at .../Icon.html, and a small example program can be found at .../IconTest.html.

No warranty of any kind is provided. Permission is granted to use and/or modify for any purpose.

7/96 Marty Hall:

Version:
1.0 (August 1996)
See Also:
ImageButton

Variable Index

 o dragCursor
The cursor to get when dragging.
 o draggable
Can the image be dragged with the mouse? Default = true.
 o highlightable
Should the image be highlighted when you click/drag? Default = true.
 o highlightColor
The Color to use for the highlight rectangle.
 o highlightThickness
How thick should the highlighting rectangle be? Default = 3.

Constructor Index

 o Icon()
Create an Icon with the default image.
 o Icon(Image)
Create an Icon using the image specified.
 o Icon(String)
Create an Icon using the image at URL specified by the string.
 o Icon(URL)
Create an Icon using the image at URL specified.

Method Index

 o componentUnder(int, int, Container)
Finds the Component (if any) that is under the specified (x,y) location.
 o handleEvent(Event)
If the Container was always going to handle the mouse events, it could simply look up the event type and call a helper method directly.
 o handleIconEvent(Event, Container)
Called from the handleEvent method of the Container if the Container is to handle the mouse events.
 o mouseDown(Event, int, int)
Changes the cursor from the current to dragCursor, and optionally draws a highlighting rectangle around image.
 o mouseDrag(Event, int, int)
If it is draggable, moves the Component to be recentered at the specified (x,y).
 o mouseUp(Event, int, int)
Changes the cursor from dragCursor to whatever it was at mouseDown.
 o paint(Graphics)
Calls the parent class' paint to draw the image, then draws a highlighting rectangle if highlightable is set.

Variables

 o highlightable
  public boolean highlightable
Should the image be highlighted when you click/drag? Default = true.
 o highlightThickness
  public int highlightThickness
How thick should the highlighting rectangle be? Default = 3. Ignored if highlightable is false.
 o highlightColor
  public Color highlightColor
The Color to use for the highlight rectangle. Default is Color.blue. Ignored if highlightable is false.
 o draggable
  public boolean draggable
Can the image be dragged with the mouse? Default = true. (If it will always be false, use an ImageLabel instead).
 o dragCursor
  public int dragCursor
The cursor to get when dragging. Default is Frame.MOVE_CURSOR

Constructors

 o Icon
  public Icon()
Create an Icon with the default image.
See Also:
defaultImageString
 o Icon
  public Icon(String imageURLString)
Create an Icon using the image at URL specified by the string.
Parameters:
imageURLString - A String specifying the URL of the image.
 o Icon
  public Icon(URL imageURL)
Create an Icon using the image at URL specified.
Parameters:
imageURL - The URL of the image.
 o Icon
  public Icon(Image image)
Create an Icon using the image specified. You would only want to call it directly if you already have an image (e.g. created via createImage).
Parameters:
image - The image.

Methods

 o handleEvent
  public boolean handleEvent(Event event)
If the Container was always going to handle the mouse events, it could simply look up the event type and call a helper method directly. This, however, allows event handling either locally (if ignoreEvents is false) or in the Container. You cannot override this arbitrarily in subclasses.
Overrides:
handleEvent in class Component
 o handleIconEvent
  public static boolean handleIconEvent(Event event,
                                        Container container)
Called from the handleEvent method of the Container if the Container is to handle the mouse events. See the introduction to this class.
 o mouseDown
  public boolean mouseDown(Event e,
                           int x,
                           int y)
Changes the cursor from the current to dragCursor, and optionally draws a highlighting rectangle around image.
Returns:
true if it is draggable, false otherwise.
Overrides:
mouseDown in class Component
See Also:
dragCursor, highlightable, draggable
 o mouseUp
  public boolean mouseUp(Event e,
                         int x,
                         int y)
Changes the cursor from dragCursor to whatever it was at mouseDown. Erases the highlighting rectangle if necessary.
Returns:
true if it is draggable, false otherwise.
Overrides:
mouseUp in class Component
See Also:
dragCursor, highlightable, draggable
 o mouseDrag
  public boolean mouseDrag(Event e,
                           int x,
                           int y)
If it is draggable, moves the Component to be recentered at the specified (x,y).
Overrides:
mouseDrag in class Component
See Also:
draggable
 o paint
  public void paint(Graphics g)
Calls the parent class' paint to draw the image, then draws a highlighting rectangle if highlightable is set.
Overrides:
paint in class ImageLabel
See Also:
highlightable
 o componentUnder
  public static Component componentUnder(int x,
                                         int y,
                                         Container container)
Finds the Component (if any) that is under the specified (x,y) location. In the Container's coordinate system. Put here because Netscape has a bug in which it doesn't process inside() and locate() tests correctly. They work properly with appletviewer and for applications.
See Also:
locate