1 7 package com.nightlabs.editor2d.impl; 8 9 import java.awt.Rectangle ; 10 import java.awt.geom.AffineTransform ; 11 import java.awt.image.AffineTransformOp ; 12 import java.awt.image.BufferedImage ; 13 import java.util.logging.Logger ; 14 15 import com.nightlabs.editor2d.ImageDrawComponent; 16 import com.nightlabs.editor2d.j2d.GeneralShape; 17 import com.nightlabs.editor2d.j2d.GeneralShapeFactory; 18 import com.nightlabs.editor2d.util.EditorModelUtil; 19 20 public class ImageDrawComponentImpl 21 extends DrawComponentImpl 22 implements ImageDrawComponent 23 { 24 26 34 protected static final BufferedImage IMAGE_EDEFAULT = null; 35 36 44 protected BufferedImage image = IMAGE_EDEFAULT; 45 46 51 public ImageDrawComponentImpl() { 52 super(); 53 } 54 55 60 public BufferedImage getImage() { 61 return image; 62 } 63 64 69 public void setImage(BufferedImage newImage) 70 { 71 BufferedImage oldImage = image; 72 primSetImage(newImage); 73 firePropertyChange(PROP_IMAGE, oldImage, image); 74 } 75 76 81 public GeneralShape getImageShape() { 82 return imageShape; 83 } 84 85 protected BufferedImage originalImage; 86 protected void primSetImage(BufferedImage newImage) 87 { 88 image = newImage; 89 originalImage = cloneImage(image); 90 bounds = null; 91 imageShape = GeneralShapeFactory.createRectangle(getBounds().x, getBounds().y, getBounds().width, getBounds().height); 92 } 93 94 99 public String toString() 100 { 101 StringBuffer result = new StringBuffer (super.toString()); 102 result.append(" (image: "); 103 result.append(image); 104 result.append(')'); 105 return result.toString(); 106 } 107 108 public Rectangle getBounds() 109 { 110 if (bounds == null) 111 { 112 if (image != null) 113 bounds = new Rectangle (x, y, image.getWidth(), image.getHeight()); 114 else 115 return super.getBounds(); 116 } 117 return bounds; 118 } 119 120 protected void primSetX(int x) 121 { 122 if (x == getBounds().x) 123 return; 124 125 bounds = null; 126 127 if (imageShape != null) { 128 atUtil.setToIdentity(); 129 atUtil.translate(x - getBounds().x, 0); 130 imageShape.transform(atUtil); 131 } 133 } 134 135 protected void primSetY(int y) 136 { 137 if (y == getBounds().y) 138 return; 139 140 bounds = null; 141 142 if (imageShape != null) { 143 atUtil.setToIdentity(); 144 atUtil.translate(0, y - getBounds().y); 145 imageShape.transform(atUtil); 146 } 148 } 149 150 protected void primSetLocation(int newX, int newY, boolean transformRotationCenter) 151 { 152 if (imageShape != null) 153 { 154 x = newX; 155 y = newY; 156 atUtil.setToIdentity(); 157 atUtil.translate((float)newX - (float)getX(), (float)newY - (float)getY()); 158 159 if (transformRotationCenter) 160 transformRotationCenter(atUtil); 161 162 imageShape.transform(atUtil); 163 } 164 bounds = null; 165 } 166 167 public void setLocation(int newX, int newY) 168 { 169 primSetLocation(newX, newY, true); 170 } 171 172 protected void setSize(int newWidth, int newHeight) 173 { 174 atUtil.setToIdentity(); 175 float ratioY = (float)newHeight / (float)getHeight(); 176 ratioY = EditorModelUtil.checkFactor(ratioY); 177 float ratioX = (float)newWidth / (float)getWidth(); 178 ratioX = EditorModelUtil.checkFactor(ratioX); 179 atUtil.scale(ratioX, ratioY); 180 181 transformRotationCenter(atUtil); 182 transform(atUtil); 183 } 184 185 protected void primSetRotation(double newRotation) 186 { 187 double oldRotation = rotation; 188 rotation = EditorModelUtil.getConstrainedValue(newRotation, rotationLimit); 190 191 if (oldRotation == newRotation) 192 return; 193 194 double degreesToRotate = EditorModelUtil.calcDiffRotation(rotation, oldRotation); 195 double degreesToRotateInRadians = Math.toRadians(degreesToRotate); 196 197 if (degreesToRotate != 0) 198 { 199 Rectangle imageShapeBounds = imageShape.getBounds(); 200 int sx = imageShapeBounds.width; 201 int sy = imageShapeBounds.height; 202 atUtil.setToIdentity(); 203 atUtil.rotate(degreesToRotateInRadians, getRotationX(), getRotationY()); 204 Rectangle shapeBounds = getTransformedShapeBounds(atUtil); 205 int dx = shapeBounds.width; 206 int dy = shapeBounds.height; 207 atUtil.setToIdentity(); 209 atUtil.setToTranslation(dx / 2, dy / 2); 210 atUtil.rotate(degreesToRotateInRadians); 211 atUtil.translate(-sx / 2, -sy / 2); 212 213 BufferedImage dest = new BufferedImage (dx, dy, BufferedImage.TYPE_INT_ARGB); 214 affineTransform.preConcatenate(atUtil); 215 AffineTransformOp op = new AffineTransformOp (affineTransform, interpolationType); 216 image = op.filter(originalImage, dest); 217 dest = null; 218 bounds = null; 219 220 227 int diffX = (dx - sx) / 2; 229 int diffY = (dy - sy) / 2; 230 231 238 primSetLocation(getX()-diffX, getY()-diffY, false); 239 240 } 245 } 246 247 protected AffineTransformOp op; 248 protected int interpolationType = AffineTransformOp.TYPE_BILINEAR; 249 public void transform(AffineTransform newAT) 250 { 251 affineTransform.preConcatenate(newAT); 252 op = new AffineTransformOp (affineTransform, interpolationType); 256 if (image != null) { 257 Rectangle shapeBounds = getTransformedShapeBounds(newAT); 259 BufferedImage bi = createCompatibleImage(affineTransform, shapeBounds.width, shapeBounds.height); 261 image = op.filter(originalImage, bi); 262 } 265 if (getParent() != null) 266 getParent().notifyChildTransform(this); 267 } 268 269 protected BufferedImage createCompatibleImage(AffineTransform at) 270 { 271 Rectangle shapeBounds = getTransformedShapeBounds(at); 272 BufferedImage dest = new BufferedImage (shapeBounds.width, shapeBounds.height, BufferedImage.TYPE_INT_ARGB); 273 return dest; 275 } 276 277 protected BufferedImage createCompatibleImage(AffineTransform at, int width, int height) 278 { 279 return new BufferedImage (width, height, BufferedImage.TYPE_INT_ARGB); 280 } 281 282 protected GeneralShape imageShape; 283 protected Rectangle getTransformedShapeBounds(AffineTransform at) 284 { 285 imageShape.transform(at); 286 return imageShape.getBounds(); 287 } 288 289 protected BufferedImage cloneImage(BufferedImage src) { 290 return src.getSubimage(0, 0, src.getWidth(), src.getHeight()); 291 } 292 293 public Class getRenderModeClass() { 294 return ImageDrawComponent.class; 295 } 296 297 public String getTypeName() { 301 return "Image"; 302 } 303 304 } | Popular Tags |