Changeset 653


Ignore:
Timestamp:
01/06/14 11:21:12 (10 years ago)
Author:
jts21
Message:

Implemented image rotation

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/expeditee/items/Picture.java

    r643 r653  
    1212import java.awt.Stroke;
    1313import java.awt.Toolkit;
     14import java.awt.geom.AffineTransform;
    1415import java.awt.geom.Point2D;
     16import java.awt.image.BufferedImage;
    1517import java.awt.image.CropImageFilter;
    1618import java.awt.image.FilteredImageSource;
     
    6769
    6870        private Point _end = new Point(0, 0);
     71       
     72        private double _rotate = 0;
    6973       
    7074        private boolean _flipX = false;
     
    139143                        _end.setLocation(_image.getWidth(null), _image.getHeight(null));
    140144                size = size.trim();
     145                String sizeLower = size.toLowerCase();
    141146                String[] values = size.split("\\s+");
    142147                // Now get the cropping values if there are any
     
    156161                }
    157162               
    158                 if(size.toLowerCase().contains("flipx")) {
     163                if(sizeLower.contains("flipx")) {
    159164                        _flipX = true;
    160165                }
    161166               
    162                 if(size.toLowerCase().contains("flipy")) {
     167                if(sizeLower.contains("flipy")) {
    163168                        _flipY = true;
     169                }
     170               
     171                int index = sizeLower.indexOf("rotation=");
     172                if(index != -1) {
     173                        int tmp = sizeLower.indexOf(" ", index);
     174                        String rotation;
     175                        if(tmp == -1) {
     176                                rotation = sizeLower.substring(index + "rotation=".length());
     177                        } else {
     178                                rotation = sizeLower.substring(index + "rotation=".length(), index + tmp);
     179                        }
     180                        _rotate = Double.parseDouble(rotation);
    164181                }
    165182
     
    252269                        parseSize();
    253270                }
    254                 _poly = new Polygon();
     271               
     272                Point[] ori = new Point[4];
     273                Point2D[] rot = new Point2D[4];
     274                Point centre = new Point();
    255275
    256276                if (_cropStart == null || _cropEnd == null) {
    257277                        int width = getWidth();
    258278                        int height = getHeight();
     279                       
     280                        centre.x = _source.getX() + width / 2;
     281                        centre.y = _source.getY() + height / 2;
    259282
    260283                        int xdiff = -MARGIN_RIGHT; // -getLeftMargin();
    261284
    262285                        // extra pixel around the image so the highlighting is visible
    263                         _poly.addPoint(_source.getX() + 1 + xdiff, _source.getY() - 1);
    264                         _poly.addPoint(_source.getX() + width, _source.getY() - 1);
    265                         _poly.addPoint(_source.getX() + width, _source.getY() + height);
    266                         _poly.addPoint(_source.getX() + 1 + xdiff, _source.getY() + height);
     286//                      _poly.addPoint(_source.getX() + 1 + xdiff, _source.getY() - 1);
     287//                      _poly.addPoint(_source.getX() + width, _source.getY() - 1);
     288//                      _poly.addPoint(_source.getX() + width, _source.getY() + height);
     289//                      _poly.addPoint(_source.getX() + 1 + xdiff, _source.getY() + height);
     290                       
     291                        ori[0] = new Point(_source.getX() + 1 + xdiff, _source.getY() - 1);
     292                        ori[1] = new Point(_source.getX() + width, _source.getY() - 1);
     293                        ori[2] = new Point(_source.getX() + width, _source.getY() + height);
     294                        ori[3] = new Point(_source.getX() + 1 + xdiff, _source.getY() + height);
     295                       
    267296                } else {
    268297                        Point topLeft = getTopLeftCrop();
    269298                        Point bottomRight = getBottomRightCrop();
     299                       
     300                        centre.x = _source.getX() + (bottomRight.x - topLeft.x) / 2;
     301                        centre.y = _source.getY() + (bottomRight.y - topLeft.y) / 2;
     302                       
    270303                        Rectangle clip = new Rectangle(topLeft.x + _source.getX(),
    271304                                        topLeft.y + _source.getY(), bottomRight.x - topLeft.x,
    272305                                        bottomRight.y - topLeft.y).getBounds();
    273                         _poly.addPoint((int) clip.getMinX() - 1, (int) clip.getMinY() - 1);
    274                         _poly.addPoint((int) clip.getMinX() - 1, (int) clip.getMaxY());
    275                         _poly.addPoint((int) clip.getMaxX(), (int) clip.getMaxY());
    276                         _poly.addPoint((int) clip.getMaxX(), (int) clip.getMinY() - 1);
    277 
     306//                      _poly.addPoint((int) clip.getMinX() - 1, (int) clip.getMinY() - 1);
     307//                      _poly.addPoint((int) clip.getMinX() - 1, (int) clip.getMaxY());
     308//                      _poly.addPoint((int) clip.getMaxX(), (int) clip.getMaxY());
     309//                      _poly.addPoint((int) clip.getMaxX(), (int) clip.getMinY() - 1);
     310                       
     311                        ori[0] = new Point((int) clip.getMinX() - 1, (int) clip.getMinY() - 1);
     312                        ori[1] = new Point((int) clip.getMinX() - 1, (int) clip.getMaxY());
     313                        ori[2] = new Point((int) clip.getMaxX(), (int) clip.getMaxY());
     314                        ori[3] = new Point((int) clip.getMaxX(), (int) clip.getMinY() - 1);
     315
     316                }
     317               
     318                AffineTransform.getRotateInstance(_rotate, centre.x, centre.y).transform(ori, 0, rot, 0, 4);
     319               
     320                _poly = new Polygon();
     321                for(Point2D p : rot) {
     322                        _poly.addPoint((int)p.getX(), (int)p.getY());
    278323                }
    279324        }
     
    339384                int dY2 = _source.getY() + getHeight();
    340385               
     386                BufferedImage tmp = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
     387                Graphics2D g2d = tmp.createGraphics();
     388                int offX = (tmp.getWidth() - getWidth()) / 2;
     389                int offY = (tmp.getHeight() - getHeight()) / 2;
     390               
     391                // g2d.rotate(rotate, tmp.getWidth() / 2, tmp.getHeight() / 2);
    341392               
    342393                int cropStartX = _start.x;
     
    368419                                }
    369420                               
    370                                 System.out.println(_flipX + " " + _flipY);
    371                                
    372421                                int sx = _flipX ? cropEndX : cropStartX;
    373422                                int ex = _flipX ? cropStartX : cropEndX;
    374423                                int sy = _flipY ? cropEndY : cropStartY;
    375424                                int ey = _flipY ? cropStartY : cropEndY;
    376                                 g.drawImage(_image, x, y, endX, endY, sx, sy, ex, ey, _imageObserver);
     425                                g2d.drawImage(_image, x - dX1 + offX, y - dY1 + offY, endX - dX1 + offX, endY - dY1 + offY, sx, sy, ex, ey, null);
    377426                               
    378427                                cropStartY = 0;
     
    387436                        x = endX;
    388437                }
     438               
     439                AffineTransform at = new AffineTransform();
     440                at.translate(dX1, dY1);
     441                at.rotate(_rotate, tmp.getWidth() / 2, tmp.getHeight() / 2);
     442                g.drawImage(tmp, at, _imageObserver);
     443                // g.drawImage(tmp, dX1, dY1, dX2, dY2, 0, 0, tmp.getWidth(), tmp.getHeight(), _imageObserver);
    389444        }
    390445
     
    724779                        newText.append(" flipY");
    725780                }
     781                if(Double.compare(_rotate, 0) != 0) {
     782                        newText.append(" rotation=" + _rotate);
     783                }
    726784
    727785                _source.setText(newText.toString());
     
    776834                return _flipY;
    777835        }
     836       
     837        public void setRotate(double rotate) {
     838                _rotate = rotate;
     839                updateSource();
     840                updatePolygon();
     841        }
     842       
     843        public double getRotate() {
     844                return _rotate;
     845        }
    778846
    779847}
Note: See TracChangeset for help on using the changeset viewer.