KickJava   Java API By Example, From Geeks To Geeks.

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


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
23 /**
24  * This test validates the convertion of Java 2D GradientPaints
25  * into SVG linearGradient definition and reference.
26  *
27  * @author <a HREF="mailto:cjolif@ilog.fr">Christophe Jolif</a>
28  * @author <a HREF="mailto:vhardy@eng.sun.com">Vincent Hardy</a>
29  * @version $Id: Gradient.java,v 1.3 2004/08/18 07:16:44 vhardy Exp $
30  */

31 public class Gradient implements Painter {
32     public void paint(Graphics2D g) {
33         g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
34                            RenderingHints.VALUE_ANTIALIAS_ON);
35
36         java.awt.geom.AffineTransform JavaDoc defaultTransform = g.getTransform();
37         Color labelColor = Color.black;
38
39         //
40
// First, define cross hair marker
41
//
42
GeneralPath crossHair = new GeneralPath();
43         crossHair.moveTo(-5, 0);
44         crossHair.lineTo(5, 0);
45         crossHair.moveTo(0, -5);
46         crossHair.lineTo(0, 5);
47
48         //
49
// Simple test checking color values and start
50
// and end points
51
//
52
java.awt.GradientPaint JavaDoc gradient = new java.awt.GradientPaint JavaDoc(30, 40, Color.red,
53                                                    30, 120, Color.yellow);
54         g.setPaint(labelColor);
55         g.drawString("Simple vertical gradient", 10, 20);
56         g.setPaint(gradient);
57         g.fillRect(10, 30, 100, 100);
58         g.setPaint(labelColor);
59         g.translate(30, 40);
60         g.draw(crossHair);
61         g.setTransform(defaultTransform);
62         g.translate(30, 120);
63         g.draw(crossHair);
64
65         g.setTransform(defaultTransform);
66         g.translate(0, 140);
67
68         //
69
// Now, test cycling behavior
70
//
71
java.awt.GradientPaint JavaDoc nonCyclicGradient = new java.awt.GradientPaint JavaDoc(0, 0, Color.red,
72                                                             20, 0, Color.yellow);
73         java.awt.GradientPaint JavaDoc cyclicGradient = new java.awt.GradientPaint JavaDoc(0, 0, Color.red,
74                                                          20, 0, Color.yellow, true);
75
76         g.setPaint(labelColor);
77         g.drawString("Non Cyclic / Cyclic Gradients", 10, 20);
78
79         g.translate(10, 30);
80
81         g.setPaint(nonCyclicGradient);
82         g.fillRect(0, 0, 100, 30);
83
84         g.translate(0, 30);
85         g.setPaint(cyclicGradient);
86         g.fillRect(0, 0, 100, 30);
87
88         g.setPaint(labelColor);
89         g.drawLine(0, 0, 100, 0);
90
91         g.setTransform(defaultTransform);
92         g.translate(0, 240);
93
94         //
95
// Now, test transformations
96
//
97
g.setPaint(labelColor);
98         g.drawString("Sheared GradientPaint", 10, 20);
99         g.translate(10, 25);
100
101         java.awt.GradientPaint JavaDoc shearedGradient = new java.awt.GradientPaint JavaDoc(0, 0, Color.red,
102                                                           100, 0, Color.yellow);
103         g.setPaint(shearedGradient);
104         g.shear(0.5, 0);
105
106         g.fillRect(0, 0, 100, 40);
107
108         g.setTransform(defaultTransform);
109         g.translate(0, 320);
110
111         g.setPaint(labelColor);
112         g.drawString("Opacity in stop color", 10, 20);
113
114         java.awt.GradientPaint JavaDoc transparentGradient = new java.awt.GradientPaint JavaDoc(10, 30, new Color(255, 0, 0, 0),
115                                                                                 110, 30, Color.yellow);
116
117         g.setPaint(transparentGradient);
118         g.fillRect(10, 30, 100, 30);
119     }
120 }
121
Popular Tags