KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > transcoder > image > BackgroundColorTest


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.transcoder.image;
19
20 import java.awt.Color JavaDoc;
21 import java.awt.Graphics2D JavaDoc;
22 import java.awt.image.BufferedImage JavaDoc;
23
24 import java.io.ByteArrayOutputStream JavaDoc;
25
26 import java.util.Map JavaDoc;
27 import java.util.HashMap JavaDoc;
28
29 import org.apache.batik.transcoder.TranscoderInput;
30 import org.apache.batik.transcoder.TranscoderOutput;
31
32 import org.apache.batik.dom.svg.SVGDOMImplementation;
33
34 import org.w3c.dom.Document JavaDoc;
35 import org.w3c.dom.Element JavaDoc;
36 import org.w3c.dom.DOMImplementation JavaDoc;
37
38 /**
39  * Test the ImageTranscoder with the KEY_BACKGROUND_COLOR transcoding hint.
40  *
41  * @author <a HREF="mailto:Thierry.Kormann@sophia.inria.fr">Thierry Kormann</a>
42  * @version $Id: BackgroundColorTest.java,v 1.5 2004/08/18 07:17:12 vhardy Exp $
43  */

44 public class BackgroundColorTest extends AbstractImageTranscoderTest {
45
46     /**
47      * Constructs a new <tt>BackgroundColorTest</tt>.
48      */

49     public BackgroundColorTest() {
50     }
51
52     /**
53      * Creates the <tt>TranscoderInput</tt>.
54      */

55     protected TranscoderInput createTranscoderInput() {
56     DOMImplementation impl = SVGDOMImplementation.getDOMImplementation();
57     String JavaDoc svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
58     Document JavaDoc doc = impl.createDocument(svgNS, "svg", null);
59
60     Element JavaDoc root = doc.getDocumentElement();
61
62     root.setAttributeNS(null, "width", "400");
63     root.setAttributeNS(null, "height", "400");
64
65     Element JavaDoc r = doc.createElementNS(svgNS, "rect");
66     r.setAttributeNS(null, "x", "100");
67     r.setAttributeNS(null, "y", "50");
68     r.setAttributeNS(null, "width", "100");
69     r.setAttributeNS(null, "height", "50");
70     r.setAttributeNS(null, "style", "fill:red");
71     root.appendChild(r);
72
73     return new TranscoderInput(doc);
74     }
75     
76     /**
77      * Creates a Map that contains additional transcoding hints.
78      */

79     protected Map JavaDoc createTranscodingHints() {
80     Map JavaDoc hints = new HashMap JavaDoc(7);
81     hints.put(ImageTranscoder.KEY_BACKGROUND_COLOR, Color.blue);
82     return hints;
83     }
84
85     /**
86      * Returns the reference image for this test.
87      */

88     protected byte [] getReferenceImageData() {
89         try {
90             BufferedImage JavaDoc img = new BufferedImage JavaDoc
91                 (400, 400, BufferedImage.TYPE_INT_ARGB);
92             Graphics2D JavaDoc g2d = img.createGraphics();
93             g2d.setColor(Color.blue);
94             g2d.fillRect(0, 0, 400, 400);
95             g2d.setColor(Color.red);
96             g2d.fillRect(100, 50, 100, 50);
97             ByteArrayOutputStream JavaDoc ostream = new ByteArrayOutputStream JavaDoc();
98             PNGTranscoder t = new PNGTranscoder();
99             TranscoderOutput output = new TranscoderOutput(ostream);
100             t.writeImage(img, output);
101             return ostream.toByteArray();
102         } catch (Exception JavaDoc ex) {
103             throw new RuntimeException JavaDoc("BackgroundColorTest error");
104         }
105     }
106 }
107
Popular Tags