KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > faceless > pdf > ColorPattern


1 // $Id: ColorPattern.java,v 1.5 2003/11/04 17:16:01 mike Exp $
2

3 package org.faceless.pdf;
4
5 import java.awt.*;
6
7 /**
8  * <p>
9  * A ColorPattern is a subclass of <code>Paint</code> that can be used with
10  * this library to fill shapes with a pattern. Currently the patterns must be
11  * chosen from a list of predefined patterns, and are limited to two colors
12  * (although this may change in future versions of the library). Prior to
13  * version 1.2 of the library, this class was a subclass of <code>java.awt.Color</code>.
14  * Here's an example.
15  * </p>
16  * <pre>
17  * PDFStyle style = new PDFStyle();
18  * ColorPattern c = ColorPattern.star(Color.red, Color.blue, 20);
19  * style.setFillColor(c);
20  * page.setStyle(style);
21  * page.drawRectangle(100,100, 200,200);
22  * </pre>
23  * This will draw a rectangle filled with red stars on a blue background.
24  * @since 1.1
25  */

26 public class ColorPattern extends PeeredObject implements Paint, Cloneable JavaDoc
27 {
28     final org.faceless.pdf2.PDFPattern pattern;
29
30     Object JavaDoc getPeer()
31     {
32         return pattern;
33     }
34
35     ColorPattern(org.faceless.pdf2.PDFPattern pattern)
36     {
37         this.pattern=pattern;
38     }
39
40     /**
41      * Create a new ColorPattern consisting of parallel stripes.
42      * @param background the background color of the pattern
43      * @param foreground the foreground color of the pattern
44      * @param on the width of each stripe, in points
45      * @param off the width of the space between each stripe, in points
46      * @param angle the angle of the stripe in degrees clockwise, with 0 at 12 o'clock
47      */

48     public static ColorPattern stripe(Color background, Color foreground, float on, float off, float angle)
49     {
50     return new ColorPattern(new org.faceless.pdf2.PDFPattern("Stripe"+((int)angle), 0, 0, on, off, foreground, background));
51     }
52
53     /**
54      * Create a new ColorPattern resembling a brick wall (specifically, a brick
55      * wall with a running pattern bond).
56      * @param background the background color of the pattern
57      * @param foreground the foreground color of the pattern
58      * @param width the width of each brick, in points
59      * @param height the height of each brick, in points
60      */

61     public static ColorPattern brick(Color background, Color foreground, float width, float height)
62     {
63     return new ColorPattern(new org.faceless.pdf2.PDFPattern("Brick", 0, 0, width, height*2, foreground, background));
64     }
65
66     /**
67      * Create a new ColorPattern with a simple check pattern.
68      * @param background the background color of the pattern
69      * @param foreground the foreground color of the pattern
70      * @param size the width and height of each check, in points
71      */

72     public static ColorPattern check(Color background, Color foreground, float size)
73     {
74     return new ColorPattern(new org.faceless.pdf2.PDFPattern("Check", 0, 0, size*2, size*2, foreground, background));
75     }
76
77     /**
78      * Create a new ColorPattern with a simple grid pattern.
79      * @param background the background color of the pattern
80      * @param foreground the foreground color of the pattern
81      * @param on the thickness of each line on the grid, in points
82      * @param off the distance between each line on the grid, in points
83      */

84     public static ColorPattern grid(Color background, Color foreground, float on, float off)
85     {
86     return new ColorPattern(new org.faceless.pdf2.PDFPattern("Grid", 0, 0, on+off, on+off, background, foreground));
87     }
88
89     /**
90      * Create a new ColorPattern with a spot pattern, similar to that used in
91      * halftoning patterns in newspapers.
92      * @param background the background color of the pattern
93      * @param foreground the foreground color of the pattern
94      * @param size the radius of each spot
95      */

96     public static ColorPattern spot(Color background, Color foreground, float size)
97     {
98     return new ColorPattern(new org.faceless.pdf2.PDFPattern("Spot", 0, 0, size*2, size*2, foreground, background));
99     }
100
101     /**
102      * Create a new ColorPattern with a polka dot pattern of random spots.
103      * @param background the background color of the pattern
104      * @param foreground the foreground color of the pattern
105      * @param size The size of the pattern in points. Each spot is between 10% and
106      * 20% of this size.
107      */

108     public static ColorPattern polka(Color background, Color foreground, float size)
109     {
110     return new ColorPattern(new org.faceless.pdf2.PDFPattern("Polka", 0, 0, size*2, size*2, foreground, background));
111     }
112
113     /**
114      * Create a new ColorPattern with a five-pointed star pattern, like that on
115      * the US flag.
116      * @param background the background color of the pattern
117      * @param foreground the foreground color of the pattern
118      * @param size the size of each star, in points
119      */

120     public static ColorPattern star(Color background, Color foreground, float size)
121     {
122     return new ColorPattern(new org.faceless.pdf2.PDFPattern("Star", 0, 0, size*2, size*2, foreground, background));
123     }
124
125     /**
126      * Return a copy of this ColorPattern with the brightness adjusted.
127      * Prior to version 1.2 this method adjusted the color inline and
128      * returned <code>void</code>.
129      *
130      * @param delta the amount to add the the brightness. The value
131      * should be between -1 and 1
132      * @param min the minimum allowable level of brightness. May
133      * be between 0 and 1
134      * @param max the maximum allowable level of brightness. May
135      * be between 0 and 1
136      */

137     public ColorPattern adjustBrightness(float delta, float min, float max)
138     {
139     return (ColorPattern)PeeredObject.getPeer(pattern.brightnessClone(delta,min,max));
140     }
141
142     public Object JavaDoc clone()
143     {
144 // return (ColorPattern)PeeredObject.getPeer(pattern.clone()); //TODO
145
throw new Error JavaDoc("Not implemented");
146     }
147
148     public int getTransparency()
149     {
150     return pattern.getTransparency();
151     }
152
153     /**
154      * As this method is intended for AWT use only, it is not implemented
155      * and will throw an <code>UnsupportedOperationException</code> if called
156      */

157     public PaintContext createContext(java.awt.image.ColorModel JavaDoc cm, Rectangle deviceBounds, java.awt.geom.Rectangle2D JavaDoc userBounds, java.awt.geom.AffineTransform JavaDoc xform, RenderingHints hints)
158     {
159         throw new UnsupportedOperationException JavaDoc("createContext not appropriate for ColorPattern");
160     }
161
162
163     /**
164      * Return the background color of the pattern
165      */

166     public Color getBackgroundColor()
167     {
168         return null;
169     }
170
171     /**
172      * Return the foreground color of the pattern
173      */

174     public Color getForegroundColor()
175     {
176         return null;
177     }
178 }
179
Popular Tags