I could not find a java library to do what you were looking for, so I studied SVG and changed its style and converted it to an image. At first, this problem occurred to me: Apache Batik.
I decided to use the open svg cards open source available on wikimedia (similar to the answer of Gregor Ofis), because it doesnβt do it so you donβt care about licensing, it is also ready for simple CSS modifications by country. (For instructions, see SVG comments). There is one hitch, some of these images on wikimedia were created by a python script , which also puts CSS style into the elements. If you want to use one of these SVG files, you will have to process this.
To my happy surprise, there was an almost perfect example in a batik wiki that needed just a few tweaks. I was able to create a modified image (png) with one small syntax change (their outfile is named .jpg, but its use is png transcoder) and a small change to load svg from project resources instead of disk. Below is an example code with my minor changes:
public class MapMaker { private final static String MAP_FLAT = "BlankMap-World6-Equirectangular.svg"; private final static String MAP_ROUND = "WorldMap.svg"; public static void main(String... args) { try {
As I mentioned, the way this works is to add CSS to the SVG before converting it to the bitmap you need. The secret sauce is SVGStyleElement , here you can see that the United States has turned green. For any other country, you simply use your 2-letter digraph and choose the color you want to fill. Of course, since this is CSS, you can also do things like changing the colors of borders or using a background image instead of color, so you have more flexibility.
I had to play a little with dependencies, so for you to overcome this obstacle, I will include my maven dependencies:
<dependencies> <dependency> <groupId>org.apache.xmlgraphics</groupId> <artifactId>batik-parser</artifactId> <version>1.7</version> </dependency> <dependency> <groupId>org.apache.xmlgraphics</groupId> <artifactId>batik-css</artifactId> <version>1.7</version> </dependency> <dependency> <groupId>org.apache.xmlgraphics</groupId> <artifactId>batik-svg-dom</artifactId> <version>1.7</version> </dependency> <dependency> <groupId>org.apache.xmlgraphics</groupId> <artifactId>batik-transcoder</artifactId> <version>1.7</version> </dependency> <dependency> <groupId>org.apache.xmlgraphics</groupId> <artifactId>batik-rasterizer</artifactId> <version>1.7</version> </dependency> <dependency> <groupId>org.apache.xmlgraphics</groupId> <artifactId>batik-codec</artifactId> <version>1.7</version> </dependency> </dependencies>
If you use a different layout manager, let me know and I can reset the transitive dependency tree.
Steve siebert
source share