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
<?xml version="1.0" encoding="iso-8859-1"?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"

"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd";>

<html xmlns="http://www.w3.org/1999/xhtml";>

<head>

  <meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1" />

  <title>VisAD - changing the cursor</title>

  <meta name="generator" content="amaya 5.3, see
http://www.w3.org/Amaya/"; />

  <link rel="stylesheet" href="styles.css" type="text/css" />

</head>



<body>

<h1>Changing the cursor for a VisAD window</h1>

<br />





<h2>The problem</h2>



<p>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.</p>



<p>You know that you can change the cursor for a Java Component, but how
does

this help when it comes to VisAD?
</p>

<p>
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.
</p>

<br />





<h2>Background - changing the cursor for a Java Component</h2>



<p>Changing the cursor for a Java Component object is quite easy.  You
would

use the following method from the java.awt.Component class: <br />

<br />

<code>public void setCursor( java.awt.Cursor cursor )</code></p>



<p>So, the question is, how do I get a Component object for my VisAD

window?</p>

<br />





<h2>Basic steps</h2>



<p>The basic steps are as follows:</p>

<ul>

  <li>get the display renderer used by the display</li>

  <li>get the canvas used by the display renderer</li>

  <li>set the cursor on the canvas</li>

</ul>

<br/>





<h2>Step 1 - Getting the display renderer</h2>


<p>Assuming a visad.DisplayImpl object has been defined as follows:
<br />
<code>DisplayImpl theDisplay;</code>
</p>


<p>For a 2-Dimensional display, <br />

<code>final DisplayRendererJ2D theDisplayRenderer =
(DisplayRendererJ2D)

theDisplay.getDisplayRenderer();</code></p>



<p>For a 3-Dimensional display, <br />

<code>final DisplayRendererJ3D theDisplayRenderer =
(DisplayRendererJ3D)

theDisplay.getDisplayRenderer();</code></p>

<br />





<p>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.</p>

<br />





<h2>Step 2 - Getting the canvas</h2>



<p>For a 2-Dimensional display: <br />

<code>final VisADCanvasJ2D theCanvas =

theDisplayRenderer.getCanvas();</code></p>



<p>For a 3-Dimensional display: <br />

<code>final VisADCanvasJ3D theCanvas =

theDisplayRenderer.getCanvas();</code></p>



<p>Taking into account that both VisADCanvasJ2D and VisADCanvasJ3D are
both

subclasses of java.awt.Component, we could simply write: <br />

<code>final Component theCanvas = (Component)

theDisplayRenderer.getCanvas();</code></p>

<br />





<h2>Step 3 - Setting the cursor on the canvas</h2>



<p>All that remains now is to set the cursor on the Component.  This can
be

done as follows: <br />

<code>theCanvas.setCursor( theCursor );</code> <br />

where <em>theCursor</em> is a Cursor object that you have created.</p>



<p>For example: <br />

<br/>

<code>final Cursor theCursor = new Cursor( Cursor.CROSSHAIR_CURSOR
);</code>

<br/>
or
<br/>

<code>final Cursor theCursor = new Cursor( Cursor.HAND_CURSOR
);</code></p>

<br />





<h2>Putting it all together</h2>



<p><code>public void setVisadCursor( DisplayImplJ2D theDisplay_IN ) <br
/>

{

<br />

final DisplayRendererJ2D theDisplayRenderer = <br />

(DisplayRendererJ2D) theDisplay_IN.getDisplayRenderer(); <br />

final Component theCanvas = (Component)
theDisplayRenderer.getCanvas(); <br />

final Cursor theCursor = new Cursor( Cursor.CROSSHAIR_CURSOR ); <br />

theCanvas.setCursor( theCursor ); <br />

}

</code></p>

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



<h2>Summary</h2>



<p>Now you know how to set the cursor for the canvas that is drawn upon
by the VisAD

window.</p>



<p>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.</p>

<br />

</body>

</html>

body {
  margin-left: 10%;
  margin-right: 10%;
  color: black;
  background: white;
  text-align: justify;
}
  • 2002 messages navigation, sorted by:
    1. Thread
    2. Subject
    3. Author
    4. Date
    5. ↑ Table Of Contents
  • Search the visad archives: