KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > svggen > ShowGraphics2DOutput


1 /*
2
3    Copyright 2002-2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.svggen;
19
20 import java.awt.Color JavaDoc;
21 import java.awt.Dimension JavaDoc;
22 import java.awt.Shape JavaDoc;
23 import java.awt.geom.Ellipse2D JavaDoc;
24
25 import org.w3c.dom.DOMImplementation JavaDoc;
26 import org.w3c.dom.Element JavaDoc;
27 import org.w3c.dom.svg.SVGDocument;
28
29 import org.apache.batik.bridge.BaseScriptingEnvironment;
30 import org.apache.batik.bridge.BridgeContext;
31 import org.apache.batik.bridge.GVTBuilder;
32 import org.apache.batik.bridge.UserAgentAdapter;
33 import org.apache.batik.dom.svg.SVGDOMImplementation;
34 import org.apache.batik.test.AbstractTest;
35 import org.apache.batik.test.TestReport;
36
37 /**
38  * Checks that the content generated from the SVGGraphics2D and to which
39  * an event handler has been added can be processed by Batik.
40  *
41  * @author <a mailto="vincent.hardy@sun.com">Vincent Hardy</a>
42  * @version $Id: ShowGraphics2DOutput.java,v 1.4 2004/08/18 07:16:45 vhardy Exp $
43  */

44 public class ShowGraphics2DOutput extends AbstractTest {
45     public TestReport runImpl() throws Exception JavaDoc {
46
47         DOMImplementation JavaDoc impl = SVGDOMImplementation.getDOMImplementation();
48         String JavaDoc svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
49         SVGDocument doc = (SVGDocument)impl.createDocument(svgNS, "svg", null);
50         
51         SVGGraphics2D g = new SVGGraphics2D(doc);
52
53         Shape JavaDoc circle = new Ellipse2D.Double JavaDoc(0,0,50,50);
54         g.setPaint(Color.red);
55         g.fill(circle);
56         g.translate(60,0);
57         g.setPaint(Color.green);
58         g.fill(circle);
59         g.translate(60,0);
60         g.setPaint(Color.blue);
61         g.fill(circle);
62         g.setSVGCanvasSize(new Dimension JavaDoc(180,50));
63
64         Element JavaDoc root = doc.getDocumentElement();
65
66         // The following populates the document root with the
67
// generated SVG content.
68
g.getRoot(root);
69
70         root.setAttribute("onload", "System.out.println('hello')");
71
72         // Now that the SVG file has been loaded, build
73
// a GVT Tree from it
74
TestUserAgent userAgent = new TestUserAgent();
75         GVTBuilder builder = new GVTBuilder();
76         BridgeContext ctx = new BridgeContext(userAgent);
77         ctx.setDynamic(true);
78
79         builder.build(ctx, doc);
80         BaseScriptingEnvironment scriptEnvironment
81             = new BaseScriptingEnvironment(ctx);
82         scriptEnvironment.loadScripts();
83         scriptEnvironment.dispatchSVGLoadEvent();
84
85         if (!userAgent.failed) {
86             return reportSuccess();
87         } else {
88             return reportError("Got exception while processing document");
89         }
90     }
91
92     class TestUserAgent extends UserAgentAdapter {
93         boolean failed;
94
95         public void displayError(Exception JavaDoc e) {
96             failed = true;
97         }
98     }
99 }
100
Popular Tags