KickJava   Java API By Example, From Geeks To Geeks.

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


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.AlphaComposite JavaDoc;
21 import java.awt.BasicStroke JavaDoc;
22 import java.awt.Color JavaDoc;
23 import java.awt.Graphics2D JavaDoc;
24 import java.awt.Rectangle JavaDoc;
25 import java.awt.RenderingHints JavaDoc;
26
27 /**
28  * This test color opacity on fill and strokes, because this
29  * is handled differently in the Java 2D API than in SVG.
30  *
31  * @author <a HREF="mailto:cjolif@ilog.fr">Christophe Jolif</a>
32  * @author <a HREF="mailto:vhardy@eng.sun.com">Vincent Hardy</a>
33  * @version $Id: Color2.java,v 1.4 2004/08/18 07:16:44 vhardy Exp $
34  */

35 public class Color2 implements Painter {
36     public void paint(Graphics2D JavaDoc g) {
37         g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
38                            RenderingHints.VALUE_ANTIALIAS_ON);
39
40         // Define Colors
41
Color JavaDoc blue = Color.blue;
42         Color JavaDoc green = Color.green;
43         Color JavaDoc transparentBlue = new Color JavaDoc(0, 0, 255, 128);
44         Color JavaDoc transparentGreen = new Color JavaDoc(0, 255, 0, 128);
45
46         // Define AlphaComposites
47
AlphaComposite JavaDoc srcOver = AlphaComposite.SrcOver;
48         AlphaComposite JavaDoc srcOverTransparent = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .5f);
49
50         // Define rectangle
51
Rectangle JavaDoc rect = new Rectangle JavaDoc(10, 40, 100, 50);
52
53         // Define thick stroke
54
BasicStroke JavaDoc thickStroke = new BasicStroke JavaDoc(5);
55
56         // First test: Opaque Colors with AlphaComposite
57
g.setPaint(Color.black);
58         g.drawString("Opaque Colors, Half Transparent AlphaComposite", 10, 30);
59
60         g.setComposite(srcOverTransparent);
61         g.setStroke(thickStroke);
62         g.setPaint(blue);
63         g.fill(rect);
64         g.setPaint(green);
65         g.draw(rect);
66         g.setPaint(Color.black);
67         g.fill(rect);
68
69         g.translate(0, 90);
70
71         // Second test: transparent color, opaque Source Over
72
g.setPaint(Color.black);
73         g.setComposite(srcOver);
74         g.drawString("Transparent Colors, Opaque AlphaComposite SrcOver", 10, 30);
75
76         g.setPaint(transparentBlue);
77         g.fill(rect);
78         g.setPaint(transparentGreen);
79         g.draw(rect);
80     }
81 }
82
Popular Tags