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:
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.
Once the image is finished, paint it on the view.
This is a fast operation because no data must be loaded or discretized.
To create a layer for painting on a background thread:
Create a regular layer, using the
TLcdGXYLayer.createmethod for example.
Decorate it with an
ILcdGXYAsynchronousLayerWrapper, by calling the
ILcdGXYAsynchronousLayerWrapper.createmethod for example.
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)); ILcdModel model = decoder.decode("Data/Shp/World/world.shp"); //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);
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
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.