xmlgraphics-batik-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Julien Beghin <minimoi1...@hotmail.com>
Subject RE: Partial Refresh
Date Mon, 19 Jul 2010 13:14:23 GMT

Hi All,


As I can't give you the whole application, I created a sample of what I want to do.


I simply create a pattern and use it in the fill attribute of a <rect>...


The application consists in changing the pattern color when pressing space.


The SVG is also displayed in the console for information...


Do someone on the mailing list have this application working correctly ?


Nota : I have a color modification when zooming with the CTRL key and the mouse after having
pressed space.






Here is the source code, same as the attached file




package com.test;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.StringReader;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.apache.batik.dom.svg.SAXSVGDocumentFactory;
import org.apache.batik.swing.JSVGCanvas;
import org.apache.batik.swing.gvt.GVTTreeRendererEvent;
import org.apache.batik.swing.gvt.GVTTreeRendererListener;
import org.apache.batik.util.RunnableQueue;
import org.apache.batik.util.SVGConstants;
import org.apache.batik.util.XMLResourceDescriptor;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.svg.SVGDocument;
import org.w3c.dom.svg.SVGSVGElement;




public class TestPattern extends JFrame {
 private static final long serialVersionUID = 5778700446793083700L;
 Canvas canvas;
 SVGDocument document;
 RunnableQueue runQ = null;
 String svg = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
 +"<svg viewbox=\"0 0 50 50\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"
contentScriptType=\"text/ecmascript\" width=\"50\" zoomAndPan=\"magnify\" contentStyleType=\"text/css\"
height=\"50\" preserveAspectRatio=\"xMidYMid meet\" version=\"1.1\">"
 +"<pattern width=\"25\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" patternUnits=\"userSpaceOnUse\"
xlink:type=\"simple\" xlink:actuate=\"none\" height=\"25\"  id=\"myPattern\" preserveAspectRatio=\"xMidYMid
meet\" xlink:show=\"other\" overflow=\"hidden\">"
 +" <path fill=\"red\" d=\"M 1 1 L 24 1 L 24 24 L 1 24z\" stroke-width=\"1\" stroke=\"green\"/>"
 +"<rect overflow=\"visible\" fill=\"url(#myPattern)\" width=\"50.0000\" height=\"50\"/>"
 private boolean invert = false;
 public TestPattern() {
  canvas = new Canvas();

  WindowAdapter windowAdapter = new WindowAdapter() {
   public void windowOpened(WindowEvent we) {
    SVGDocument doc = null;
    try {
     String parser = XMLResourceDescriptor
     SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
     StringReader reader = new StringReader(svg);
     doc = f.createSVGDocument(null, reader);
    } catch (Exception ex) {
    try {
     Source source = new DOMSource(doc);
     Result result = new StreamResult(System.out);
     Transformer xformer = TransformerFactory.newInstance()
     xformer.setOutputProperty(OutputKeys.INDENT, "yes");
     xformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
     xformer.transform(source, result);
    } catch (Exception ex) {

  setSize(100, 100);

 public class Canvas extends JSVGCanvas implements GVTTreeRendererListener {

  private static final long serialVersionUID = 1L;

  public Canvas() {
   canvas = this;

   KeyAdapter keyAdapter = new KeyAdapter() {
    public void keyPressed(KeyEvent e) {
     if(e.getKeyCode() != KeyEvent.VK_SPACE )
     if (runQ == null)
     runQ.invokeLater(new Runnable() {
      public void run() {
       try {
        Source source = new DOMSource(document);
        Result result = new StreamResult(System.out);
        Transformer xformer = TransformerFactory
          OutputKeys.OMIT_XML_DECLARATION, "no");
        xformer.transform(source, result);
       } catch (Exception ex) {

  public void gvtRenderingPrepare(GVTTreeRendererEvent e) {

  public void gvtRenderingStarted(GVTTreeRendererEvent e) {

  public void gvtRenderingCompleted(GVTTreeRendererEvent e) {
   document = canvas.getSVGDocument();
   runQ = canvas.getUpdateManager().getUpdateRunnableQueue();

  public void gvtRenderingCancelled(GVTTreeRendererEvent e) {

  public void gvtRenderingFailed(GVTTreeRendererEvent e) {

 private void changeColor() {
  SVGSVGElement root = document.getRootElement();

  NodeList liste = document.getElementsByTagName(SVGConstants.SVG_PATH_TAG);
  Element path = (Element)liste.item(0);
  if(invert == false){
   path.setAttributeNS(null, SVGConstants.SVG_FILL_ATTRIBUTE, "blue");
   path.setAttributeNS(null, SVGConstants.SVG_STROKE_ATTRIBUTE, "yellow");
   path.setAttributeNS(null, SVGConstants.SVG_FILL_ATTRIBUTE, "red");
   path.setAttributeNS(null, SVGConstants.SVG_STROKE_ATTRIBUTE, "green");   
  invert = !invert;


 public static void main(String[] args) {
  SwingUtilities.invokeLater(new Runnable() {
   public void run() {
    new TestPattern();


Allumez et ├ęteignez votre PC en un instant avec Windows 7 !
View raw message