Re: [OpenMap Users] Threading

From: Carsten Madsen <com_at_navicon.dk>
Date: Thu, 21 Jan 2010 21:27:10 +0100

|Hello John

Here is a simple OM based image generator and an example that uses it
render the image into a jpanel. Just add your layers to props and the
layer names to layers vector in the S52ImageGenerator.

BR
Carsten


*import *java.awt.Graphics;
*import *java.awt.Graphics2D;
*import *java.awt.RenderingHints;
*import *java.awt.event.MouseEvent;
*import *java.awt.event.MouseListener;
*import *java.awt.event.MouseWheelEvent;
*import *java.awt.event.MouseWheelListener;
*import *java.awt.event.WindowAdapter;
*import *java.awt.event.WindowEvent;
*import *java.awt.image.BufferedImage;
*import *java.util.Properties;
*import *java.util.Vector;

*import *javax.swing.JFrame;
*import *javax.swing.JPanel;
*import *javax.swing.SwingUtilities;

*import *com.bbn.openmap.LatLonPoint;
*import *com.bbn.openmap.Layer;
*import *com.bbn.openmap.LayerHandler;
*import *com.bbn.openmap.proj.LLXY;
*import *com.bbn.openmap.proj.Proj;
*import *com.bbn.openmap.util.Debug;

*public class *BufferedImageDemo {

*final static **int *WIDTH = 400;

*final static **int *HEIGHT = 400;

*static class *S52ImageGenerator {

*private *Layer[] layerArray;

S52ImageGenerator() {
Properties props = *new *Properties();

// # Specify shapefile and spatial-index file as a filename or
// pathname.
// # If the former, you must reference the directory where this file
// # lives in your CLASSPATH
// #shapePolitical.shapeFile=data/shape/dcwpo-browse.shp
// #shapePolitical.spatialIndex=data/shape/dcwpo-browse.ssx
// props.put("shapePolitical.class",
// "com.bbn.openmap.layer.shape.ShapeLayer");
// props.put("shapePolitical.prettyName", "Political Boundaries");
// props.put("shapePolitical.shapeFile",
// "/opt/openmap-4.6.3/share/data/shape/cntry02/cntry02.shp");
// props.put("shapePolitical.spatialIndex",
// "/opt/openmap-4.6.3/share/data/shape/cntry02/cntry02.ssx");
// // # Colors (32bit ARGB)
// props.put("shapePolitical.lineColor", "ff000000");
// props.put("shapePolitical.fillColor", "ffbdde83");

// debug openmap layer creation not necessary in production
Debug.put("layerhandler");
Debug.put("componentfactory");
Debug.put("componentfactorydetail");

Vector<String> layers = *new *Vector<String>();
layers.add("||shapePolitical||");
layerArray = LayerHandler.getLayers(layers, layers, props);
}

BufferedImage getS52Image(Proj proj) {
BufferedImage s52image = *new *BufferedImage(proj.getWidth(), proj
.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g = s52image.createGraphics();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
*for *(*int *i = 0; i < layerArray.length; i++) {
layerArray[i].renderDataForProjection(proj, g);
}
*return *s52image;
}
}

*static class *S52Panel *extends *JPanel {

*static final **float *SCALE_FACTOR = 0.1f;

*private *S52ImageGenerator s52ImageGenerator = *new *S52ImageGenerator();

// set projection to use for image generation
*private *LLXY epsg4326Proj = *new *LLXY(*new *LatLonPoint(55f, 10f), 0,
0, 0); // Mecator(....)

S52Panel() {
epsg4326Proj.setScale(299000);

// make scrollwheel zoom
addMouseWheelListener(*new *MouseWheelListener() {
*public **void *mouseWheelMoved(MouseWheelEvent e) {
*float *currentScale = epsg4326Proj.getScale();
*if *(e.getWheelRotation() > 0) {
epsg4326Proj.setScale(currentScale
* (1f + (e.getScrollAmount() * SCALE_FACTOR)));
} *else *{
epsg4326Proj.setScale(currentScale
* (1f - (e.getScrollAmount() * SCALE_FACTOR)));
}
repaint();
}
});

// make mouse click pan
addMouseListener(*new *MouseListener() {

*public **void *mouseClicked(MouseEvent e) {
LatLonPoint newCenter = epsg4326Proj.inverse(e.getPoint());
epsg4326Proj.setCenter(newCenter);
repaint();
}

*public **void *mouseEntered(MouseEvent e) {
}

*public **void *mouseExited(MouseEvent e) {
}

*public **void *mousePressed(MouseEvent e) {
}

*public **void *mouseReleased(MouseEvent e) {
}

});
}

_at_Override
*public **void *paint(Graphics g) {
g.setColor(getBackground());
g.fillRect(0, 0, getWidth(), getHeight());
epsg4326Proj.setWidth(getWidth());
epsg4326Proj.setHeight(getHeight());
BufferedImage s52Image = s52ImageGenerator
.getS52Image(epsg4326Proj);
g.drawImage(s52Image, 0, 0, *null*);
}

}

*public static **void *main(String[] args) {
SwingUtilities.invokeLater(*new *Runnable() {
*public **void *run() {
JFrame f = *new *JFrame();
f.addWindowListener(*new *WindowAdapter() {
*public **void *windowClosing(WindowEvent e) {
System.exit(0);
}
});
f.setContentPane(*new *S52Panel());
f.setSize(WIDTH, HEIGHT);
f.setVisible(*true*);
}
});

}
}|
On 01/20/2010 07:21 PM, John McMahon wrote:
>
> All,
>
> I'm trying to use OpenMap to generate reports that include images from
> maps drawn behind the scenes but not shown to the user. The problem
> seems to be a threading issue:
>
> Order things need to happen
>
> 1. Layers need to load completely
>
> 2. Image needs to be taken from map layers
>
> 3. Report needs to be generated using map image
>
> Order things are actually happening because of threading
>
> 1. Image taken of map layers
>
> 2. Layers loading completely
>
> 3. Report generated
>
> This results in a report that has a blank image ( or the image may
> only include some layers).
>
> Is there some way of getting a flag that tells us when the layers are
> done loading/drawing?
>
> Thanks,
>
> John
>



--
[To unsubscribe to this list send an email to "majdart_at_bbn.com"
with the following text in the BODY of the message "unsubscribe openmap-users"]
Received on Thu Jan 21 2010 - 15:46:14 EST

This archive was generated by hypermail 2.3.0 : Tue Mar 28 2017 - 23:25:09 EDT