KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Copyright 2001 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.*;
21 import java.awt.geom.*;
22 import java.awt.image.*;
23
24 /**
25  * This test validates convertion of Java 2D clip inot SVG clipPath
26  * definition and attributes.
27  *
28  * @author <a HREF="mailto:cjolif@ilog.fr">Christophe Jolif</a>
29  * @author <a HREF="mailto:vhardy@eng.sun.com">Vincent Hardy</a>
30  * @version $Id: Clip.java,v 1.3 2004/08/18 07:16:44 vhardy Exp $
31  */

32 public class Clip implements Painter {
33     public void paint(Graphics2D g) {
34         g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
35                            RenderingHints.VALUE_ANTIALIAS_ON);
36
37         // Save original clip
38
Shape clipShape = g.getClip();
39         java.awt.geom.AffineTransform JavaDoc transform = g.getTransform();
40
41         g.setPaint(Color.black);
42
43         Dimension size = new Dimension(300, 400);
44         int w=100, h=50;
45         int vOffset = h + 20;
46         BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
47         Graphics2D gi = image.createGraphics();
48         gi.setPaint(Color.white);
49         gi.fillRect(0, 0, 100, 50);
50         gi.setPaint(Color.green);
51         gi.fillRect(0, 0, 50, 25);
52         gi.setPaint(Color.black);
53         gi.fillRect(50, 0, 50, 25);
54         gi.setPaint(Color.red);
55         gi.fillRect(50, 25, 50, 25);
56         gi.dispose();
57
58         // Set simple clip : does not modify the output
59
g.clipRect(0, 0, size.width, size.height);
60         g.drawImage(image, 0, 0, null);
61         g.setClip(clipShape);
62
63         g.drawString("Clip set to device bounds", 110, 25);
64
65         g.translate(0, vOffset);
66
67         // Intersect current clip with a smaller clip : show only
68
// the top right corner of the image
69
g.drawString("Clip set to upper right quarter", 110, 25);
70
71         g.clipRect(w/2, 0, w/2, h/2);
72         g.drawImage(image, 0, 0, null);
73
74         // Restore
75
g.setTransform(transform);
76         g.setClip(clipShape);
77         g.translate(0, 2*vOffset);
78
79         // Scale before setting the same clip
80
g.drawString("Clip set to upper right quarter", 110, 15);
81         g.drawString("after .5 scale", 110, 30);
82         g.scale(.5, .5);
83         g.clipRect(w/2, 0, w/2, h/2);
84         g.drawImage(image, 0, 0, null);
85
86         // Restore
87
g.setTransform(transform);
88         g.setClip(clipShape);
89
90         g.translate(0, 3*vOffset);
91
92         // Use a non-rectangle clipping area
93
g.drawString("Non-Rectagular clip", 110, 25);
94         Shape circle = new Ellipse2D.Float(0, 0, w, h);
95         g.clip(circle);
96         g.drawImage(image, 0, 0, null);
97
98         // Restore
99
g.setTransform(transform);
100         g.setClip(clipShape);
101
102         g.translate(0, 4*vOffset);
103
104         // Use a non-rectangle clipping area again,
105
// after setting a scale transform
106
g.drawString("Non-Rectagular clip after", 110, 15);
107         g.drawString(".5 scale", 110, 30);
108         g.scale(.5, .5);
109         g.clip(circle);
110         g.drawImage(image, 0, 0, null);
111
112         // Restore
113
g.setTransform(transform);
114         g.setClip(clipShape);
115         g.translate(0, 5*vOffset);
116
117         // Use a non-rectangle clipping area again,
118
// before setting a scale transform
119
g.drawString("Non-Rectagular clip before", 110, 15);
120         g.drawString(".5 scale", 110, 30);
121         g.clip(circle);
122         g.scale(.5, .5);
123         g.drawImage(image, 0, 0, null);
124     }
125 }
126
Popular Tags