KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nightlabs > editor2d > impl > ImageDrawComponentImpl


1 /* *****************************************************************************
2  * NightLabs Editor2D - Graphical editor framework *
3  * Copyright (C) 2004-2005 NightLabs - http://NightLabs.org *
4  * *
5  * This library is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU Lesser General Public *
7  * License as published by the Free Software Foundation; either *
8  * version 2.1 of the License, or (at your option) any later version. *
9  * *
10  * This library is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
13  * Lesser General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU Lesser General Public *
16  * License along with this library; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 51 Franklin St, Fifth Floor, *
19  * Boston, MA 02110-1301 USA *
20  * *
21  * Or get it online : *
22  * http://www.gnu.org/copyleft/lesser.html *
23  * *
24  * *
25  ******************************************************************************/

26
27 package org.nightlabs.editor2d.impl;
28
29 import java.awt.Rectangle JavaDoc;
30 import java.awt.geom.AffineTransform JavaDoc;
31 import java.awt.image.AffineTransformOp JavaDoc;
32 import java.awt.image.BufferedImage JavaDoc;
33
34 import org.nightlabs.editor2d.DrawComponent;
35 import org.nightlabs.editor2d.DrawComponentContainer;
36 import org.nightlabs.editor2d.ImageDrawComponent;
37 import org.nightlabs.editor2d.j2d.GeneralShape;
38 import org.nightlabs.editor2d.j2d.GeneralShapeFactory;
39 import org.nightlabs.editor2d.util.EditorModelUtil;
40
41 public class ImageDrawComponentImpl
42 extends DrawComponentImpl
43 implements ImageDrawComponent
44 {
45 // public static final Logger LOGGER = Logger.getLogger(ImageDrawComponentImpl.class);
46

47   protected static final BufferedImage JavaDoc IMAGE_EDEFAULT = null;
48   protected BufferedImage JavaDoc image = IMAGE_EDEFAULT;
49
50   public ImageDrawComponentImpl() {
51         super();
52     }
53
54   /**
55    * @see ImageDrawComponent#getImage()
56    */

57   public BufferedImage JavaDoc getImage() {
58         return image;
59     }
60
61   /**
62    * sets the BufferedImage and fires a PropertyChange with the propertyName
63    * {@link ImageDrawComponent#PROP_IMAGE}
64    *
65    * @see ImageDrawComponent#setImage()
66    */

67   public void setImage(BufferedImage JavaDoc newImage)
68   {
69     BufferedImage JavaDoc oldImage = image;
70     primSetImage(newImage);
71     firePropertyChange(PROP_IMAGE, oldImage, image);
72   }
73
74   /**
75    * @see ImageDrawComponent#getImageShape()
76    */

77   public GeneralShape getImageShape() {
78     return imageShape;
79   }
80   
81   /**
82    * @see ImageDrawComponent#setImageShape(GeneralShape)
83    */

84   public void setImageShape(GeneralShape newImageShape) {
85     imageShape = newImageShape;
86   }
87
88   protected BufferedImage JavaDoc originalImage;
89   
90   /**
91    * @see ImageDrawComponent#getOriginalImage()
92    */

93   public BufferedImage JavaDoc getOriginalImage() {
94     return originalImage;
95   }
96   
97   protected void primSetImage(BufferedImage JavaDoc newImage)
98   {
99     image = newImage;
100     originalImage = cloneImage(image);
101     bounds = null;
102     imageShape = GeneralShapeFactory.createRectangle(getBounds().x, getBounds().y, getBounds().width, getBounds().height);
103   }
104   
105   public String JavaDoc toString()
106   {
107         StringBuffer JavaDoc result = new StringBuffer JavaDoc(super.toString());
108         result.append(" (image: ");
109         result.append(image);
110         result.append(')');
111         return result.toString();
112     }
113
114   public Rectangle JavaDoc getBounds()
115   {
116     if (bounds == null)
117     {
118       if (image != null)
119         bounds = new Rectangle JavaDoc(x, y, image.getWidth(), image.getHeight());
120       else
121         return super.getBounds();
122     }
123     return bounds;
124   }
125
126   protected void primSetX(int x)
127   {
128     if (x == getBounds().x)
129       return;
130     
131     bounds = null;
132         
133     if (imageShape != null) {
134       atUtil.setToIdentity();
135       atUtil.translate(x - getBounds().x, 0);
136       imageShape.transform(atUtil);
137 // super.primSetX(x);
138
}
139   }
140   
141   protected void primSetY(int y)
142   {
143     if (y == getBounds().y)
144       return;
145     
146     bounds = null;
147
148     if (imageShape != null) {
149       atUtil.setToIdentity();
150       atUtil.translate(0, y - getBounds().y);
151       imageShape.transform(atUtil);
152 // super.primSetY(y);
153
}
154   }
155     
156   protected void primSetLocation(int newX, int newY, boolean transformRotationCenter)
157   {
158     if (imageShape != null)
159     {
160       x = newX;
161       y = newY;
162       atUtil.setToIdentity();
163       atUtil.translate((float)newX - (float)getX(), (float)newY - (float)getY());
164       
165       if (transformRotationCenter)
166         transformRotationCenter(atUtil);
167       
168       imageShape.transform(atUtil);
169     }
170     bounds = null;
171   }
172   
173   public void setLocation(int newX, int newY)
174   {
175     primSetLocation(newX, newY, true);
176   }
177   
178   protected void setSize(int newWidth, int newHeight)
179   {
180     atUtil.setToIdentity();
181     float ratioY = (float)newHeight / (float)getHeight();
182     ratioY = EditorModelUtil.checkFactor(ratioY);
183     float ratioX = (float)newWidth / (float)getWidth();
184     ratioX = EditorModelUtil.checkFactor(ratioX);
185     atUtil.scale(ratioX, ratioY);
186     
187     transformRotationCenter(atUtil);
188     transform(atUtil);
189   }
190            
191 // protected void primSetRotation(double newRotation)
192
// {
193
// double oldRotation = rotation;
194
// rotation = EditorModelUtil.getConstrainedValue(newRotation, rotationLimit);
195
//
196
// if (oldRotation == newRotation)
197
// return;
198
//
199
// double degreesToRotate = EditorModelUtil.calcDiffRotation(rotation, oldRotation);
200
// double degreesToRotateInRadians = Math.toRadians(degreesToRotate);
201
//
202
// if (degreesToRotate != 0)
203
// {
204
// if (imageShape != null && originalImage != null)
205
// {
206
// Rectangle imageShapeBounds = imageShape.getBounds();
207
// int sx = imageShapeBounds.width;
208
// int sy = imageShapeBounds.height;
209
// atUtil.setToIdentity();
210
// atUtil.rotate(degreesToRotateInRadians, getRotationX(), getRotationY());
211
// Rectangle shapeBounds = getTransformedShapeBounds(atUtil);
212
// int dx = shapeBounds.width;
213
// int dy = shapeBounds.height;
214
// // reset the transform.
215
// atUtil.setToIdentity();
216
// atUtil.setToTranslation(dx / 2, dy / 2);
217
// atUtil.rotate(degreesToRotateInRadians);
218
// atUtil.translate(-sx / 2, -sy / 2);
219
//
220
// BufferedImage dest = new BufferedImage(dx, dy, BufferedImage.TYPE_INT_ARGB);
221
// affineTransform.preConcatenate(atUtil);
222
// AffineTransformOp op = new AffineTransformOp(affineTransform, interpolationType);
223
// image = op.filter(originalImage, dest);
224
// dest = null;
225
// bounds = null;
226
//
227
//// LOGGER.debug("oldImage width = "+sx);
228
//// LOGGER.debug("oldImage height = "+sy);
229
//// LOGGER.debug("shapeBounds width = "+shapeBounds.width);
230
//// LOGGER.debug("shapeBounds height = "+shapeBounds.height);
231
//// LOGGER.debug("newImage width = "+image.getWidth());
232
//// LOGGER.debug("newImage height = "+image.getHeight());
233
//
234
// // translate the rotated image to the center of the rotation
235
// int diffX = (dx - sx) / 2;
236
// int diffY = (dy - sy) / 2;
237
//
238
//// LOGGER.debug("oldX = "+getX());
239
//// LOGGER.debug("oldY = "+getY());
240
//// LOGGER.debug("shapeBounds.x = "+shapeBounds.x);
241
//// LOGGER.debug("shapeBounds.y = "+shapeBounds.y);
242
//// LOGGER.debug("getX()-diffX = "+(getX()-diffX));
243
//// LOGGER.debug("getY()-diffY = "+(getY()-diffY));
244
//
245
// primSetLocation(getX()-diffX, getY()-diffY, false);
246
//
247
//// LOGGER.debug("diffX = "+diffX);
248
//// LOGGER.debug("diffY = "+diffY);
249
//// LOGGER.debug("newX = "+getX());
250
//// LOGGER.debug("newY = "+getY());
251
// }
252
// }
253
// }
254

255   protected void primSetRotation(double newRotation)
256   {
257     double oldRotation = rotation;
258     rotation = EditorModelUtil.getConstrainedValue(newRotation, rotationLimit);
259     
260     if (oldRotation == newRotation)
261       return;
262               
263     double degreesToRotate = EditorModelUtil.calcDiffRotation(rotation, oldRotation);
264     double degreesToRotateInRadians = Math.toRadians(degreesToRotate);
265         
266     if (degreesToRotate != 0)
267     {
268         if (imageShape != null && originalImage != null)
269         {
270         Rectangle JavaDoc imageShapeBounds = imageShape.getBounds();
271         int sx = imageShapeBounds.width;
272         int sy = imageShapeBounds.height;
273         atUtil.setToIdentity();
274         atUtil.rotate(degreesToRotateInRadians, getRotationX(), getRotationY());
275         Rectangle JavaDoc shapeBounds = getTransformedShapeBounds(atUtil);
276         int dx = shapeBounds.width;
277         int dy = shapeBounds.height;
278         
279         // reset the transform.
280
atUtil.setToIdentity();
281         atUtil.setToTranslation(dx / 2, dy / 2);
282         atUtil.rotate(degreesToRotateInRadians);
283         atUtil.translate(-sx / 2, -sy / 2);
284               
285         BufferedImage JavaDoc dest = new BufferedImage JavaDoc(dx, dy, BufferedImage.TYPE_INT_ARGB);
286         affineTransform.preConcatenate(atUtil);
287         AffineTransformOp JavaDoc op = new AffineTransformOp JavaDoc(affineTransform, interpolationType);
288         image = op.filter(originalImage, dest);
289         dest = null;
290         bounds = null;
291                 
292         // translate the rotated image to the center of the rotation
293
int diffX = (dx - sx) / 2;
294         int diffY = (dy - sy) / 2;
295                 
296         primSetLocation(getX()-diffX, getY()-diffY, false);
297         }
298     }
299   }
300     
301   protected AffineTransformOp JavaDoc op;
302   protected int interpolationType = AffineTransformOp.TYPE_BILINEAR;
303   public void transform(AffineTransform JavaDoc newAT)
304   {
305     affineTransform.preConcatenate(newAT);
306     op = new AffineTransformOp JavaDoc(affineTransform, interpolationType);
307     if (image != null) {
308       Rectangle JavaDoc shapeBounds = getTransformedShapeBounds(newAT);
309       BufferedImage JavaDoc bi = createCompatibleImage(affineTransform, shapeBounds.width, shapeBounds.height);
310       image = op.filter(originalImage, bi);
311     }
312     if (getParent() != null)
313       getParent().notifyChildTransform(this);
314   }
315     
316 // protected BufferedImage createCompatibleImage(AffineTransform at)
317
// {
318
// Rectangle shapeBounds = getTransformedShapeBounds(at);
319
// BufferedImage dest = new BufferedImage(shapeBounds.width, shapeBounds.height, BufferedImage.TYPE_INT_ARGB);
320
//// LOGGER.debug("shapeBounds = "+shapeBounds);
321
// return dest;
322
// }
323

324   protected BufferedImage JavaDoc createCompatibleImage(AffineTransform JavaDoc at, int width, int height)
325   {
326     return new BufferedImage JavaDoc(width, height, BufferedImage.TYPE_INT_ARGB);
327   }
328       
329   protected GeneralShape imageShape;
330   protected Rectangle JavaDoc getTransformedShapeBounds(AffineTransform JavaDoc at)
331   {
332     imageShape.transform(at);
333     return imageShape.getBounds();
334   }
335   
336   protected BufferedImage JavaDoc cloneImage(BufferedImage JavaDoc src) {
337     return src.getSubimage(0, 0, src.getWidth(), src.getHeight());
338   }
339   
340   public Class JavaDoc getRenderModeClass() {
341     return ImageDrawComponent.class;
342   }
343   
344   public String JavaDoc getTypeName() {
345     return "Image";
346   }
347   
348 // public Object clone()
349
// {
350
// ImageDrawComponentImpl imageDC = (ImageDrawComponentImpl) super.clone();
351
// imageDC.image = cloneImage(this.image);
352
// imageDC.imageShape = (GeneralShape) this.imageShape.clone();
353
// imageDC.op = new AffineTransformOp(op.getTransform(), interpolationType);
354
// imageDC.originalImage = cloneImage(this.originalImage);
355
// return imageDC;
356
// }
357

358   public Object JavaDoc clone(DrawComponentContainer parent)
359   {
360     ImageDrawComponentImpl imageDC = (ImageDrawComponentImpl) super.clone(parent);
361     imageDC.image = cloneImage(this.image);
362     imageDC.imageShape = (GeneralShape) this.imageShape.clone();
363     imageDC.op = new AffineTransformOp JavaDoc(op.getTransform(), interpolationType);
364     imageDC.originalImage = cloneImage(this.originalImage);
365     imageDC.interpolationType = interpolationType;
366     return imageDC;
367   }
368     
369 // public DrawComponent clone()
370
// {
371
// ImageDrawComponent image = new ImageDrawComponentImpl();
372
// image = (ImageDrawComponent) assign(image);
373
// return image;
374
// }
375
//
376
// protected DrawComponent assign(DrawComponent dc)
377
// {
378
// super.assign(dc);
379
// if (dc instanceof ImageDrawComponent) {
380
// ImageDrawComponent image = (ImageDrawComponent) dc;
381
// image.setImage(cloneImage(getImage()));
382
// }
383
// return dc;
384
// }
385
} //ImageDrawComponentImpl
386
Popular Tags