What is asynchronous painting ?

In an ILcdGXYView,

  • Painting of the data happens on the Event Dispatch Thread (EDT).

  • User interaction, such as using the mouse to navigate around, happens on the EDT as well.

The result is that the user cannot interact with the view while the view is being painted,. This is not a problem for fast-painting layers, but some layers paint themselves relatively slowly. For example, a WMS layer needs to request its data over the network first. The longer painting time can make the view feel really unresponsive to a user trying to interact with it.

The technique of asynchronous painting solves this problem by:

  1. First painting the layer to an image on a background thread.

    In the example of the WMS layer, the network request for new data is made in this step. The network request now happens on the background thread and no longer on the EDT, so that the view can still handle user interaction events.

  2. Once the image is finished, paint it on the view.

    This is a fast operation because no data must be loaded or discretized.

How to use it ?

Creating and adding an asynchronous layer to a view

To create a layer for painting on a background thread:

  1. Create a regular layer, using the TLcdGXYLayer.create method for example.

  2. Decorate it with an ILcdGXYAsynchronousLayerWrapper, by calling the ILcdGXYAsynchronousLayerWrapper.create method for example.

  3. Add the resulting asynchronous layer directly to the view.

The following example shows how to create and add an asynchronously painted SHP layer to a view:

//First create the model
ILcdModelDecoder decoder =
    new TLcdCompositeModelDecoder(TLcdServiceLoader.getInstance(ILcdModelDecoder.class));
String countriesFile = "Data/Shp/NaturalEarth/50m_cultural/ne_50m_admin_0_countries_lakes.shp";
ILcdModel model = decoder.decode(countriesFile);

//Create a layer for the model with default styling
ILcdGXYLayer layer = TLcdGXYLayer.create(model);
//Wrap the layer with an async layer wrapper to ensure
//that the view remains responsive while data is being painted
layer = ILcdGXYAsynchronousLayerWrapper.create(layer);

//Add the async layer to the GXY view (an ILcdGXYView)
view.addGXYLayer(layer);

Modifying properties of the asynchronous layer

The ILcdGXYAsynchronousLayerWrapper must be treated exactly like a non-asynchronous ILcdGXYLayer. Once the layer has been added to an ILcdGXYView, you access it on the EDT.

You can use the available setters on the ILcdGXYAsynchronousLayerWrapper to change the visibility or the label of the layer, for example.

If you need to change a property that is not exposed by the ILcdGXYAsynchronousLayerWrapper, you must access the original inner layer. You can use any of the ILcdGXYAsynchronousLayerWrapper.invoke*OnGXYLayer methods for that.

See the Asynchronous painting in a GXY view reference guide for more information.