[919] | 1 | /**
|
---|
| 2 | * Picture.java
|
---|
| 3 | * Copyright (C) 2010 New Zealand Digital Library, http://expeditee.org
|
---|
| 4 | *
|
---|
| 5 | * This program is free software: you can redistribute it and/or modify
|
---|
| 6 | * it under the terms of the GNU General Public License as published by
|
---|
| 7 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 8 | * (at your option) any later version.
|
---|
| 9 | *
|
---|
| 10 | * This program 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
|
---|
| 13 | * GNU General Public License for more details.
|
---|
| 14 | *
|
---|
| 15 | * You should have received a copy of the GNU General Public License
|
---|
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 17 | */
|
---|
| 18 |
|
---|
[4] | 19 | package org.expeditee.items;
|
---|
| 20 |
|
---|
| 21 | import java.awt.AlphaComposite;
|
---|
[214] | 22 | import java.awt.BasicStroke;
|
---|
[242] | 23 | import java.awt.Color;
|
---|
[4] | 24 | import java.awt.Graphics2D;
|
---|
| 25 | import java.awt.Image;
|
---|
| 26 | import java.awt.Point;
|
---|
| 27 | import java.awt.Polygon;
|
---|
| 28 | import java.awt.Rectangle;
|
---|
| 29 | import java.awt.Shape;
|
---|
[214] | 30 | import java.awt.Stroke;
|
---|
[226] | 31 | import java.awt.Toolkit;
|
---|
[653] | 32 | import java.awt.geom.AffineTransform;
|
---|
[108] | 33 | import java.awt.geom.Point2D;
|
---|
[653] | 34 | import java.awt.image.BufferedImage;
|
---|
[226] | 35 | import java.awt.image.CropImageFilter;
|
---|
| 36 | import java.awt.image.FilteredImageSource;
|
---|
[4] | 37 | import java.awt.image.ImageObserver;
|
---|
| 38 | import java.io.File;
|
---|
[601] | 39 | import java.io.IOException;
|
---|
| 40 | import java.text.DecimalFormat;
|
---|
[4] | 41 |
|
---|
| 42 | import javax.imageio.ImageIO;
|
---|
| 43 | import javax.swing.ImageIcon;
|
---|
| 44 |
|
---|
[88] | 45 | import org.expeditee.gui.FrameGraphics;
|
---|
[843] | 46 | import org.expeditee.gui.FrameIO;
|
---|
[108] | 47 | import org.expeditee.gui.FrameMouseActions;
|
---|
[843] | 48 | import org.expeditee.gui.FrameUtils;
|
---|
[601] | 49 | import org.expeditee.stats.Logger;
|
---|
[4] | 50 |
|
---|
| 51 | /**
|
---|
| 52 | * This class represents an Image loaded from a file which is shown on the
|
---|
| 53 | * screen. Loading of the Image from disk occurs in the constructor, and takes
|
---|
| 54 | * approximately one second per mb of the Image file size. <br>
|
---|
| 55 | * <br>
|
---|
| 56 | * Currently Supported (Tested) Image formats:<br>
|
---|
| 57 | * BMP<br>
|
---|
| 58 | * JPG<br>
|
---|
| 59 | * GIF<br>
|
---|
| 60 | * GIF (Animated)<br>
|
---|
| 61 | * <br>
|
---|
| 62 | * Currently only the default size of the Image is supported, but future
|
---|
| 63 | * versions may support scaling.
|
---|
| 64 | *
|
---|
| 65 | * @author jdm18
|
---|
| 66 | *
|
---|
| 67 | */
|
---|
[601] | 68 | public class Picture extends XRayable {
|
---|
[4] | 69 |
|
---|
[133] | 70 | private static final int MINIMUM_WIDTH = 10;
|
---|
[601] | 71 |
|
---|
[4] | 72 | public static final int WIDTH = 0;
|
---|
[601] | 73 |
|
---|
[4] | 74 | public static final int RATIO = 1;
|
---|
| 75 |
|
---|
[78] | 76 | protected Image _image = null;
|
---|
[4] | 77 |
|
---|
| 78 | private int _scaleType = RATIO;
|
---|
| 79 |
|
---|
| 80 | private float _scale = 1.0f;
|
---|
| 81 |
|
---|
[601] | 82 | // Start of the crop relative to START
|
---|
[4] | 83 | private Point _cropStart = null;
|
---|
[601] | 84 |
|
---|
| 85 | // Start of the crop relative to END
|
---|
[4] | 86 | private Point _cropEnd = null;
|
---|
| 87 |
|
---|
[376] | 88 | private Point _start = new Point(0, 0);
|
---|
[601] | 89 |
|
---|
[376] | 90 | private Point _end = new Point(0, 0);
|
---|
[643] | 91 |
|
---|
[653] | 92 | private double _rotate = 0;
|
---|
| 93 |
|
---|
[643] | 94 | private boolean _flipX = false;
|
---|
| 95 | private boolean _flipY = false;
|
---|
[80] | 96 |
|
---|
[4] | 97 | private boolean _showCropping = false;
|
---|
| 98 |
|
---|
[147] | 99 | private String _path = "";
|
---|
[226] | 100 |
|
---|
[601] | 101 | private String _size = "";
|
---|
| 102 |
|
---|
| 103 | private String _fileName = null;
|
---|
| 104 |
|
---|
[4] | 105 | // used to repaint animated GIF images, among other things.
|
---|
[78] | 106 | protected ImageObserver _imageObserver = null;
|
---|
[4] | 107 |
|
---|
[601] | 108 | protected Picture(Text source, ImageObserver observer, Image image) {
|
---|
| 109 | super(source);
|
---|
| 110 | _imageObserver = observer;
|
---|
| 111 | _image = image;
|
---|
| 112 |
|
---|
| 113 | refresh();
|
---|
| 114 |
|
---|
| 115 | if (_image != null)
|
---|
| 116 | parseSize();
|
---|
| 117 | }
|
---|
| 118 |
|
---|
[4] | 119 | /**
|
---|
| 120 | * Creates a new Picture from the given path. The ImageObserver is optional
|
---|
| 121 | * and can be set to NULL. <br>
|
---|
| 122 | * Note: It is assumed that the file described in path has already been
|
---|
| 123 | * checked to exist.
|
---|
| 124 | *
|
---|
[601] | 125 | * @param source
|
---|
| 126 | * The Text Item that was used to create this Picture
|
---|
| 127 | * @param fileName
|
---|
| 128 | * the name of the file as it should be displayed in the source
|
---|
| 129 | * text
|
---|
[4] | 130 | * @param path
|
---|
| 131 | * The Path of the Image to load from disk.
|
---|
| 132 | * @param observer
|
---|
| 133 | * The ImageObserver to assign when painting the Image on the
|
---|
| 134 | * screen.
|
---|
| 135 | */
|
---|
[601] | 136 | public Picture(Text source, String fileName, String path, String size,
|
---|
| 137 | ImageObserver observer) {
|
---|
| 138 | super(source);
|
---|
[147] | 139 | _imageObserver = observer;
|
---|
[601] | 140 | _fileName = fileName;
|
---|
| 141 | _path = path;
|
---|
| 142 | _size = size;
|
---|
| 143 |
|
---|
| 144 | refresh();
|
---|
| 145 | parseSize();
|
---|
[78] | 146 | }
|
---|
[601] | 147 |
|
---|
| 148 | protected String getImageSize() {
|
---|
| 149 | return _size;
|
---|
[147] | 150 | }
|
---|
[124] | 151 |
|
---|
[601] | 152 | protected void parseSize() {
|
---|
| 153 | String size = getImageSize();
|
---|
| 154 |
|
---|
| 155 | if (_end.x != 0 || _end.y != 0)
|
---|
| 156 | return;
|
---|
| 157 |
|
---|
[80] | 158 | // set the default values for start and end
|
---|
[108] | 159 | _start.setLocation(0, 0);
|
---|
| 160 | if (_image == null)
|
---|
| 161 | _end.setLocation(0, 0);
|
---|
| 162 | else
|
---|
| 163 | _end.setLocation(_image.getWidth(null), _image.getHeight(null));
|
---|
[80] | 164 | size = size.trim();
|
---|
[653] | 165 | String sizeLower = size.toLowerCase();
|
---|
[80] | 166 | String[] values = size.split("\\s+");
|
---|
| 167 | // Now get the cropping values if there are any
|
---|
[4] | 168 | try {
|
---|
[80] | 169 | if (values.length > 2) {
|
---|
| 170 | int startX = Integer.parseInt(values[1]);
|
---|
| 171 | int startY = Integer.parseInt(values[2]);
|
---|
| 172 | _start = new Point(startX, startY);
|
---|
| 173 | if (values.length > 4) {
|
---|
| 174 | int endX = Integer.parseInt(values[3]);
|
---|
| 175 | int endY = Integer.parseInt(values[4]);
|
---|
| 176 | _end = new Point(endX, endY);
|
---|
| 177 | }
|
---|
[609] | 178 | scaleCrop();
|
---|
[80] | 179 | }
|
---|
| 180 | } catch (Exception e) {
|
---|
| 181 | }
|
---|
[643] | 182 |
|
---|
[653] | 183 | if(sizeLower.contains("flipx")) {
|
---|
[643] | 184 | _flipX = true;
|
---|
| 185 | }
|
---|
| 186 |
|
---|
[653] | 187 | if(sizeLower.contains("flipy")) {
|
---|
[643] | 188 | _flipY = true;
|
---|
| 189 | }
|
---|
[653] | 190 |
|
---|
| 191 | int index = sizeLower.indexOf("rotation=");
|
---|
| 192 | if(index != -1) {
|
---|
| 193 | int tmp = sizeLower.indexOf(" ", index);
|
---|
| 194 | String rotation;
|
---|
| 195 | if(tmp == -1) {
|
---|
| 196 | rotation = sizeLower.substring(index + "rotation=".length());
|
---|
| 197 | } else {
|
---|
| 198 | rotation = sizeLower.substring(index + "rotation=".length(), index + tmp);
|
---|
| 199 | }
|
---|
| 200 | _rotate = Double.parseDouble(rotation);
|
---|
| 201 | }
|
---|
[80] | 202 |
|
---|
| 203 | try {
|
---|
[4] | 204 | if (size.length() == 0) {
|
---|
| 205 | size = "" + _image.getWidth(null);
|
---|
[601] | 206 | _source.setText(getTagText() + size);
|
---|
[80] | 207 | return;
|
---|
[4] | 208 | }
|
---|
[80] | 209 | size = values[0];
|
---|
| 210 | // parse width or ratio from text
|
---|
| 211 | if (size.contains(".")) {
|
---|
[4] | 212 | // this is a ratio
|
---|
[80] | 213 | _scale = Float.parseFloat(size);
|
---|
[4] | 214 | _scaleType = RATIO;
|
---|
| 215 | } else if (size.length() > 0) {
|
---|
| 216 | // this is an absolute width
|
---|
[80] | 217 | int width = Integer.parseInt(size);
|
---|
[4] | 218 | _scaleType = WIDTH;
|
---|
[80] | 219 | setWidth(width);
|
---|
[4] | 220 | }
|
---|
| 221 | } catch (Exception e) {
|
---|
[80] | 222 | _scale = 1F;
|
---|
[4] | 223 | }
|
---|
| 224 | }
|
---|
| 225 |
|
---|
| 226 | public void setStartCrop(int x, int y) {
|
---|
[124] | 227 | invalidateCroppedArea();
|
---|
[4] | 228 | _cropStart = new Point(x - getX(), y - getY());
|
---|
[124] | 229 | invalidateCroppedArea();
|
---|
[4] | 230 | }
|
---|
| 231 |
|
---|
| 232 | public void setEndCrop(int x, int y) {
|
---|
[124] | 233 | invalidateCroppedArea();
|
---|
[4] | 234 | _cropEnd = new Point(x - getX(), y - getY());
|
---|
[124] | 235 | invalidateCroppedArea();
|
---|
[4] | 236 | }
|
---|
| 237 |
|
---|
[124] | 238 | private void invalidateCroppedArea() {
|
---|
| 239 | if (_cropStart != null && _cropEnd != null) {
|
---|
| 240 | Point topLeft = getTopLeftCrop();
|
---|
| 241 | Point bottomRight = getBottomRightCrop();
|
---|
| 242 | int startX = getX() + topLeft.x - _highlightThickness;
|
---|
| 243 | int startY = getY() + topLeft.y - _highlightThickness;
|
---|
| 244 | int border = 2 * _highlightThickness;
|
---|
| 245 | invalidate(new Rectangle(startX, startY, bottomRight.x - topLeft.x
|
---|
| 246 | + 2 * border, bottomRight.y - topLeft.y + 2 * border));
|
---|
| 247 | invalidateAll();
|
---|
| 248 | } else {
|
---|
| 249 | invalidateAll();
|
---|
| 250 | }
|
---|
| 251 | }
|
---|
| 252 |
|
---|
[80] | 253 | public Point getTopLeftCrop() {
|
---|
| 254 | return new Point(Math.min(_cropStart.x, _cropEnd.x), Math.min(
|
---|
| 255 | _cropStart.y, _cropEnd.y));
|
---|
| 256 | }
|
---|
| 257 |
|
---|
| 258 | public Point getBottomRightCrop() {
|
---|
| 259 | return new Point(Math.max(_cropStart.x, _cropEnd.x), Math.max(
|
---|
| 260 | _cropStart.y, _cropEnd.y));
|
---|
| 261 | }
|
---|
| 262 |
|
---|
[4] | 263 | public void setShowCrop(boolean value) {
|
---|
[130] | 264 | // invalidateCroppedArea();
|
---|
[4] | 265 | _showCropping = value;
|
---|
[124] | 266 | invalidateCroppedArea();
|
---|
[4] | 267 | }
|
---|
| 268 |
|
---|
[80] | 269 | public boolean isCropTooSmall() {
|
---|
[601] | 270 | if (_cropStart == null || _cropEnd == null)
|
---|
[80] | 271 | return true;
|
---|
[4] | 272 |
|
---|
[80] | 273 | int cropWidth = Math.abs(_cropEnd.x - _cropStart.x);
|
---|
| 274 | int cropHeight = Math.abs(_cropEnd.y - _cropStart.y);
|
---|
| 275 |
|
---|
| 276 | return cropWidth < MINIMUM_WIDTH || cropHeight < MINIMUM_WIDTH;
|
---|
[4] | 277 | }
|
---|
| 278 |
|
---|
| 279 | public void clearCropping() {
|
---|
[124] | 280 | invalidateCroppedArea();
|
---|
[4] | 281 | _cropStart = null;
|
---|
| 282 | _cropEnd = null;
|
---|
| 283 | setShowCrop(false);
|
---|
| 284 | }
|
---|
| 285 |
|
---|
[115] | 286 | public void updatePolygon() {
|
---|
[108] | 287 | if (_image == null) {
|
---|
| 288 | refresh();
|
---|
[601] | 289 | parseSize();
|
---|
[108] | 290 | }
|
---|
[653] | 291 |
|
---|
| 292 | Point[] ori = new Point[4];
|
---|
| 293 | Point2D[] rot = new Point2D[4];
|
---|
| 294 | Point centre = new Point();
|
---|
[4] | 295 |
|
---|
| 296 | if (_cropStart == null || _cropEnd == null) {
|
---|
[80] | 297 | int width = getWidth();
|
---|
| 298 | int height = getHeight();
|
---|
[653] | 299 |
|
---|
| 300 | centre.x = _source.getX() + width / 2;
|
---|
| 301 | centre.y = _source.getY() + height / 2;
|
---|
[4] | 302 |
|
---|
[108] | 303 | int xdiff = -MARGIN_RIGHT; // -getLeftMargin();
|
---|
[4] | 304 |
|
---|
| 305 | // extra pixel around the image so the highlighting is visible
|
---|
[653] | 306 | // _poly.addPoint(_source.getX() + 1 + xdiff, _source.getY() - 1);
|
---|
| 307 | // _poly.addPoint(_source.getX() + width, _source.getY() - 1);
|
---|
| 308 | // _poly.addPoint(_source.getX() + width, _source.getY() + height);
|
---|
| 309 | // _poly.addPoint(_source.getX() + 1 + xdiff, _source.getY() + height);
|
---|
| 310 |
|
---|
| 311 | ori[0] = new Point(_source.getX() + 1 + xdiff, _source.getY() - 1);
|
---|
| 312 | ori[1] = new Point(_source.getX() + width, _source.getY() - 1);
|
---|
| 313 | ori[2] = new Point(_source.getX() + width, _source.getY() + height);
|
---|
| 314 | ori[3] = new Point(_source.getX() + 1 + xdiff, _source.getY() + height);
|
---|
| 315 |
|
---|
[4] | 316 | } else {
|
---|
[80] | 317 | Point topLeft = getTopLeftCrop();
|
---|
| 318 | Point bottomRight = getBottomRightCrop();
|
---|
[653] | 319 |
|
---|
| 320 | centre.x = _source.getX() + (bottomRight.x - topLeft.x) / 2;
|
---|
| 321 | centre.y = _source.getY() + (bottomRight.y - topLeft.y) / 2;
|
---|
| 322 |
|
---|
[601] | 323 | Rectangle clip = new Rectangle(topLeft.x + _source.getX(),
|
---|
| 324 | topLeft.y + _source.getY(), bottomRight.x - topLeft.x,
|
---|
[80] | 325 | bottomRight.y - topLeft.y).getBounds();
|
---|
[653] | 326 | // _poly.addPoint((int) clip.getMinX() - 1, (int) clip.getMinY() - 1);
|
---|
| 327 | // _poly.addPoint((int) clip.getMinX() - 1, (int) clip.getMaxY());
|
---|
| 328 | // _poly.addPoint((int) clip.getMaxX(), (int) clip.getMaxY());
|
---|
| 329 | // _poly.addPoint((int) clip.getMaxX(), (int) clip.getMinY() - 1);
|
---|
| 330 |
|
---|
| 331 | ori[0] = new Point((int) clip.getMinX() - 1, (int) clip.getMinY() - 1);
|
---|
| 332 | ori[1] = new Point((int) clip.getMinX() - 1, (int) clip.getMaxY());
|
---|
| 333 | ori[2] = new Point((int) clip.getMaxX(), (int) clip.getMaxY());
|
---|
| 334 | ori[3] = new Point((int) clip.getMaxX(), (int) clip.getMinY() - 1);
|
---|
[4] | 335 |
|
---|
| 336 | }
|
---|
[653] | 337 |
|
---|
[726] | 338 | AffineTransform.getRotateInstance(Math.PI * _rotate / 180, centre.x, centre.y).transform(ori, 0, rot, 0, 4);
|
---|
[653] | 339 |
|
---|
| 340 | _poly = new Polygon();
|
---|
| 341 | for(Point2D p : rot) {
|
---|
| 342 | _poly.addPoint((int)p.getX(), (int)p.getY());
|
---|
| 343 | }
|
---|
[4] | 344 | }
|
---|
| 345 |
|
---|
| 346 | @Override
|
---|
[108] | 347 | public double getEnclosedArea() {
|
---|
| 348 | return getWidth() * getHeight();
|
---|
[4] | 349 | }
|
---|
| 350 |
|
---|
| 351 | @Override
|
---|
[400] | 352 | public void setWidth(Integer width) {
|
---|
[601] | 353 | _scale = width * 1F / (_end.x - _start.x);
|
---|
[80] | 354 | }
|
---|
| 355 |
|
---|
[376] | 356 | public Point getStart() {
|
---|
| 357 | return _start;
|
---|
| 358 | }
|
---|
| 359 |
|
---|
| 360 | public Point getEnd() {
|
---|
| 361 | return _end;
|
---|
| 362 | }
|
---|
| 363 |
|
---|
[80] | 364 | /**
|
---|
| 365 | * Gets the width with which the picture is displayed on the screen.
|
---|
| 366 | */
|
---|
| 367 | @Override
|
---|
[400] | 368 | public Integer getWidth() {
|
---|
[609] | 369 | return Math.round(getUnscaledWidth() * _scale);
|
---|
[80] | 370 | }
|
---|
| 371 |
|
---|
| 372 | /**
|
---|
| 373 | * Gets the height with which the picture is displayed on the screen.
|
---|
| 374 | */
|
---|
[282] | 375 | @Override
|
---|
[80] | 376 | public int getHeight() {
|
---|
[609] | 377 | return Math.round(getUnscaledHeight() * _scale);
|
---|
[80] | 378 | }
|
---|
| 379 |
|
---|
[88] | 380 | /**
|
---|
| 381 | * Dont paint links in audience mode for images.
|
---|
| 382 | */
|
---|
[80] | 383 | @Override
|
---|
[108] | 384 | protected void paintLink(Graphics2D g) {
|
---|
[88] | 385 | if (FrameGraphics.isAudienceMode())
|
---|
| 386 | return;
|
---|
| 387 | super.paintLink(g);
|
---|
| 388 | }
|
---|
[607] | 389 |
|
---|
[793] | 390 | public void paintImageTiling(Graphics2D g) {
|
---|
[607] | 391 | if (_image == null) {
|
---|
| 392 | return;
|
---|
| 393 | }
|
---|
| 394 |
|
---|
| 395 | int iw = _image.getWidth(null);
|
---|
| 396 | int ih = _image.getHeight(null);
|
---|
| 397 | if(iw <= 0 || ih <= 0) {
|
---|
| 398 | return;
|
---|
| 399 | }
|
---|
| 400 |
|
---|
[609] | 401 | int dX1 = _source.getX();
|
---|
| 402 | int dY1 = _source.getY();
|
---|
| 403 | int dX2 = _source.getX() + getWidth();
|
---|
| 404 | int dY2 = _source.getY() + getHeight();
|
---|
| 405 |
|
---|
[654] | 406 | BufferedImage tmp = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
|
---|
[653] | 407 | Graphics2D g2d = tmp.createGraphics();
|
---|
| 408 | int offX = (tmp.getWidth() - getWidth()) / 2;
|
---|
| 409 | int offY = (tmp.getHeight() - getHeight()) / 2;
|
---|
[610] | 410 |
|
---|
[653] | 411 | // g2d.rotate(rotate, tmp.getWidth() / 2, tmp.getHeight() / 2);
|
---|
| 412 |
|
---|
[610] | 413 | int cropStartX = _start.x;
|
---|
| 414 | int cropEndX = _end.x;
|
---|
| 415 | if(cropEndX > iw) {
|
---|
| 416 | cropEndX = iw;
|
---|
| 417 | }
|
---|
| 418 | for(int x = dX1; x < dX2; ) {
|
---|
| 419 | // end - start = (cropEnd - cropStart) * scale
|
---|
| 420 | // => cropEnd = cropStart + (end - start) / scale
|
---|
| 421 | int w = (int) ((cropEndX - cropStartX) * _scale);
|
---|
| 422 | int endX = x + w;
|
---|
| 423 | if(endX > dX2) {
|
---|
| 424 | endX = dX2;
|
---|
| 425 | cropEndX = cropStartX + (int) ((dX2 - x) / _scale);
|
---|
| 426 | }
|
---|
| 427 |
|
---|
| 428 | int cropStartY = _start.y;
|
---|
| 429 | int cropEndY = _end.y;
|
---|
| 430 | if(cropEndY > ih) {
|
---|
| 431 | cropEndY = ih;
|
---|
| 432 | }
|
---|
| 433 | for(int y = dY1; y < dY2; ) {
|
---|
| 434 | int h = (int) ((cropEndY - cropStartY) * _scale);
|
---|
| 435 | int endY = y + h;
|
---|
[607] | 436 | if(endY > dY2) {
|
---|
| 437 | endY = dY2;
|
---|
[610] | 438 | cropEndY = cropStartY + (int) ((dY2 - y) / _scale);
|
---|
[607] | 439 | }
|
---|
[610] | 440 |
|
---|
[643] | 441 | int sx = _flipX ? cropEndX : cropStartX;
|
---|
| 442 | int ex = _flipX ? cropStartX : cropEndX;
|
---|
| 443 | int sy = _flipY ? cropEndY : cropStartY;
|
---|
| 444 | int ey = _flipY ? cropStartY : cropEndY;
|
---|
[653] | 445 | g2d.drawImage(_image, x - dX1 + offX, y - dY1 + offY, endX - dX1 + offX, endY - dY1 + offY, sx, sy, ex, ey, null);
|
---|
[643] | 446 |
|
---|
[610] | 447 | cropStartY = 0;
|
---|
| 448 | cropEndY = ih;
|
---|
| 449 |
|
---|
| 450 | y = endY;
|
---|
[607] | 451 | }
|
---|
[610] | 452 |
|
---|
| 453 | cropStartX = 0;
|
---|
| 454 | cropEndX = iw;
|
---|
| 455 |
|
---|
| 456 | x = endX;
|
---|
[607] | 457 | }
|
---|
[653] | 458 |
|
---|
[793] | 459 | AffineTransform at = new AffineTransform();
|
---|
| 460 | at.translate(dX1, dY1);
|
---|
| 461 | at.rotate(Math.PI * _rotate / 180, tmp.getWidth() / 2, tmp.getHeight() / 2);
|
---|
| 462 | g.drawImage(tmp, at, _imageObserver);
|
---|
| 463 | // g.drawImage(tmp, dX1, dY1, dX2, dY2, 0, 0, tmp.getWidth(), tmp.getHeight(), _imageObserver);
|
---|
[607] | 464 | }
|
---|
[108] | 465 |
|
---|
[88] | 466 | @Override
|
---|
[4] | 467 | public void paint(Graphics2D g) {
|
---|
[601] | 468 | if (_image == null)
|
---|
[597] | 469 | return;
|
---|
[4] | 470 |
|
---|
[78] | 471 | paintLink(g);
|
---|
[4] | 472 |
|
---|
| 473 | // if we are showing the cropping, then show the original as transparent
|
---|
[80] | 474 | if (_showCropping && !isCropTooSmall()) {
|
---|
[4] | 475 | // show the full image as transparent
|
---|
| 476 | float alpha = .5f;
|
---|
| 477 | g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
|
---|
| 478 | alpha));
|
---|
[607] | 479 |
|
---|
| 480 | paintImageTiling(g);
|
---|
[4] | 481 |
|
---|
| 482 | g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
|
---|
| 483 | 1.0f));
|
---|
| 484 | // show the cropped area normally
|
---|
[80] | 485 | Point topLeft = getTopLeftCrop();
|
---|
| 486 | Point bottomRight = getBottomRightCrop();
|
---|
[607] | 487 | Shape clip = new Rectangle(_source.getX() + topLeft.x, _source.getY() + topLeft.y,
|
---|
[80] | 488 | bottomRight.x - topLeft.x, bottomRight.y - topLeft.y);
|
---|
[4] | 489 | g.setColor(getPaintHighlightColor());
|
---|
| 490 | g.draw(clip);
|
---|
| 491 | g.setClip(clip);
|
---|
| 492 |
|
---|
[607] | 493 | paintImageTiling(g);
|
---|
| 494 |
|
---|
[4] | 495 | g.draw(clip);
|
---|
| 496 | // if the image is cropped, but we are not showing the cropping
|
---|
| 497 | // otherwise, paint normally
|
---|
| 498 | } else {
|
---|
[607] | 499 | paintImageTiling(g);
|
---|
[4] | 500 | }
|
---|
[80] | 501 |
|
---|
[242] | 502 | if (hasVisibleBorder()) {
|
---|
| 503 | g.setColor(getPaintBorderColor());
|
---|
[226] | 504 | Stroke borderStroke = new BasicStroke(getThickness(), CAP, JOIN);
|
---|
[214] | 505 | g.setStroke(borderStroke);
|
---|
| 506 | g.drawPolygon(getPolygon());
|
---|
[80] | 507 | }
|
---|
[376] | 508 |
|
---|
[242] | 509 | if (isHighlighted()) {
|
---|
| 510 | Stroke borderStroke = new BasicStroke(1, CAP, JOIN);
|
---|
| 511 | g.setStroke(borderStroke);
|
---|
| 512 | g.setColor(getHighlightColor());
|
---|
| 513 | g.drawPolygon(getPolygon());
|
---|
| 514 | }
|
---|
[376] | 515 |
|
---|
| 516 | //System.out.print("p_");
|
---|
[4] | 517 | }
|
---|
[376] | 518 |
|
---|
[242] | 519 | @Override
|
---|
| 520 | public Color getHighlightColor() {
|
---|
| 521 | if (_highlightColor.equals(getBorderColor()))
|
---|
| 522 | return ALTERNATE_HIGHLIGHT;
|
---|
| 523 | return _highlightColor;
|
---|
| 524 | }
|
---|
[4] | 525 |
|
---|
| 526 | @Override
|
---|
[115] | 527 | public int setHighlightColor() {
|
---|
| 528 | super.setHighlightColor();
|
---|
[4] | 529 |
|
---|
| 530 | return Item.DEFAULT_CURSOR;
|
---|
| 531 | }
|
---|
| 532 |
|
---|
[78] | 533 | protected Picture createPicture() {
|
---|
[601] | 534 | return ItemUtils.CreatePicture((Text) _source.copy(), _imageObserver);
|
---|
[78] | 535 | }
|
---|
| 536 |
|
---|
[4] | 537 | @Override
|
---|
| 538 | public Picture copy() {
|
---|
[78] | 539 | Picture p = createPicture();
|
---|
[80] | 540 | p._image = _image;
|
---|
[108] | 541 | p._mode = _mode;
|
---|
| 542 | // Doing Duplicate item duplicates link mark which we dont want to do
|
---|
| 543 | // when in audience mode because the linkMark will be copied incorrectly
|
---|
| 544 | // Get all properties from the source
|
---|
[4] | 545 |
|
---|
[97] | 546 | if (!isCropTooSmall() && _cropStart != null && _cropEnd != null) {
|
---|
[80] | 547 | assert (_cropEnd != null);
|
---|
| 548 | // make the start be the top left
|
---|
| 549 | // make the end be the bottom right
|
---|
| 550 | Point topLeft = getTopLeftCrop();
|
---|
| 551 | Point bottomRight = getBottomRightCrop();
|
---|
[601] | 552 | int startX = Math.round(topLeft.x / _scale) + _start.x;
|
---|
| 553 | int startY = Math.round(topLeft.y / _scale) + _start.y;
|
---|
| 554 | int endX = Math.round(bottomRight.x / _scale + _start.x);
|
---|
| 555 | int endY = Math.round(bottomRight.y / _scale + _start.y);
|
---|
[80] | 556 | int width = _image.getWidth(null);
|
---|
| 557 | int height = _image.getHeight(null);
|
---|
| 558 | // adjust our start and end if the user has dragged outside of the
|
---|
| 559 | // shape
|
---|
| 560 | if (endX > width) {
|
---|
| 561 | endX = width;
|
---|
| 562 | }
|
---|
| 563 | if (endY > height) {
|
---|
| 564 | endY = height;
|
---|
| 565 | }
|
---|
| 566 | if (startX < 0) {
|
---|
| 567 | startX = 0;
|
---|
| 568 | }
|
---|
| 569 | if (startY < 0) {
|
---|
| 570 | startY = 0;
|
---|
| 571 | }
|
---|
| 572 | p._start = new Point(startX, startY);
|
---|
| 573 | p._end = new Point(endX, endY);
|
---|
[601] | 574 | p._source.setPosition(topLeft.x + _source.getX(), topLeft.y
|
---|
| 575 | + _source.getY());
|
---|
[80] | 576 | } else {
|
---|
[609] | 577 | p._start = new Point(_start);
|
---|
| 578 | p._end = new Point(_end);
|
---|
[80] | 579 | }
|
---|
| 580 | p._scale = _scale;
|
---|
| 581 | p._scaleType = _scaleType;
|
---|
| 582 | p._path = _path;
|
---|
[601] | 583 | p._fileName = _fileName;
|
---|
[4] | 584 |
|
---|
[601] | 585 | p.updateSource();
|
---|
[80] | 586 | p.updatePolygon();
|
---|
[4] | 587 |
|
---|
| 588 | return p;
|
---|
| 589 | }
|
---|
| 590 |
|
---|
[108] | 591 | public float getScale() {
|
---|
| 592 | return _scale;
|
---|
[4] | 593 | }
|
---|
[124] | 594 |
|
---|
| 595 | public void setScale(float scale) {
|
---|
[108] | 596 | _scale = scale;
|
---|
[4] | 597 | }
|
---|
[603] | 598 |
|
---|
[609] | 599 | public void scaleCrop() {
|
---|
| 600 | // scale crop values to within image bounds
|
---|
| 601 | int iw = _image.getWidth(null);
|
---|
| 602 | int ih = _image.getHeight(null);
|
---|
| 603 | if(iw > 0 || ih > 0) {
|
---|
| 604 | while(_start.x >= iw) {
|
---|
| 605 | _start.x -= iw;
|
---|
| 606 | _end.x -= iw;
|
---|
| 607 | }
|
---|
| 608 | while(_start.y >= ih) {
|
---|
| 609 | _start.y -= ih;
|
---|
| 610 | _end.y -= ih;
|
---|
| 611 | }
|
---|
| 612 | while(_start.x < 0) {
|
---|
| 613 | _start.x += iw;
|
---|
| 614 | _end.x += iw;
|
---|
| 615 | }
|
---|
| 616 | while(_start.y < 0) {
|
---|
| 617 | _start.y += ih;
|
---|
| 618 | _end.y += ih;
|
---|
| 619 | }
|
---|
| 620 | }
|
---|
| 621 | }
|
---|
| 622 |
|
---|
[603] | 623 | public void setCrop(int startX, int startY, int endX, int endY) {
|
---|
| 624 | _start = new Point(startX, startY);
|
---|
| 625 | _end = new Point(endX, endY);
|
---|
[610] | 626 | updateSource();
|
---|
[603] | 627 | }
|
---|
[124] | 628 |
|
---|
[4] | 629 | @Override
|
---|
[108] | 630 | public float getSize() {
|
---|
[601] | 631 | return _source.getSize();
|
---|
[4] | 632 | }
|
---|
| 633 |
|
---|
| 634 | @Override
|
---|
[108] | 635 | public void setSize(float size) {
|
---|
[601] | 636 | float diff = size - _source.getSize();
|
---|
[4] | 637 | float oldScale = _scale;
|
---|
| 638 |
|
---|
[108] | 639 | float multiplier = (1000F + diff * 40F) / 1000F;
|
---|
| 640 | _scale = _scale * multiplier;
|
---|
[86] | 641 |
|
---|
[80] | 642 | // picture must still be at least XX pixels wide
|
---|
[108] | 643 | if (getWidth() < MINIMUM_WIDTH) {
|
---|
[4] | 644 | _scale = oldScale;
|
---|
[108] | 645 | } else {
|
---|
[601] | 646 | _source.translate(new Point2D.Float(FrameMouseActions.MouseX,
|
---|
[108] | 647 | FrameMouseActions.MouseY), multiplier);
|
---|
[4] | 648 | }
|
---|
[601] | 649 | updateSource();
|
---|
[4] | 650 | updatePolygon();
|
---|
[147] | 651 | // Make sure items that are resized display the border
|
---|
[130] | 652 | invalidateAll();
|
---|
[4] | 653 | }
|
---|
| 654 |
|
---|
| 655 | @Override
|
---|
| 656 | public void setAnnotation(boolean val) {
|
---|
| 657 | }
|
---|
| 658 |
|
---|
| 659 | /**
|
---|
| 660 | * Returns the Image that this Picture object is painting on the screen.
|
---|
| 661 | * This is used by Frame to repaint animated GIFs.
|
---|
| 662 | *
|
---|
| 663 | * @return The Image that this Picture object represents.
|
---|
| 664 | */
|
---|
| 665 | public Image getImage() {
|
---|
| 666 | return _image;
|
---|
| 667 | }
|
---|
| 668 |
|
---|
[226] | 669 | public Image getCroppedImage() {
|
---|
[376] | 670 | if (_image == null)
|
---|
[282] | 671 | return null;
|
---|
[376] | 672 | if (!isCropped()) {
|
---|
| 673 | return _image;
|
---|
| 674 | }
|
---|
| 675 |
|
---|
| 676 | return Toolkit.getDefaultToolkit().createImage(
|
---|
| 677 | new FilteredImageSource(_image.getSource(),
|
---|
[601] | 678 | new CropImageFilter(_start.x, _start.y,
|
---|
[376] | 679 | getUnscaledWidth(), getUnscaledHeight())));
|
---|
[226] | 680 | }
|
---|
| 681 |
|
---|
[376] | 682 | public int getUnscaledWidth() {
|
---|
[601] | 683 | return _end.x - _start.x;
|
---|
[376] | 684 | }
|
---|
| 685 |
|
---|
| 686 | public int getUnscaledHeight() {
|
---|
[601] | 687 | return _end.y - _start.y;
|
---|
[376] | 688 | }
|
---|
| 689 |
|
---|
| 690 | /**
|
---|
| 691 | * @return true if this is a cropped image.
|
---|
| 692 | */
|
---|
| 693 | public boolean isCropped() {
|
---|
[726] | 694 | return (_end.x != 0 && _end.x != _image.getWidth(null)) || (_end.y != 0 && _end.y != _image.getHeight(null)) || _start.y != 0 || _start.x != 0;
|
---|
[376] | 695 | }
|
---|
| 696 |
|
---|
[601] | 697 | @Override
|
---|
[78] | 698 | public boolean refresh() {
|
---|
| 699 | // ImageIcon is faster, but cannot handle some formats
|
---|
[147] | 700 | // (notably.bmp) hence, we try this first, then if it fails we try
|
---|
[78] | 701 | // ImageIO
|
---|
[147] | 702 | try {
|
---|
| 703 | _image = new ImageIcon(_path).getImage();
|
---|
| 704 | } catch (Exception e) {
|
---|
| 705 | }
|
---|
[78] | 706 |
|
---|
| 707 | // if ImageIcon failed to read the image
|
---|
[147] | 708 | if (_image == null || _image.getWidth(null) <= 0) {
|
---|
[78] | 709 | try {
|
---|
| 710 | _image = ImageIO.read(new File(_path));
|
---|
[601] | 711 | } catch (IOException e) {
|
---|
[78] | 712 | // e.printStackTrace();
|
---|
[601] | 713 | Logger.Log(e);
|
---|
| 714 | _image = null;
|
---|
[80] | 715 | return false;
|
---|
[7] | 716 | }
|
---|
| 717 | }
|
---|
[78] | 718 | return true;
|
---|
[7] | 719 | }
|
---|
| 720 |
|
---|
[78] | 721 | @Override
|
---|
| 722 | protected int getLinkYOffset() {
|
---|
| 723 | return getBoundsHeight() / 2;
|
---|
| 724 | }
|
---|
| 725 |
|
---|
| 726 | @Override
|
---|
| 727 | public void setLinkMark(boolean state) {
|
---|
[147] | 728 | // TODO use the more efficient invalidiate method
|
---|
| 729 | // The commented code below is not quite working
|
---|
[130] | 730 | // if(!state)
|
---|
| 731 | // invalidateCommonTrait(ItemAppearence.LinkChanged);
|
---|
[601] | 732 | _source.setLinkMark(state);
|
---|
[130] | 733 | // if(state)
|
---|
| 734 | // invalidateCommonTrait(ItemAppearence.LinkChanged);
|
---|
| 735 | invalidateAll();
|
---|
[78] | 736 | }
|
---|
| 737 |
|
---|
| 738 | @Override
|
---|
| 739 | public void setActionMark(boolean state) {
|
---|
[130] | 740 | // if (!state)
|
---|
| 741 | // invalidateCommonTrait(ItemAppearence.LinkChanged);
|
---|
[601] | 742 | _source.setActionMark(state);
|
---|
[130] | 743 | // if (state)
|
---|
| 744 | // invalidateCommonTrait(ItemAppearence.LinkChanged);
|
---|
| 745 | invalidateAll();
|
---|
[78] | 746 | }
|
---|
| 747 |
|
---|
| 748 | @Override
|
---|
[601] | 749 | public boolean getLinkMark() {
|
---|
| 750 | return !FrameGraphics.isAudienceMode() && _source.getLinkMark();
|
---|
| 751 | }
|
---|
| 752 |
|
---|
| 753 | @Override
|
---|
| 754 | public boolean getActionMark() {
|
---|
| 755 | return _source.getActionMark();
|
---|
| 756 | }
|
---|
| 757 |
|
---|
| 758 | @Override
|
---|
[80] | 759 | public String getName() {
|
---|
[601] | 760 | return _fileName;
|
---|
[80] | 761 | }
|
---|
[843] | 762 |
|
---|
| 763 | public String getPath() {
|
---|
| 764 | return _path;
|
---|
| 765 | }
|
---|
| 766 |
|
---|
| 767 | /**
|
---|
| 768 | * Copies the image to the default images folder and updates the reference to it in Expeditee
|
---|
| 769 | * Used for correcting image references for FrameShare
|
---|
| 770 | */
|
---|
| 771 | public void moveToImagesFolder() {
|
---|
| 772 | File f = new File(getPath());
|
---|
| 773 | // if the file is not in the default images folder, copy it there
|
---|
| 774 | if(! f.getParentFile().equals(new File(FrameIO.IMAGES_PATH))) {
|
---|
| 775 | try {
|
---|
| 776 | File f2 = new File(FrameIO.IMAGES_PATH + f.getName());
|
---|
| 777 | FrameUtils.copyFile(f, f2, false);
|
---|
| 778 | f = f2;
|
---|
| 779 | } catch (IOException e) {
|
---|
| 780 | e.printStackTrace();
|
---|
| 781 | f = null;
|
---|
| 782 | }
|
---|
| 783 | }
|
---|
| 784 | _path = f.getPath();
|
---|
| 785 | _fileName = f.getName();
|
---|
| 786 | updateSource();
|
---|
| 787 | }
|
---|
[80] | 788 |
|
---|
[601] | 789 | protected String getTagText() {
|
---|
| 790 | return "@i: " + _fileName + " ";
|
---|
| 791 | }
|
---|
| 792 |
|
---|
| 793 | /**
|
---|
| 794 | * Updates the source text for this item to match the current size of the
|
---|
| 795 | * image.
|
---|
| 796 | *
|
---|
| 797 | */
|
---|
| 798 | private void updateSource() {
|
---|
| 799 | StringBuffer newText = new StringBuffer(getTagText());
|
---|
| 800 |
|
---|
| 801 | switch (_scaleType) {
|
---|
| 802 | case (RATIO):
|
---|
| 803 | DecimalFormat format = new DecimalFormat("0.00");
|
---|
| 804 | newText.append(format.format(_scale));
|
---|
| 805 | break;
|
---|
| 806 | case (WIDTH):
|
---|
| 807 | newText.append(getWidth());
|
---|
| 808 | break;
|
---|
| 809 | }
|
---|
[609] | 810 |
|
---|
| 811 | scaleCrop();
|
---|
[601] | 812 |
|
---|
| 813 | // If the image is cropped add the position for the start and finish of
|
---|
| 814 | // the crop to the soure text
|
---|
[609] | 815 | if (_start.x > 0 || _start.y > 0 || _end.x != _image.getWidth(null)
|
---|
| 816 | || _end.y != _image.getHeight(null)) {
|
---|
[601] | 817 | newText.append(" ").append(_start.x).append(" ").append(_start.y);
|
---|
| 818 | newText.append(" ").append(_end.x).append(" ").append(_end.y);
|
---|
| 819 | }
|
---|
[643] | 820 |
|
---|
| 821 | if(_flipX) {
|
---|
| 822 | newText.append(" flipX");
|
---|
| 823 | }
|
---|
| 824 | if(_flipY) {
|
---|
| 825 | newText.append(" flipY");
|
---|
| 826 | }
|
---|
[653] | 827 | if(Double.compare(_rotate, 0) != 0) {
|
---|
| 828 | newText.append(" rotation=" + _rotate);
|
---|
| 829 | }
|
---|
[601] | 830 |
|
---|
| 831 | _source.setText(newText.toString());
|
---|
| 832 | }
|
---|
| 833 |
|
---|
[108] | 834 | @Override
|
---|
[124] | 835 | public void translate(Point2D origin, double ratio) {
|
---|
[108] | 836 | _scale *= ratio;
|
---|
[601] | 837 | updateSource();
|
---|
[108] | 838 | super.translate(origin, ratio);
|
---|
| 839 | }
|
---|
[121] | 840 |
|
---|
| 841 | @Override
|
---|
| 842 | public Rectangle[] getDrawingArea() {
|
---|
[124] | 843 |
|
---|
[121] | 844 | Rectangle[] da = super.getDrawingArea();
|
---|
[124] | 845 |
|
---|
[133] | 846 | if (getLink() != null || hasAction()) {
|
---|
[121] | 847 | Rectangle[] da2 = new Rectangle[da.length + 1];
|
---|
| 848 | System.arraycopy(da, 0, da2, 0, da.length);
|
---|
| 849 | da2[da.length] = getLinkPoly().getBounds();
|
---|
[124] | 850 | da2[da.length].translate(getX() - LEFT_MARGIN, getY()
|
---|
| 851 | + getLinkYOffset());
|
---|
[121] | 852 | da2[da.length].width += 2;
|
---|
| 853 | da2[da.length].height += 2;
|
---|
| 854 | da = da2;
|
---|
[124] | 855 | }
|
---|
| 856 |
|
---|
[121] | 857 | return da;
|
---|
[124] | 858 |
|
---|
[121] | 859 | }
|
---|
[376] | 860 |
|
---|
[311] | 861 | @Override
|
---|
[376] | 862 | public void scale(Float scale, int originX, int originY) {
|
---|
[311] | 863 | setScale(getScale() * scale);
|
---|
| 864 | super.scale(scale, originX, originY);
|
---|
| 865 | }
|
---|
[643] | 866 |
|
---|
| 867 | public void setFlipX(boolean flip) {
|
---|
| 868 | _flipX = flip;
|
---|
| 869 | }
|
---|
| 870 |
|
---|
| 871 | public void setFlipY(boolean flip) {
|
---|
| 872 | _flipY = flip;
|
---|
| 873 | }
|
---|
| 874 |
|
---|
| 875 | public boolean getFlipX() {
|
---|
| 876 | return _flipX;
|
---|
| 877 | }
|
---|
| 878 |
|
---|
| 879 | public boolean getFlipY() {
|
---|
| 880 | return _flipY;
|
---|
| 881 | }
|
---|
[653] | 882 |
|
---|
| 883 | public void setRotate(double rotate) {
|
---|
| 884 | _rotate = rotate;
|
---|
| 885 | updateSource();
|
---|
| 886 | updatePolygon();
|
---|
| 887 | }
|
---|
| 888 |
|
---|
| 889 | public double getRotate() {
|
---|
| 890 | return _rotate;
|
---|
| 891 | }
|
---|
[124] | 892 |
|
---|
[4] | 893 | }
|
---|