KickJava   Java API By Example, From Geeks To Geeks.

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


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.Graphics2D JavaDoc;
21 import java.awt.RenderingHints JavaDoc;
22 import java.awt.Font JavaDoc;
23
24 /**
25  * This test validates convertion of Java 2D Color into SVG fill,
26  * stroke and opacity 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: Color1.java,v 1.4 2004/08/18 07:16:44 vhardy Exp $
31  */

32 public class Color1 implements Painter {
33     public void paint(Graphics2D JavaDoc g) {
34         g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
35                            RenderingHints.VALUE_ANTIALIAS_ON);
36
37         java.awt.geom.AffineTransform JavaDoc defaultTransform = g.getTransform();
38
39         // Colors used for labels and test output
40
java.awt.Color JavaDoc labelColor = java.awt.Color.black;
41
42         java.awt.Color JavaDoc colorConstants[] = { java.awt.Color.black,
43                                    java.awt.Color.blue,
44                                    java.awt.Color.cyan,
45                                    java.awt.Color.darkGray,
46                                    java.awt.Color.gray,
47                                    java.awt.Color.green,
48                                    java.awt.Color.lightGray,
49                                    java.awt.Color.magenta,
50                                    java.awt.Color.orange,
51                                    java.awt.Color.pink,
52                                    java.awt.Color.red,
53                                    java.awt.Color.white,
54                                    java.awt.Color.yellow };
55
56         String JavaDoc colorConstantStrings[] = { "black",
57                                            "blue",
58                                            "cyan",
59                                            "darkGray",
60                                            "gray",
61                                            "green",
62                                            "lightGray",
63                                            "magenta",
64                                            "orange",
65                                            "pink",
66                                            "red",
67                                            "white",
68                                            "yellow" };
69
70
71         g.translate(20, 20);
72         g.setPaint(labelColor);
73         g.drawString("Color Constants", -5, 0);
74         g.translate(0, 20);
75
76         for(int i=0; i<colorConstants.length; i++){
77             g.setPaint(labelColor);
78             g.drawString(colorConstantStrings[i], 10, 3);
79             g.setPaint(colorConstants[i]);
80             g.fillRect(-5, -5, 10, 10);
81             g.setPaint(labelColor);
82             g.drawRect(-5, -5, 10, 10);
83             g.translate(0, 20);
84         }
85
86         g.setTransform(defaultTransform);
87         g.translate(150, 20);
88         g.setColor(labelColor);
89         g.drawString("Various opacities", 0, 0);
90         g.translate(0, 10);
91
92         //
93
// Now, test opacities
94
//
95
int opacitySteps = 20;
96         g.setPaint(new java.awt.Color JavaDoc(80, 255, 80));
97         g.fillRect(0, 0, 40, 260);
98         int stepHeight = 260/opacitySteps;
99         Font JavaDoc defaultFont = g.getFont();
100         Font JavaDoc opacityFont = new Font JavaDoc(defaultFont.getFamily(),
101                                     defaultFont.getStyle(),
102                                     (int)(defaultFont.getSize()*0.8));
103         g.setFont(opacityFont);
104
105         for(int i=0; i<opacitySteps; i++){
106             int opacity = ((i + 1)*255)/opacitySteps;
107             java.awt.Color JavaDoc color = new java.awt.Color JavaDoc(0, 0, 0, opacity);
108             g.setPaint(color);
109             g.fillRect(0, 0, 40, stepHeight);
110
111             g.setPaint(labelColor);
112             g.drawString("" + opacity, 50, stepHeight/2);
113             g.translate(0, stepHeight);
114         }
115     }
116 }
117
Popular Tags