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:
- It doesn't require any changes in the code for the Container. Thus,
Icons can be placed in arbitrary Containers (including builtin ones
such as Panels, Applets, and Frames).
However, given the current state of the AWT, it has two main disadvantages:
- If you move the mouse too quickly, especially for small Icons, you
can lose the cursor. Given that there is no cursorLocation or
warpCursor methods, there is no way to get it back.
- If you drag over the top of another Icon, the one on top gets
the mouse events. Unfortunately, controlling who is on top
is not portable.
On the other hand, you can have the Container insert a small amount of code
into its handleEvent method. This has two advantages:
- You cannot lose track of the mouse as long as the cursor stays inside
the Container. So you can drag very small Icons while moving the mouse
quickly.
- You can drag over the top of other Icons reliably.
This has two disadvantages:
- Since it requires changes in the Container's code, it cannot be
used for builtin classes or classes for which source is not
available. The user would have to write custom Container
subclasses in such a case.
- Maintenance is more difficult since code is required in multiple
classes.
- You still cannot drag over the top of arbitrary Components. If the
Component is looking for a mouse drag event and is on top, it will get
the event and may never pass it on to the Container. So care must be
taken when using Icons with other custom Components that make use
of mouseDrag.
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
-
dragCursor
- The cursor to get when dragging.
-
draggable
- Can the image be dragged with the mouse?
Default = true.
-
highlightable
- Should the image be highlighted when you click/drag? Default = true.
-
highlightColor
- The Color to use for the highlight rectangle.
-
highlightThickness
- How thick should the highlighting rectangle be?
Default = 3.
-
Icon()
- Create an Icon with the default image.
-
Icon(Image)
- Create an Icon using the image specified.
-
Icon(String)
- Create an Icon using the image at URL specified by the string.
-
Icon(URL)
- Create an Icon using the image at URL specified.
-
componentUnder(int, int, Container)
- Finds the Component (if any) that is under the specified (x,y)
location.
-
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.
-
handleIconEvent(Event, Container)
- Called from the handleEvent method of the Container if the Container
is to handle the mouse events.
-
mouseDown(Event, int, int)
- Changes the cursor from the current to dragCursor, and optionally
draws a highlighting rectangle around image.
-
mouseDrag(Event, int, int)
- If it is draggable, moves the Component to be recentered at
the specified (x,y).
-
mouseUp(Event, int, int)
- Changes the cursor from dragCursor to whatever it was at mouseDown.
-
paint(Graphics)
- Calls the parent class' paint to draw the image, then
draws a highlighting rectangle if highlightable is set.
highlightable
public boolean highlightable
- Should the image be highlighted when you click/drag? Default = true.
highlightThickness
public int highlightThickness
- How thick should the highlighting rectangle be?
Default = 3. Ignored if highlightable is false.
highlightColor
public Color highlightColor
- The Color to use for the highlight rectangle.
Default is Color.blue. Ignored if highlightable is false.
draggable
public boolean draggable
- Can the image be dragged with the mouse?
Default = true. (If it will always be false, use
an ImageLabel instead).
dragCursor
public int dragCursor
- The cursor to get when dragging. Default is Frame.MOVE_CURSOR
Icon
public Icon()
- Create an Icon with the default image.
- See Also:
- defaultImageString
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.
Icon
public Icon(URL imageURL)
- Create an Icon using the image at URL specified.
- Parameters:
- imageURL - The URL of the image.
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.
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
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.
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
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
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
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
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