KickJava   Java API By Example, From Geeks To Geeks.

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


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.Graphics2D JavaDoc;
22 import java.awt.Rectangle JavaDoc;
23 import java.awt.RenderingHints JavaDoc;
24 import java.awt.image.BufferedImage JavaDoc;
25
26 /**
27  * This test validates the convertion of Java 2D TexturePaints
28  * into SVG patterns and fill and fill-opacity values
29  *
30  * @author <a HREF="mailto:cjolif@ilog.fr">Christophe Jolif</a>
31  * @author <a HREF="mailto:vhardy@eng.sun.com">Vincent Hardy</a>
32  * @version $Id: Texture.java,v 1.4 2004/08/18 07:16:45 vhardy Exp $
33  */

34 public class Texture implements Painter {
35     public void paint(Graphics2D JavaDoc g) {
36         g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
37                            RenderingHints.VALUE_ANTIALIAS_ON);
38         Color JavaDoc labelColor = Color.black;
39
40         BufferedImage JavaDoc texture = new BufferedImage JavaDoc(20, 20, BufferedImage.TYPE_INT_RGB);
41         Graphics2D JavaDoc bg = texture.createGraphics();
42         bg.setPaint(Color.red);
43         bg.fillRect(0, 0, 10, 10);
44         bg.setPaint(Color.yellow);
45         bg.fillRect(10, 10, 10, 10);
46         bg.dispose();
47
48         Rectangle JavaDoc anchors[] = { new Rectangle JavaDoc(0, 0, texture.getWidth(), texture.getHeight()),
49                                 new Rectangle JavaDoc(texture.getWidth()/2, texture.getHeight()/2, texture.getWidth(), texture.getHeight()),
50                                 new Rectangle JavaDoc(0, 0, texture.getWidth()/2, texture.getHeight()/2) };
51
52         String JavaDoc anchorDesc[] = { "Anchor matches texture image",
53                                 "Anchor offset to texture image center",
54                                 "Anchor half the size of texture" };
55
56         // Now, fill a rectangle that is 4 times the size of the texture
57
// along each axis, once for each texture.
58

59         g.translate(0, 20);
60
61         for(int i=0; i<anchors.length; i++){
62             java.awt.TexturePaint JavaDoc texturePaint = new java.awt.TexturePaint JavaDoc(texture, anchors[i]);
63             g.setPaint(texturePaint);
64             g.fillRect(0, 0, texture.getWidth()*4, texture.getHeight()*4);
65             java.awt.geom.AffineTransform JavaDoc curTxf = g.getTransform();
66             g.translate(150, 0);
67             g.shear(.5, 0);
68             g.fillRect(0, 0, texture.getWidth()*4, texture.getHeight()*4);
69             g.setTransform(curTxf);
70             g.setPaint(labelColor);
71             g.drawString(anchorDesc[i], 10, texture.getHeight()*4 + 20);
72             g.translate(0, texture.getHeight()*4 + 40);
73         }
74     }
75 }
76
Popular Tags