xmlgraphics-batik-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "David Eccles (programming)" <programm...@gringer.org>
Subject Problem overlaying SVG element on top of loaded file
Date Wed, 29 Sep 2010 15:48:14 GMT
I'm interested in dynamically overlaying SVG elements on a world map, and can't
work out why things aren't working for me. I confess that I don't really
understand the swing process, so have tried to change code I found elsewhere
(for the invokeLater method) in order to get this working. I know I'm using
sleeps instead of listeners in the main method -- those will change later on
once I get things displaying properly:

What should happen is that a huge white circle should appear centered on
Atlanta. This doesn't happen, but the map displays. The map is a 1000x420 SVG
image of the world (http://user.interface.org.nz/~gringer/pics/worldmap.svg).

What things am I doing wrong?

Code is shown below:

import java.awt.BorderLayout;
import java.io.File;
import java.util.Vector;
import javax.swing.JFrame;
import org.apache.batik.swing.JSVGCanvas;
import org.w3c.dom.Element;

public class Pathogenic {

    JSVGCanvas boardDrawing;

    public Pathogenic(JSVGCanvas drawing){
        boardDrawing = drawing;
    }

    public boolean drawCities(final Vector<City> cityList){
        final double mapMinX = -170;
        final double mapMinY = -90;
        final double mapSizeX = 1000;
        final double mapSizeY = 420;
        boardDrawing.getUpdateManager().getUpdateRunnableQueue().invokeLater
          (new Runnable() {
              public void run() {
                  for(City tCity : cityList){
                      double cx =  (tCity.getX() - mapMinX) * (mapSizeX / 360.0);
                      double cy =  (tCity.getY() - mapMinY) * (mapSizeY / 180.0);
                      Element cityCirc =
boardDrawing.getSVGDocument().createElement("circle");
                      cityCirc.setAttribute("cx", Double.toString(cx));
                      cityCirc.setAttribute("cy", Double.toString(cy));
                      cityCirc.setAttribute("r", "50");

cityCirc.setAttribute("style","fill:white;stroke:black;stroke-width:3");

boardDrawing.getSVGDocument().getRootElement().appendChild(cityCirc);
                      System.out.println("Drawing " + tCity.getName() + " at " +
cx + "," + cy);
                  }
              }
          });
        return true;
    }

    /**
     * @param args
     */
    public static void main(String[] args) {

        JFrame program = new JFrame("Pathogenic");
        program.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        program.setLayout(new BorderLayout());
        JSVGCanvas mapScreen = new JSVGCanvas();
        mapScreen.setDocumentState(JSVGCanvas.ALWAYS_DYNAMIC);
        program.add(mapScreen, BorderLayout.CENTER);
        File wmFile = new File("data/worldmap.svg");
        mapScreen.loadSVGDocument(wmFile.toURI().toString());
        // mapScreen.setURI(wmFile.toURI().toString());
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        program.pack();
        program.setVisible(true);
        try {
            Thread.sleep(4000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Board b = new Board();
        b.addCity("Atlanta", -84.39, 33.755);
        Pathogenic myApp = new Pathogenic(mapScreen);
        myApp.drawCities(b.getCities());
        mapScreen.repaint();
    }

}

---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org


Mime
View raw message