JavaFX with Geotools Tutorial – Part 1: Load shape file

Please visit instead


Build with Maven

First you set up a basic build script.

Create the project structure
  1. Create directory structure following maven standard
  2. Download global
    country boundaries shape files
    and put to resource directory
  3. Create 2 classes for and
└── src
    └── main
        └── java
            └── map_example
        └── resource
            └── map_example
                └── countries.dbf
                └── countries.prj
                └── countries.shp
                └── countries.shx
└── pom.xml
Configure pom.xml
    1. Define the version number of libraries that you
      wish to use.
    1. Specify the following dependencies.
    1. Define repository to download jar files.
			<name> repository</name>
			<name>Open Source Geospatial Foundation Repository</name>
			<name>Boundless Maven Repository</name>


Now that you’ve set up the project and build system, you can
create your map canvas.

Create map canvas

    1. Initial java fx canvas and GraphicsContext. The
      GraphicsContext will be used to draw map content on the canvas
	private Canvas canvas;
	private MapContent map;
	private GraphicsContext gc;

	public MapCanvas(int width, int height) {
		canvas = new Canvas(width, height);
		gc = canvas.getGraphicsContext2D();

	public Node getCanvas() {
		return canvas;
    1. Load shape files and setting up style for
      displaying on the screen.
	private void initMap() {
		try {
			FileDataStore store = FileDataStoreFinder.getDataStore(this.getClass().getResource("countries.shp"));
			SimpleFeatureSource featureSource = store.getFeatureSource();
			map = new MapContent();
			Style style = SLD.createSimpleStyle(featureSource.getSchema());
			FeatureLayer layer = new FeatureLayer(featureSource, style);
			map.getViewport().setScreenArea(new Rectangle((int) canvas.getWidth(), (int) canvas.getHeight()));
		} catch (IOException e) {
    1. Use StreamingRender to draw MapContent on the canvas

We create new instance of FXGraphic2D for rendering Graphic2D on GraphicsContext.

	private void drawMap(GraphicsContext gc) {
		StreamingRenderer draw = new StreamingRenderer();
		FXGraphics2D graphics = new FXGraphics2D(gc);
		Rectangle rectangle = new Rectangle((int) canvas.getWidth(), (int) canvas.getHeight());
		draw.paint(graphics, rectangle, map.getViewport().getBounds());

Initial main application and screen size for display map

	public class Main extends Application {
		public void start(Stage primaryStage) {
			try {
				MapCanvas canvas = new MapCanvas(1024, 768);
				Pane pane = new	Pane(canvas.getCanvas());
				Scene scene = new Scene(pane);
				primaryStage.setTitle("Map Example");;
			} catch (Exception e) {

		public static void main(String[] args) {

Source code

What’s next?

In Tutorial Part 2, we will add more functionality like pan, zoom
in and zoom out.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s