KickJava   Java API By Example, From Geeks To Geeks.

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


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 the convertion of the three elementary
26  * thypes of Java 2D API graphic objects: shapes, text and
27  * images
28  *
29  * @author <a HREF="mailto:cjolif@ilog.fr">Christophe Jolif</a>
30  * @author <a HREF="mailto:vhardy@eng.sun.com">Vincent Hardy</a>
31  * @version $Id: GraphicObjects.java,v 1.6 2004/08/18 07:16:44 vhardy Exp $
32  */

33 public class GraphicObjects implements Painter {
34     public void paint(Graphics2D g) {
35         g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
36                            RenderingHints.VALUE_ANTIALIAS_ON);
37
38         // Text
39
g.setPaint(Color.black);
40         g.setFont(new Font("Times New Roman", Font.PLAIN, 20));
41         g.drawString("Hello SVG drawString(...)", 20, 40);
42
43         g.translate(0, 70);
44
45         // Shapes
46
Ellipse2D ellipse = new Ellipse2D.Float(20, 0, 60, 60);
47         g.setPaint(new Color(176, 22, 40));
48         g.fill(ellipse);
49         g.translate(60, 0);
50         g.setPaint(new Color(208, 170, 119));
51         g.fill(ellipse);
52         g.translate(60, 0);
53         g.setPaint(new Color(221, 229, 111));
54         g.fill(ellipse);
55         g.translate(60, 0);
56         g.setPaint(new Color(240, 165, 0));
57         g.fill(ellipse);
58
59         g.translate(-180, 60);
60
61         // Draw background pattern
62
BufferedImage pattern = new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB);
63         Graphics2D ig = pattern.createGraphics();
64         ig.setPaint(Color.white);
65         ig.fillRect(0, 0, 10, 10);
66         ig.setPaint(new Color(0xaaaaaa));
67         ig.fillRect(0, 0, 5, 5);
68         ig.fillRect(5, 5, 5, 50);
69         TexturePaint texture = new TexturePaint(pattern, new Rectangle(0, 0, 10, 10));
70
71         // Image
72
BufferedImage image = new BufferedImage(200, 150, BufferedImage.TYPE_INT_ARGB);
73         ig = image.createGraphics();
74         ig.setPaint(texture);
75         ig.fillRect(0, 0, 200, 150);
76         g.drawImage(image, 40, 40, null);
77
78         image = new BufferedImage(200, 150, BufferedImage.TYPE_INT_ARGB);
79         ig = image.createGraphics();
80         GradientPaint paint = new GradientPaint(0, 0, new Color(103, 103, 152),
81                                                         200, 150, new Color(103, 103, 152, 0));
82         ig.setPaint(paint);
83         ig.fillRect(0, 0, 200, 150);
84         ig.setPaint(Color.black);
85         ig.setFont(new Font("Arial", Font.PLAIN, 10));
86         ig.drawString("This is an image with alpha", 10, 30);
87         ig.dispose();
88
89         g.drawImage(image, 40, 40, null);
90     }
91 }
92
Popular Tags