title: Refresh / Repaint SVG in Apache Batik
body: <p>I am using <strong>Apache Batik</strong> to load, manipulate and render an SVG graphic for simulation purposes.
  Loading and manipulating works so far, at least when writing the SVG out to a file. Also rendering and updating it ONCE (!) works.</p>
  
  <p>In order to manipulate the SVG, I am creating a org.w3c.dom.svg.SVGDocument and to display it I am using a org.apache.batik.swing.JSVGCanvas.</p>
  
  <p>In the manipulation part, what happens is that e.g. the "style" attribute of an element is retrieved and manipulated by e.g. changing the "fill:#HEX_CODE". </p>
  
  <p>I am able to render the SVG when loading the Java GUI created by IntelliJ using the GridLayoutManager (IntelliJ). Basically, there is a Form, a mainPanel within and a visualizationPanel within that. Panels are all of type JPanel. The visualizationPanel contains my svgCanvas of type JSVGCanvas.</p>
  
  <p>In order to trigger the manipulation and refresh the canvas I created a button. When I click the button, the svgDocument is changed and correctly refreshed in the canvas.</p>
  
  <p><strong>The question now is: how can I do this multiple times in a row?</strong> The idea is to change the color every second and repaint / refresh the canvas after manipulation. It does not seem to work with Thread.sleep(1000);</p>
  
  <p>Here is the runSimulation() method that is triggered when I click the button:</p>
  
  <pre><code>public void runSimulation() {
  
      Element bodyElement = svgDocument.getElementById("body_");
      String bodyStyleAttribute = bodyElement.getAttribute("style");
      String saturationHexColor = "";
      String newBodyStyleAttribute = "";
  
      saturationHexColor = simulationDataModel.getSaturationHexColor(70);
      newBodyStyleAttribute = bodyStyleAttribute.substring(0, bodyStyleAttribute.lastIndexOf("fill:#")) + "fill:#" + saturationHexColor + bodyStyleAttribute.substring(bodyStyleAttribute.lastIndexOf("fill:#") + "fill:#".length() + 6);
  
      svgDocument.getElementById("body_").setAttribute("style", newBodyStyleAttribute);
      simulatorView.getSvgCanvas().getUpdateManager().getUpdateRunnableQueue().invokeLater(runnable);
  
  
  }
  </code></pre>
  
  <p>The "runnable" variable is defined in the constructor of the View as follows:</p>
  
  <pre><code>runnable = new Runnable() {
              @Override
              public void run() {
                  simulatorView.getSvgCanvas().repaint();
              }
          };
  </code></pre>
  
  <p>I am quite unfamiliar with threading in Java and also with complex GUIs. Maybe you can help me out?</p>
  
  <p>Thanks !</p>