A simple tutorial on changing the cursor in VisAD

Hi all,

after a year of developing an application with
Java and VisAD, we have learned to do a few things
that make the VisAD graphics window look like any
other graphics window you would see in a GUI.

One of these, is changing the mouse cursor
to a cross-hair when the mouse moves over
the graphics window.  Another example is
having a lat/lon readout displayed in a
text-field in the application's status bar
which updates as the cursor moves around
the graphics window.
 
I thought that others might benefit from
this, so I wrote a small tutorial on how
to change the cursor in a VisAD window.
I have attached this tutorial and hope that
you find it useful.

My overall plan is to have a list of tutorials
that describe how you can make VisAD fit in
better with your user interface.

I'll keep you posted, and hopefully, these
tutorials can be hosted on a web-site so
they are visible to all VisAD users.

Please feel free to post any comments to
the list or to myself.

Best regards,
Jim.
---
Jim Koutsovasilis
Bureau of Meteorology, Australia
jikm@xxxxxxxxxx
Title: VisAD - changing the cursor

Changing the cursor for a VisAD window


The problem

Assume you have an application which displays some Swing components, together with a VisAD window. You would like the cursor to change its appearance when you move the mouse into the VisAD window.

You know that you can change the cursor for a Java Component, but how does this help when it comes to VisAD?

Well, a VisAD window draws on a canvas (VisADCanvasJ2D for 2-Dimensional displays, or VisADCanvasJ3D for 3-Dimensional displays), which is subclassed from java.awt.Component. This means that you can set the cursor on the canvas.


Background - changing the cursor for a Java Component

Changing the cursor for a Java Component object is quite easy. You would use the following method from the java.awt.Component class:

public void setCursor( java.awt.Cursor cursor )

So, the question is, how do I get a Component object for my VisAD window?


Basic steps

The basic steps are as follows:


Step 1 - Getting the display renderer

Assuming a visad.DisplayImpl object has been defined as follows:
DisplayImpl theDisplay;

For a 2-Dimensional display,
final DisplayRendererJ2D theDisplayRenderer = (DisplayRendererJ2D) theDisplay.getDisplayRenderer();

For a 3-Dimensional display,
final DisplayRendererJ3D theDisplayRenderer = (DisplayRendererJ3D) theDisplay.getDisplayRenderer();


Note that there is no getCanvas() method in the DisplayRenderer class, so we have to make a distinction between the 2D and 3D versions of DisplayRenderer.


Step 2 - Getting the canvas

For a 2-Dimensional display:
final VisADCanvasJ2D theCanvas = theDisplayRenderer.getCanvas();

For a 3-Dimensional display:
final VisADCanvasJ3D theCanvas = theDisplayRenderer.getCanvas();

Taking into account that both VisADCanvasJ2D and VisADCanvasJ3D are both subclasses of java.awt.Component, we could simply write:
final Component theCanvas = (Component) theDisplayRenderer.getCanvas();


Step 3 - Setting the cursor on the canvas

All that remains now is to set the cursor on the Component. This can be done as follows:
theCanvas.setCursor( theCursor );
where theCursor is a Cursor object that you have created.

For example:

final Cursor theCursor = new Cursor( Cursor.CROSSHAIR_CURSOR );
or
final Cursor theCursor = new Cursor( Cursor.HAND_CURSOR );


Putting it all together

public void setVisadCursor( DisplayImplJ2D theDisplay_IN )
{
final DisplayRendererJ2D theDisplayRenderer =
(DisplayRendererJ2D) theDisplay_IN.getDisplayRenderer();
final Component theCanvas = (Component) theDisplayRenderer.getCanvas();
final Cursor theCursor = new Cursor( Cursor.CROSSHAIR_CURSOR );
theCanvas.setCursor( theCursor );
}

public void setVisadCursor( DisplayImplJ3D theDisplay_IN )
{
final DisplayRendererJ3D theDisplayRenderer =
(DisplayRendererJ3D) theDisplay_IN.getDisplayRenderer();
final Component theCanvas = (Component) theDisplayRenderer.getCanvas();
final Cursor theCursor = new Cursor( Cursor.CROSSHAIR_CURSOR );
theCanvas.setCursor( theCursor );
}

Summary

Now you know how to set the cursor for the canvas that is drawn upon by the VisAD window.

When the mouse moves into the VisAD window, the cursor will change to whatever you have set it to. When the mouse moves out of the VisAD window, the cursor will change back to the default cursor.


body {
  margin-left: 10%;
  margin-right: 10%;
  color: black;
  background: white;
  text-align: justify;
}