Hi Julien,
Julien Beghin <[email protected]> wrote on 07/19/2010 09:14:23 AM:
> I simply create a pattern and use it in the fill attribute of a
<rect>...
This is the problem. Batik doesn't do a good job of tracking
complex dependencies like patterns. The only solution is to twiddle
all of the pattern references (set them to nothing then back to the
pattern).
> 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\"/>"
> +"</pattern>"
> +"<rect overflow=\"visible\" fill=\"url(#myPattern)\" width=\"50.
> 0000\" height=\"50\"/>"
> +"</svg>";
>
> private boolean invert = false;
>
>
>
> public TestPattern() {
> super("");
> canvas = new Canvas();
> setContentPane(canvas);
> WindowAdapter windowAdapter = new WindowAdapter() {
> public void windowOpened(WindowEvent we) {
> SVGDocument doc = null;
> try {
> String parser = XMLResourceDescriptor
> .getXMLParserClassName();
> SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
> StringReader reader = new StringReader(svg);
> doc = f.createSVGDocument(null, reader);
> } catch (Exception ex) {
> }
> canvas.setSVGDocument(doc);
> try {
> Source source = new DOMSource(doc);
> Result result = new StreamResult(System.out);
> Transformer xformer = TransformerFactory.newInstance()
> .newTransformer();
> xformer.setOutputProperty(OutputKeys.INDENT, "yes");
> xformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
> xformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,
> "no");
> xformer.transform(source, result);
> } catch (Exception ex) {
> ex.printStackTrace();
> }
> }
> };
> addWindowListener(windowAdapter);
> setDefaultCloseOperation(EXIT_ON_CLOSE);
> setSize(100, 100);
> setVisible(true);
> }
> public class Canvas extends JSVGCanvas implements
GVTTreeRendererListener {
> private static final long serialVersionUID = 1L;
> public Canvas() {
> canvas = this;
> setDocumentState(JSVGCanvas.ALWAYS_DYNAMIC);
> addGVTTreeRendererListener(this);
> KeyAdapter keyAdapter = new KeyAdapter() {
> public void keyPressed(KeyEvent e) {
>
> if(e.getKeyCode() != KeyEvent.VK_SPACE )
> return;
>
>
> if (runQ == null)
> return;
> runQ.invokeLater(new Runnable() {
> public void run() {
> changeColor();
> try {
> Source source = new DOMSource(document);
> Result result = new StreamResult(System.out);
> Transformer xformer = TransformerFactory
> .newInstance().newTransformer();
> xformer.setOutputProperty(OutputKeys.INDENT,
> "yes");
> xformer.setOutputProperty(OutputKeys.ENCODING,
> "UTF-8");
> xformer.setOutputProperty(
> OutputKeys.OMIT_XML_DECLARATION, "no");
> xformer.transform(source, result);
> } catch (Exception ex) {
> ex.printStackTrace();
> }
> }
> });
> }
> };
> addKeyListener(keyAdapter);
> }
> 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");
> }else{
> 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();
> }
> });
> }
> }
>
>
> De nouvelles Emoticones sur Messenger ? Téléchargez gratuitement les
> émoticônes Summer ![attachment "TestPattern.java" deleted by Thomas
> E. DeWeese/449433/EKC]
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]