KickJava   Java API By Example, From Geeks To Geeks.

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


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

37 public class Paints implements Painter {
38     public void paint(Graphics2D JavaDoc g) {
39         g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
40                            RenderingHints.VALUE_ANTIALIAS_ON);
41
42         // Get default paint for painting text
43
Paint JavaDoc defaultPaint = Color.black;
44         g.setPaint(defaultPaint);
45
46         g.translate(0, 30);
47
48         // Define the rectangle that will be drawn multiple
49
// times
50
Rectangle JavaDoc rect = new Rectangle JavaDoc(10, 20, 100, 60);
51
52         // First, test plain color with transparency
53
Color JavaDoc fillColor = new Color JavaDoc(255, 255, 0, 128);
54         g.drawString("Semi transparent black", 10, 10);
55         g.drawString("Behind Rectangle", 40, 60);
56         g.setPaint(fillColor);
57         g.fill(rect);
58
59         g.translate(0, 90);
60
61         // Now, test linear gradient
62
GradientPaint JavaDoc fillGradient = new GradientPaint JavaDoc(10, 20, Color.red,
63                                                        110, 80, Color.yellow);
64         g.setPaint(defaultPaint);
65         g.drawString("Red to Yellow linear gradient", 10, 10);
66         g.setPaint(fillGradient);
67         g.fill(rect);
68
69         g.translate(0, 90);
70
71         // Now, test texture paint
72
BufferedImage JavaDoc buf = new BufferedImage JavaDoc(20, 20, BufferedImage.TYPE_INT_RGB);
73         Graphics2D JavaDoc bg = buf.createGraphics();
74         bg.setPaint(Color.red);
75         bg.fillRect(0, 0, 10, 10);
76         bg.setPaint(Color.yellow);
77         bg.fillRect(10, 10, 10, 10);
78         bg.dispose();
79         TexturePaint JavaDoc fillTexture = new TexturePaint JavaDoc(buf, new Rectangle JavaDoc(10, 20, 20, 20));
80         g.setPaint(defaultPaint);
81         g.drawString("Texture Paint", 10, 10);
82         g.setPaint(fillTexture);
83         g.fill(rect);
84     }
85 }
86
Popular Tags