Purpose: Display a semi-transparent picture, like the rose on the previous page. When the mouse cursor enters the picture it becomes completely opaque and is on top of all other windows. When the mouse cursor leaves the picture, it becomes semi-transparent again.
Discussion: When the program starts, showImage() takes the path of the picture file, 50% of opacity and the boolean false, because we do not want the picture to be on top of all other windows.. (10, 20) is the screen position in pixels of the upper left corner. In order to get callbacks from mouse events, addNativeMouseListener() registers a NativeMouseListener and defines with an OR-combination of constants, which mouse events are really needed. This boosts the performance of the overall system, because mouse events, especially mouse moves, may be rather time consuming.
In the implementation of NativeMouseListener's method mouseEvent() , we use getEvent() to find out, which event actually occurred. When the mouse cursor enters the picture, we show it fully opaque and on top of all other windows. When the mouse cursor leaves the picture, we show it semi-transparent, so that the underlying desktop shines through. Double-clicking on the picture terminates the program.
No code is necessary to drag the picture with the left mouse button.
Special care must be taken for the location of the image file. Either it is in the same directory as the Java application class file GadgetEx1.class or you must use a fully qualified path, including drive and directory, likeString img = "c:\\temp\\rose.gif"
JawGadget uses a native DLL called jawgadget.dll . This file must either reside in the directory of the Java application class file or in the path of the Windows operating system, e.g. in c:\windows\system32 .
To create an pleasant image for JawGadget some special care must be taken. Here some recommandations:
Purpose: Display a fully opaque picture, like a Jman with a red nose. When the mouse cursor moves over the nose, it's shown in green color and an speech balloon is displayed.
Discussion: We use two pictures jman.bmp (red nose) and jman1.bmp (green nose with speech balloon). The nose is considered as hot spot area. When the mouse cursor enters the hot spot, we show the second picture, when the cursor leaves the hot we show the first picture again. We use a boolean state variable isGreenNose, because we only want to load the new picture when the mouse enters or leaves the hot spot and not repeatedly when the cursor is inside or outside.
Purpose: Add a turning wheel as icon in the taskbar tray (animated tray icon). A left mouse button click reverses the rotation direction, a right mouse button click terminates the program. At startup a balloon tool tip is shown. Each time the mouse cursor enters the wheel, a tool tip reports the current direction.// GadgetEx3.java
// Dynamic tray icon
public class GadgetEx3 implements TrayIconListener
private JawGadget jg = new JawGadget();
boolean volatile isRunning = true;
private int start = 0;
int iconEventMask = NativeMouse.lRelease | NativeMouse.rRelease;
boolean first = true;
for (int i = 0; i < 4; i++)
String ico = "wheel" + Math.abs(start - i) + ".ico";
String msg = (start == 0) ?
"Now turning clockwise" : "Now turning anti-clockwise";
"Left click to change direction\n" +
"Right click to quit");
first = false;
public void iconEvent(TrayIcon icon)
if (start == 0)
start = 3;
start = 0;
isRunning = false;
public static void main(String args)
Discussion: We use 4 pictures wheel0.ico, .. wheel3.ico for the different positions of the wheel. In order to generate the sequence 0,1,2,3,0,... or 3,2,1,0,3,... it's a trick to take abs(start - i), using an integer direction flag start. While i runs from 0 to 3, the first sequence is generated with start = 0, the second with start = 3. It's better to register the mouse release event, especially for terminating the program, because another tray icon may take over the place of the wheel and get the release event.
To conform to the Java guidelines we declare isRunning volatile, because it is modified by the callback method, which runs in another thread.
Because of the importance of tray icons, quite a few other implementations of tray icons are known, among them
Tray icons are also supposed to be included in the next release of the Java Platform (Edition 6). A beta-release is already available at Sun's website.
Purpose: Create a sample datebook with daily appointments in semi-transparent window that can be moved on the desktop. On entry of the mouse cursor, the windows becomes fully opaque. Clicking the left mouse button advances the appointment view for one hour, clicking right terminates the program (improvement: iconize it as tray icon).
Each time we want another look in the datebook by clicking on the picture, we must create a new BMP and redisplay it as GadGadget. Obviously the BMP file serves as data link between Java and the native code.