KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > iv > flash > api > shape > FillStyle


1 /*
2  * $Id: FillStyle.java,v 1.3 2002/03/02 01:39:44 awason Exp $
3  *
4  * ==========================================================================
5  *
6  * The JGenerator Software License, Version 1.0
7  *
8  * Copyright (c) 2000 Dmitry Skavish (skavish@usa.net). All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if
22  * any, must include the following acknowlegement:
23  * "This product includes software developed by Dmitry Skavish
24  * (skavish@usa.net, http://www.flashgap.com/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The name "The JGenerator" must not be used to endorse or promote
29  * products derived from this software without prior written permission.
30  * For written permission, please contact skavish@usa.net.
31  *
32  * 5. Products derived from this software may not be called "The JGenerator"
33  * nor may "The JGenerator" appear in their names without prior written
34  * permission of Dmitry Skavish.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL DMITRY SKAVISH OR THE OTHER
40  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  *
49  */

50
51 package org.openlaszlo.iv.flash.api.shape;
52
53 import java.awt.geom.*;
54 import java.io.PrintStream JavaDoc;
55 import org.openlaszlo.iv.flash.util.*;
56 import org.openlaszlo.iv.flash.api.*;
57 import org.openlaszlo.iv.flash.parser.*;
58 import org.openlaszlo.iv.flash.api.image.*;
59
60 /**
61  * FillStyle of Shape.
62  * <P>
63  * SWF supports three basic types of fills for a shape.
64  * <UL>
65  * <LI>Solid fill - A simple RGBA color that fills a portion of a shape.
66  * An alpha value of 255 means a completely opaque fill.
67  * An alpha value of zero means a completely transparent fill.
68  * Any alpha between 0 and 255 will be partially transparent.
69  * <LI>Gradient Fill - A gradient fill can be either a linear or a radial gradient.
70  * See {@link org.openlaszlo.iv.flash.api.Gradient}s for an in depth description of how gradients are defined.
71  * <LI>Bitmap fill - Bitmap fills refer to a bitmap characterId.
72  * There are two styles: clipped and tiled. A clipped bitmap fill repeats the color on
73  * the edge of a bitmap if the fill extends beyond the edge of the bitmap.
74  * A tiled fill repeats the bitmap if the fill extends beyond the edge of the bitmap.
75  * </UL>
76  *
77  * @author Dmitry Skavish
78  */

79 public final class FillStyle extends FlashItem {
80
81     public static final int SOLID = 0x00;
82     public static final int LINEAR_GRADIENT = 0x10;
83     public static final int RADIAL_GRADIENT = 0x12;
84     public static final int TILED_BITMAP = 0x40;
85     public static final int CLIPPED_BITMAP = 0x41;
86
87     private int type; // type of this fillstyle
88
private Color color; // color of this fillstyle
89
private AffineTransform matrix; // matrix of this fillstyle
90
private Gradient gradient; // gradient of this fillstyle
91
private Bitmap bitmap; // bitmap of this fillstyle
92

93     public FillStyle() {}
94
95     public int getType() {
96         return type;
97     }
98
99     public void setType( int type ) {
100         this.type = type;
101     }
102
103     public Color getColor() {
104         return color;
105     }
106
107     public void setColor( Color color ) {
108         this.color = color;
109     }
110
111     public AffineTransform getMatrix() {
112         return matrix;
113     }
114
115     public void setMatrix( AffineTransform matrix ) {
116         this.matrix = matrix;
117     }
118
119     public Gradient getGraduent() {
120         return gradient;
121     }
122
123     public void setGradient( Gradient gradient ) {
124         this.gradient = gradient;
125     }
126
127     public Bitmap getBitmap() {
128         return bitmap;
129     }
130
131     public void setBitmap( Bitmap bitmap ) {
132         this.bitmap = bitmap;
133     }
134
135     /**
136      * Creates new solid fill.
137      *
138      * @param color color of solid fill
139      * @return solid fillstyle
140      */

141     public static FillStyle newSolid( Color color ) {
142         FillStyle fs = new FillStyle();
143         fs.setColor( color );
144         fs.setType( SOLID );
145         return fs;
146     }
147
148     /**
149      * Creates new linear gradient fill.
150      *
151      * @param gradient gradient of gradient fill
152      * @param matrix matrix of gradient fill
153      * @return linear gradient fill
154      */

155     public static FillStyle newLinearGradient( Gradient gradient, AffineTransform matrix ) {
156         FillStyle fs = new FillStyle();
157         fs.setGradient( gradient );
158         fs.setMatrix( matrix );
159         fs.setType( LINEAR_GRADIENT );
160         return fs;
161     }
162
163     /**
164      * Creates new radial gradient fill.
165      *
166      * @param gradient gradient of gradient fill
167      * @param matrix matrix of gradient fill
168      * @return radial gradient fill
169      */

170     public static FillStyle newRadialGradient( Gradient gradient, AffineTransform matrix ) {
171         FillStyle fs = new FillStyle();
172         fs.setGradient( gradient );
173         fs.setMatrix( matrix );
174         fs.setType( RADIAL_GRADIENT );
175         return fs;
176     }
177
178     /**
179      * Creates new tiled bitmap fill.
180      *
181      * @param bitmap bitmap of bitmap fill
182      * @param matrix matrix of bitmap fill
183      * @return tiled bitmap fill
184      */

185     public static FillStyle newTiledBitmap( Bitmap bitmap, AffineTransform matrix ) {
186         FillStyle fs = new FillStyle();
187         fs.setBitmap( bitmap );
188         fs.setMatrix( matrix );
189         fs.setType( TILED_BITMAP );
190         return fs;
191     }
192
193     /**
194      * Creates new clipped bitmap fill.
195      *
196      * @param bitmap bitmap of bitmap fill
197      * @param matrix matrix of bitmap fill
198      * @return clipped bitmap fill
199      */

200     public static FillStyle newClippedBitmap( Bitmap bitmap, AffineTransform matrix ) {
201         FillStyle fs = new FillStyle();
202         fs.setBitmap( bitmap );
203         fs.setMatrix( matrix );
204         fs.setType( CLIPPED_BITMAP );
205         return fs;
206     }
207
208     /**
209      * Creates new tiled bitmap fill with default tranformation matrix.
210      *
211      * @param bitmap bitmap of bitmap fill
212      * @return tiled bitmap fill
213      */

214     public static FillStyle newTiledBitmap( Bitmap bitmap ) {
215         AffineTransform matrix = AffineTransform.getScaleInstance(20.0, 20.0);
216         return newTiledBitmap( bitmap, matrix );
217     }
218
219     /**
220      * Creates new clipped bitmap fill with default tranformation matrix.
221      *
222      * @param bitmap bitmap of bitmap fill
223      * @return clipped bitmap fill
224      */

225     public static FillStyle newClippedBitmap( Bitmap bitmap ) {
226         AffineTransform matrix = AffineTransform.getScaleInstance(20.0, 20.0);
227         return newClippedBitmap( bitmap, matrix );
228     }
229
230     public static FillStyle parse( Parser p, boolean withAlpha ) {
231         FillStyle fs = new FillStyle();
232         int type = fs.type = p.getUByte();
233
234         if( (type&0x10) != 0 ) { // gradient
235

236             // Get the gradient matrix.
237
fs.matrix = p.getMatrix();
238             // get gradient itself
239
fs.gradient = Gradient.parse(p, withAlpha);
240
241         } else if( (type&0x40) != 0 ) { // bitmap
242

243             int id = p.getUWord(); // id may be equal to 0xffff, I don't know what it means
244
fs.bitmap = (Bitmap) p.getDef(id);
245             fs.matrix = p.getMatrix();
246
247         } else { // A solid color
248

249             fs.color = Color.parse(p, withAlpha);
250         }
251
252         return fs;
253     }
254
255     public void write( FlashOutput fob ) {
256         fob.writeByte(type);
257         switch( type ) {
258             case SOLID:
259                 Shape shape = (Shape) fob.getUserData();
260                 if( shape.isWithAlpha() ) {
261                     color.writeRGBA(fob);
262                 } else {
263                     color.writeRGB(fob);
264                 }
265                 break;
266             case LINEAR_GRADIENT:
267             case RADIAL_GRADIENT:
268                 fob.write(matrix);
269                 gradient.write(fob);
270                 break;
271             case TILED_BITMAP:
272             case CLIPPED_BITMAP:
273                 if( bitmap == null ) {
274                     fob.writeWord(0xffff);
275                 } else {
276                     fob.writeDefID(bitmap);
277                 }
278                 fob.write(matrix);
279                 break;
280         }
281     }
282
283     public void printContent( PrintStream JavaDoc out, String JavaDoc indent ) {
284         switch( type ) {
285             case SOLID:
286                 out.println( indent+"FillStyle (SOLID):" );
287                 color.printContent(out, indent+" ");
288                 break;
289             case LINEAR_GRADIENT:
290                 out.println( indent+"FillStyle (LINEAR_GRADIENT):" );
291                 out.println( indent+" "+matrix );
292                 gradient.printContent(out, indent+" ");
293                 break;
294             case RADIAL_GRADIENT:
295                 out.println( indent+"FillStyle (RADIAL_GRADIENT):" );
296                 out.println( indent+" "+matrix );
297                 gradient.printContent(out, indent+" ");
298                 break;
299             case TILED_BITMAP:
300                 out.println( indent+"FillStyle (TILED_BITMAP):" );
301                 out.println( indent+" "+matrix );
302                 out.println( indent+" bitmapID="+ (bitmap == null ? 0xffff : bitmap.getID()) );
303                 break;
304             case CLIPPED_BITMAP:
305                 out.println( indent+"FillStyle (CLIPPED_BITMAP):" );
306                 out.println( indent+" "+matrix );
307                 out.println( indent+" bitmapID="+ (bitmap == null ? 0xffff : bitmap.getID()) );
308                 break;
309         }
310     }
311
312     protected FlashItem copyInto( FlashItem item, ScriptCopier copier ) {
313         ((FillStyle)item).type = type;
314         switch( type ) {
315             case SOLID:
316                 ((FillStyle)item).color = (Color) color.getCopy(copier);
317                 break;
318             case LINEAR_GRADIENT:
319             case RADIAL_GRADIENT:
320                 ((FillStyle)item).gradient = (Gradient) gradient.getCopy(copier);
321                 ((FillStyle)item).matrix = (AffineTransform) matrix.clone();
322                 break;
323             case TILED_BITMAP:
324             case CLIPPED_BITMAP:
325                 ((FillStyle)item).bitmap = (Bitmap) copier.copy(bitmap);
326                 ((FillStyle)item).matrix = (AffineTransform) matrix.clone();
327                 break;
328         }
329         return item;
330     }
331
332     public FlashItem getCopy( ScriptCopier copier ) {
333         return copyInto( new FillStyle(), copier );
334     }
335 }
336
Popular Tags