KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Copyright 2001,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.GridLayout JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.FileOutputStream JavaDoc;
25 import java.io.OutputStreamWriter JavaDoc;
26
27 import javax.swing.JFrame JavaDoc;
28 import javax.swing.JPanel JavaDoc;
29
30 import org.w3c.dom.DOMImplementation JavaDoc;
31 import org.w3c.dom.Document JavaDoc;
32
33 import org.apache.batik.dom.svg.SVGDOMImplementation;
34 import org.apache.batik.swing.JSVGCanvas;
35 import org.apache.batik.swing.svg.SVGDocumentLoaderAdapter;
36 import org.apache.batik.swing.svg.SVGDocumentLoaderEvent;
37 import org.apache.batik.util.SVGConstants;
38
39 /**
40  * Simple component which displays, side by side, the drawing
41  * created by a <tt>Painter</tt>, rendered in a
42  * <tt>JPainterComponent</tt> on the left, and in a
43  * <tt>JSVGCanvas</tt> on the right, where the SVG
44  * displayed is the one created by the <tt>SVGGraphics2D</tt>
45  *
46  * @author <a HREF="mailto:vincent.hardy@sun.com">Vincent Hardy</a>
47  * @version $Id: JPainterCompare.java,v 1.5 2004/08/18 07:16:45 vhardy Exp $
48  */

49 public class JPainterCompare extends JPanel JavaDoc implements SVGConstants{
50     /**
51      * Canvas size for all tests
52      */

53     public static final Dimension JavaDoc CANVAS_SIZE
54         = new Dimension JavaDoc(300, 400);
55
56     public static String JavaDoc MESSAGES_USAGE
57         = "JPainterCompare.messages.usage";
58
59     public static String JavaDoc MESSAGES_LOADING_CLASS
60         = "JPainterCompare.messages.loading.class";
61
62     public static String JavaDoc MESSAGES_LOADED_CLASS
63         = "JPainterCompare.messages.loaded.class";
64
65     public static String JavaDoc MESSAGES_INSTANCIATED_OBJECT
66         = "JPainterCompare.messages.instanciated.object";
67
68     public static String JavaDoc ERROR_COULD_NOT_LOAD_CLASS
69         = "JPainterCompare.error.could.not.load.class";
70
71     public static String JavaDoc ERROR_COULD_NOT_INSTANCIATE_OBJECT
72         = "JPainterCompare.error.could.not.instanciate.object";
73
74     public static String JavaDoc ERROR_CLASS_NOT_PAINTER
75         = "JPainterCompare.error.class.not.painter";
76
77     public static String JavaDoc ERROR_COULD_NOT_TRANSCODE_TO_SVG
78         = "JPainterCompare.error.could.not.transcode.to.svg";
79
80     public static String JavaDoc ERROR_COULD_NOT_CONVERT_FILE_PATH_TO_URL
81         = "JPainterCompare.error.could.not.convert.file.path.to.url";
82
83     public static String JavaDoc ERROR_COULD_NOT_RENDER_GENERATED_SVG
84         = "JPainterCompare.error.could.not.render.generated.svg";
85
86     public static String JavaDoc CONFIG_TMP_FILE_PREFIX
87         = "JPainterCompare.config.tmp.file.prefix";
88
89     /**
90      * Builds an <tt>SVGGraphics2D</tt> with a default
91      * configuration.
92      */

93     protected SVGGraphics2D buildSVGGraphics2D() {
94         DOMImplementation JavaDoc impl = SVGDOMImplementation.getDOMImplementation();
95         String JavaDoc namespaceURI = SVGDOMImplementation.SVG_NAMESPACE_URI;
96         Document JavaDoc domFactory = impl.createDocument(namespaceURI, SVG_SVG_TAG, null);
97
98         // Create a default context from our Document instance
99
SVGGeneratorContext ctx = SVGGeneratorContext.createDefault(domFactory);
100
101         GenericImageHandler ihandler = new CachedImageHandlerBase64Encoder();
102         ctx.setGenericImageHandler(ihandler);
103
104         return new SVGGraphics2D(ctx, false);
105     }
106
107     static class LoaderListener extends SVGDocumentLoaderAdapter{
108         public final String JavaDoc sem = "sem";
109         public boolean success = false;
110         public void documentLoadingFailed(SVGDocumentLoaderEvent e){
111             synchronized(sem){
112                 sem.notifyAll();
113             }
114         }
115         
116         public void documentLoadingCompleted(SVGDocumentLoaderEvent e){
117             success = true;
118             synchronized(sem){
119                 sem.notifyAll();
120             }
121         }
122     }
123
124     /**
125      * Constructor
126      */

127     public JPainterCompare(Painter painter){
128         // First, create the AWT reference.
129
JPainterComponent ref = new JPainterComponent(painter);
130
131         // Now, generate the SVG from this Painter
132
SVGGraphics2D g2d = buildSVGGraphics2D();
133
134         g2d.setSVGCanvasSize(CANVAS_SIZE);
135
136         //
137
// Generate SVG content
138
//
139
File JavaDoc tmpFile = null;
140         try{
141             tmpFile = File.createTempFile(CONFIG_TMP_FILE_PREFIX,
142                                           ".svg");
143             
144             OutputStreamWriter JavaDoc osw = new OutputStreamWriter JavaDoc(new FileOutputStream JavaDoc(tmpFile), "UTF-8");
145
146             painter.paint(g2d);
147             g2d.stream(osw);
148             osw.flush();
149         }catch(Exception JavaDoc e){
150             e.printStackTrace();
151             throw new IllegalArgumentException JavaDoc
152                 (Messages.formatMessage(ERROR_COULD_NOT_TRANSCODE_TO_SVG,
153                                         new Object JavaDoc[]{e.getClass().getName()}));
154         }
155         
156         //
157
// Now, transcode SVG to a BufferedImage
158
//
159
JSVGCanvas svgCanvas = new JSVGCanvas();
160         LoaderListener l = new LoaderListener();
161         svgCanvas.addSVGDocumentLoaderListener(l);
162
163         try{
164             svgCanvas.setURI(tmpFile.toURL().toString());
165             synchronized(l.sem){
166                 l.sem.wait();
167             }
168         }catch(Exception JavaDoc e){
169             e.printStackTrace();
170             new Error JavaDoc
171                 (Messages.formatMessage(ERROR_COULD_NOT_CONVERT_FILE_PATH_TO_URL,
172                                         new Object JavaDoc[]{e.getMessage()}));
173         }
174
175         if(l.success){
176             setLayout(new GridLayout JavaDoc(1,2));
177             add(ref);
178             add(svgCanvas);
179         }
180
181         else{
182             throw new Error JavaDoc
183                 (Messages.formatMessage(ERROR_COULD_NOT_RENDER_GENERATED_SVG,null));
184         }
185     }
186
187     public Dimension JavaDoc getPreferredSize(){
188         return new Dimension JavaDoc(CANVAS_SIZE.width*2, CANVAS_SIZE.height);
189     }
190
191     /*
192      * Debug application: shows the image creatd by a <tt>Painter</tt>
193      * on the left and the image created by a <tt>JSVGComponent</tt>
194      * from the SVG generated by <tt>SVGGraphics2D</tt> from the same
195      * <tt>Painter</tt> on the right.
196      *
197      */

198     public static void main(String JavaDoc args[]){
199         if(args.length <= 0){
200             System.out.println(Messages.formatMessage
201                                (MESSAGES_USAGE, null));
202             System.exit(0);
203         }
204
205         // Load class.
206
String JavaDoc className = args[0];
207         System.out.println
208             (Messages.formatMessage(MESSAGES_LOADING_CLASS,
209                                     new Object JavaDoc[]{className}));
210
211         Class JavaDoc cl = null;
212
213         try{
214             cl = Class.forName(className);
215             System.out.println
216                 (Messages.formatMessage(MESSAGES_LOADED_CLASS,
217                                         new Object JavaDoc[]{className}));
218         }catch(Exception JavaDoc e){
219             System.out.println
220                 (Messages.formatMessage(ERROR_COULD_NOT_LOAD_CLASS,
221                                         new Object JavaDoc[] {className,
222                                                       e.getClass().getName() }));
223             System.exit(0);
224         }
225
226         // Instanciate object
227
Object JavaDoc o = null;
228
229         try{
230             o = cl.newInstance();
231             System.out.println
232                 (Messages.formatMessage(MESSAGES_INSTANCIATED_OBJECT,
233                                         null));
234         }catch(Exception JavaDoc e){
235             System.out.println
236                 (Messages.formatMessage(ERROR_COULD_NOT_INSTANCIATE_OBJECT,
237                                         new Object JavaDoc[] {className,
238                                                       e.getClass().getName()}));
239             System.exit(0);
240         }
241
242         // Cast to Painter
243
Painter p = null;
244
245         try{
246             p = (Painter)o;
247         }catch(ClassCastException JavaDoc e){
248             System.out.println
249                 (Messages.formatMessage(ERROR_CLASS_NOT_PAINTER,
250                                         new Object JavaDoc[]{className}));
251             System.exit(0);
252         }
253
254         // Build frame
255
JFrame JavaDoc f = new JFrame JavaDoc();
256         JPainterCompare c = new JPainterCompare(p);
257         c.setBackground(Color.white);
258         c.setPreferredSize(new Dimension JavaDoc(300, 400));
259         f.getContentPane().add(c);
260         f.getContentPane().setBackground(Color.white);
261         f.pack();
262         f.setVisible(true);
263     }
264
265 }
266
Popular Tags