KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: MorphFillStyle.java,v 1.1 2002/02/15 23:46:26 skavish 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 MorphShape.
62  *
63  * @author Dmitry Skavish
64  * @see FillStyle
65  */

66 public final class MorphFillStyle extends FlashItem {
67
68     private int type; // type of this fillstyle
69
private Color color_start; // start color of this fillstyle
70
private Color color_end; // end color of this fillstyle
71
private AffineTransform matrix_start; // start matrix of this fillstyle
72
private AffineTransform matrix_end; // end matrix of this fillstyle
73
private MorphGradient gradient; // gradient of this fillstyle
74
private Bitmap bitmap; // bitmap of this fillstyle
75

76     public MorphFillStyle() {}
77
78     public int getType() {
79         return type;
80     }
81
82     public void setType( int type ) {
83         this.type = type;
84     }
85
86     public Color getColorStart() {
87         return color_start;
88     }
89
90     public void setColorStart( Color color ) {
91         this.color_start = color;
92     }
93
94     public Color getColorEnd() {
95         return color_end;
96     }
97
98     public void setColorEnd( Color color ) {
99         this.color_end = color;
100     }
101
102     public AffineTransform getMatrixStart() {
103         return matrix_start;
104     }
105
106     public void setMatrixStart( AffineTransform matrix ) {
107         this.matrix_start = matrix;
108     }
109
110     public AffineTransform getMatrixEnd() {
111         return matrix_end;
112     }
113
114     public void setMatrixEnd( AffineTransform matrix ) {
115         this.matrix_end = matrix;
116     }
117
118     public MorphGradient getGraduent() {
119         return gradient;
120     }
121
122     public void setGradient( MorphGradient gradient ) {
123         this.gradient = gradient;
124     }
125
126     public Bitmap getBitmap() {
127         return bitmap;
128     }
129
130     public void setBitmap( Bitmap bitmap ) {
131         this.bitmap = bitmap;
132     }
133
134     /**
135      * Creates new solid fill.
136      *
137      * @param color_start start color
138      * @param color_end end color
139      * @return solid fillstyle
140      */

141     public static MorphFillStyle newSolid( Color color_start, Color color_end ) {
142         MorphFillStyle fs = new MorphFillStyle();
143         fs.setColorStart( color_start );
144         fs.setColorEnd( color_end );
145         fs.setType( FillStyle.SOLID );
146         return fs;
147     }
148
149     /**
150      * Creates new linear gradient fill.
151      *
152      * @param gradient gradient of gradient fill
153      * @param matrix_start start matrix
154      * @param matrix_end end matrix
155      * @return linear gradient fill
156      */

157     public static MorphFillStyle newLinearGradient(
158         MorphGradient gradient,
159         AffineTransform matrix_start,
160         AffineTransform matrix_end )
161     {
162         MorphFillStyle fs = new MorphFillStyle();
163         fs.setGradient( gradient );
164         fs.setMatrixStart( matrix_start );
165         fs.setMatrixEnd( matrix_end );
166         fs.setType( FillStyle.LINEAR_GRADIENT );
167         return fs;
168     }
169
170     /**
171      * Creates new radial gradient fill.
172      *
173      * @param gradient gradient of gradient fill
174      * @param matrix_start start matrix
175      * @param matrix_end end matrix
176      * @return radial gradient fill
177      */

178     public static MorphFillStyle newRadialGradient(
179         MorphGradient gradient,
180         AffineTransform matrix_start,
181         AffineTransform matrix_end )
182     {
183         MorphFillStyle fs = new MorphFillStyle();
184         fs.setGradient( gradient );
185         fs.setMatrixStart( matrix_start );
186         fs.setMatrixEnd( matrix_end );
187         fs.setType( FillStyle.RADIAL_GRADIENT );
188         return fs;
189     }
190
191     /**
192      * Creates new tiled bitmap fill.
193      *
194      * @param bitmap bitmap of bitmap fill
195      * @param matrix_start start matrix
196      * @param matrix_end end matrix
197      * @return tiled bitmap fill
198      */

199     public static MorphFillStyle newTiledBitmap(
200         Bitmap bitmap,
201         AffineTransform matrix_start,
202         AffineTransform matrix_end )
203     {
204         MorphFillStyle fs = new MorphFillStyle();
205         fs.setBitmap( bitmap );
206         fs.setMatrixStart( matrix_start );
207         fs.setMatrixEnd( matrix_end );
208         fs.setType( FillStyle.TILED_BITMAP );
209         return fs;
210     }
211
212     /**
213      * Creates new clipped bitmap fill.
214      *
215      * @param bitmap bitmap of bitmap fill
216      * @param matrix_start start matrix
217      * @param matrix_end end matrix
218      * @return clipped bitmap fill
219      */

220     public static MorphFillStyle newClippedBitmap(
221         Bitmap bitmap,
222         AffineTransform matrix_start,
223         AffineTransform matrix_end )
224     {
225         MorphFillStyle fs = new MorphFillStyle();
226         fs.setBitmap( bitmap );
227         fs.setMatrixStart( matrix_start );
228         fs.setMatrixEnd( matrix_end );
229         fs.setType( FillStyle.CLIPPED_BITMAP );
230         return fs;
231     }
232
233     /**
234      * Parses morph fill style
235      *
236      * @param p parser
237      * @return parsed fillstyle
238      */

239     public static MorphFillStyle parse( Parser p ) {
240         MorphFillStyle fs = new MorphFillStyle();
241         int type = fs.type = p.getUByte();
242
243         if( (type&0x10) != 0 ) { // gradient
244

245             // Get the gradient matrix.
246
fs.matrix_start = p.getMatrix();
247             fs.matrix_end = p.getMatrix();
248             // get gradient itself
249
fs.gradient = MorphGradient.parse(p);
250
251         } else if( (type&0x40) != 0 ) { // bitmap
252

253             int id = p.getUWord(); // id may be equal to 0xffff, I don't know what it means
254
fs.bitmap = (Bitmap) p.getDef(id);
255             fs.matrix_start = p.getMatrix();
256             fs.matrix_end = p.getMatrix();
257
258         } else { // A solid color
259

260             fs.color_start = AlphaColor.parse(p);
261             fs.color_end = AlphaColor.parse(p);
262         }
263
264         return fs;
265     }
266
267     /**
268      * Writes fillstyle to flash buffer
269      *
270      * @param fob buffer to write
271      */

272     public void write( FlashOutput fob ) {
273         fob.writeByte(type);
274         switch( type ) {
275             case FillStyle.SOLID:
276                 color_start.writeRGBA(fob);
277                 color_end.writeRGBA(fob);
278                 break;
279             case FillStyle.LINEAR_GRADIENT:
280             case FillStyle.RADIAL_GRADIENT:
281                 fob.write(matrix_start);
282                 fob.write(matrix_end);
283                 gradient.write(fob);
284                 break;
285             case FillStyle.TILED_BITMAP:
286             case FillStyle.CLIPPED_BITMAP:
287                 if( bitmap == null ) {
288                     fob.writeWord(0xffff);
289                 } else {
290                     fob.writeDefID(bitmap);
291                 }
292                 fob.write(matrix_start);
293                 fob.write(matrix_end);
294                 break;
295         }
296     }
297
298     public void printContent( PrintStream JavaDoc out, String JavaDoc indent ) {
299         switch( type ) {
300             case FillStyle.SOLID:
301                 out.println( indent+"MorphFillStyle (SOLID):" );
302                 color_start.printContent(out, indent+" ");
303                 color_end.printContent(out, indent+" ");
304                 break;
305             case FillStyle.LINEAR_GRADIENT:
306                 out.println( indent+"MorphFillStyle (LINEAR_GRADIENT):" );
307                 out.println( indent+" "+matrix_start );
308                 out.println( indent+" "+matrix_end );
309                 gradient.printContent(out, indent+" ");
310                 break;
311             case FillStyle.RADIAL_GRADIENT:
312                 out.println( indent+"FillStyle (RADIAL_GRADIENT):" );
313                 out.println( indent+" "+matrix_start );
314                 out.println( indent+" "+matrix_end );
315                 gradient.printContent(out, indent+" ");
316                 break;
317             case FillStyle.TILED_BITMAP:
318                 out.println( indent+"FillStyle (TILED_BITMAP):" );
319                 out.println( indent+" "+matrix_start );
320                 out.println( indent+" "+matrix_end );
321                 out.println( indent+" bitmapID="+bitmap.getID() );
322                 break;
323             case FillStyle.CLIPPED_BITMAP:
324                 out.println( indent+"FillStyle (CLIPPED_BITMAP):" );
325                 out.println( indent+" "+matrix_start );
326                 out.println( indent+" "+matrix_end );
327                 out.println( indent+" bitmapID="+bitmap.getID() );
328                 break;
329         }
330     }
331
332     protected FlashItem copyInto( FlashItem item, ScriptCopier copier ) {
333         ((MorphFillStyle)item).type = type;
334         switch( type ) {
335             case FillStyle.SOLID:
336                 ((MorphFillStyle)item).color_start = (Color) color_start.getCopy(copier);
337                 ((MorphFillStyle)item).color_end = (Color) color_end.getCopy(copier);
338                 break;
339             case FillStyle.LINEAR_GRADIENT:
340             case FillStyle.RADIAL_GRADIENT:
341                 ((MorphFillStyle)item).gradient = (MorphGradient) gradient.getCopy(copier);
342                 ((MorphFillStyle)item).matrix_start = (AffineTransform) matrix_start.clone();
343                 ((MorphFillStyle)item).matrix_end = (AffineTransform) matrix_end.clone();
344                 break;
345             case FillStyle.TILED_BITMAP:
346             case FillStyle.CLIPPED_BITMAP:
347                 ((MorphFillStyle)item).bitmap = (Bitmap) copier.copy(bitmap);
348                 ((MorphFillStyle)item).matrix_start = (AffineTransform) matrix_start.clone();
349                 ((MorphFillStyle)item).matrix_end = (AffineTransform) matrix_end.clone();
350                 break;
351         }
352         return item;
353     }
354
355     public FlashItem getCopy( ScriptCopier copier ) {
356         return copyInto( new MorphFillStyle(), copier );
357     }
358 }
359
360
Popular Tags