001package com.hfg.graphics;
002
003import java.awt.*;
004import java.awt.geom.Path2D;
005import java.awt.geom.Point2D;
006import java.awt.geom.Rectangle2D;
007
008//------------------------------------------------------------------------------
009/**
010 * Utility methods for use with Graphics2D.
011 * <div>
012 *   @author J. Alex Taylor, hairyfatguy.com
013 * </div>
014 */
015//------------------------------------------------------------------------------
016// com.hfg XML/HTML Coding Library
017//
018// This library is free software; you can redistribute it and/or
019// modify it under the terms of the GNU Lesser General Public
020// License as published by the Free Software Foundation; either
021// version 2.1 of the License, or (at your option) any later version.
022//
023// This library is distributed in the hope that it will be useful,
024// but WITHOUT ANY WARRANTY; without even the implied warranty of
025// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
026// Lesser General Public License for more details.
027//
028// You should have received a copy of the GNU Lesser General Public
029// License along with this library; if not, write to the Free Software
030// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
031//
032// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
033// jataylor@hairyfatguy.com
034//------------------------------------------------------------------------------
035
036public class GraphicsUtil
037{
038
039   //--------------------------------------------------------------------------
040   public static Path2D rotate(Rectangle2D inRect, float inAngleInDegrees, Point2D inCenterOfRotation)
041   {
042      Point2D.Double p1 = new Point2D.Double(inRect.getMinX(), inRect.getMinY());
043      Point2D.Double p2 = new Point2D.Double(inRect.getMaxX(), inRect.getMinY());
044      Point2D.Double p3 = new Point2D.Double(inRect.getMinX(), inRect.getMaxY());
045      Point2D.Double p4 = new Point2D.Double(inRect.getMaxX(), inRect.getMaxY());
046
047      Double cx = inCenterOfRotation.getX();
048      Double cy = inCenterOfRotation.getY();
049
050      double angleInRadians = Math.toRadians(inAngleInDegrees);
051
052      p1.setLocation((p1.getX()-cx) * Math.cos(angleInRadians) - (p1.getY()-cy) * Math.sin(angleInRadians)+cx,
053            (p1.getX()-cx) * Math.sin(angleInRadians) + (p1.getY()-cy) * Math.cos(angleInRadians)+cy);
054
055      p2.setLocation((p2.getX()-cx) * Math.cos(angleInRadians) - (p2.getY()-cy) * Math.sin(angleInRadians)+cx,
056            (p2.getX()-cx) * Math.sin(angleInRadians) + (p2.getY()-cy) * Math.cos(angleInRadians)+cy);
057
058      p3.setLocation((p3.getX()-cx) * Math.cos(angleInRadians) - (p3.getY()-cy) * Math.sin(angleInRadians)+cx,
059            (p3.getX()-cx) * Math.sin(angleInRadians) + (p3.getY()-cy) * Math.cos(angleInRadians)+cy);
060
061      p4.setLocation((p4.getX()-cx) * Math.cos(angleInRadians) - (p4.getY()-cy) * Math.sin(angleInRadians)+cx,
062            (p4.getX()-cx) * Math.sin(angleInRadians) + (p4.getY()-cy) * Math.cos(angleInRadians)+cy);
063
064      double[] x = {p1.getX(), p2.getX(), p3.getX(), p4.getX()};
065      double[] y = {p1.getY(), p2.getY(), p3.getY(), p4.getY()};
066
067      Path2D path = new Path2D.Double(Path2D.WIND_NON_ZERO, 2);
068      path.moveTo(p1.getX(), p1.getY());
069      path.lineTo(p2.getX(), p2.getY());
070      path.lineTo(p3.getX(), p3.getY());
071      path.lineTo(p4.getX(), p4.getY());
072      path.closePath();
073
074      return path;
075   }
076
077   //--------------------------------------------------------------------------
078   public static Polygon rotate(Rectangle inRect, float inAngleInDegrees, Point2D inCenterOfRotation)
079   {
080      Point2D.Double p1 = new Point2D.Double(inRect.getMinX(), inRect.getMinY());
081      Point2D.Double p2 = new Point2D.Double(inRect.getMaxX(), inRect.getMinY());
082      Point2D.Double p3 = new Point2D.Double(inRect.getMinX(), inRect.getMaxY());
083      Point2D.Double p4 = new Point2D.Double(inRect.getMaxX(), inRect.getMaxY());
084
085      Double cx = inCenterOfRotation.getX();
086      Double cy = inCenterOfRotation.getY();
087
088      double angleInRadians = Math.toRadians(inAngleInDegrees);
089
090      p1.setLocation((p1.getX()-cx) * Math.cos(angleInRadians) - (p1.getY()-cy) * Math.sin(angleInRadians)+cx,
091            (p1.getX()-cx) * Math.sin(angleInRadians) + (p1.getY()-cy) * Math.cos(angleInRadians)+cy);
092
093      p2.setLocation((p2.getX()-cx) * Math.cos(angleInRadians) - (p2.getY()-cy) * Math.sin(angleInRadians)+cx,
094            (p2.getX()-cx) * Math.sin(angleInRadians) + (p2.getY()-cy) * Math.cos(angleInRadians)+cy);
095
096      p3.setLocation((p3.getX()-cx) * Math.cos(angleInRadians) - (p3.getY()-cy) * Math.sin(angleInRadians)+cx,
097            (p3.getX()-cx) * Math.sin(angleInRadians) + (p3.getY()-cy) * Math.cos(angleInRadians)+cy);
098
099      p4.setLocation((p4.getX()-cx) * Math.cos(angleInRadians) - (p4.getY()-cy) * Math.sin(angleInRadians)+cx,
100            (p4.getX()-cx) * Math.sin(angleInRadians) + (p4.getY()-cy) * Math.cos(angleInRadians)+cy);
101
102      int[] x = {(int) p1.getX(), (int) p2.getX(), (int)p3.getX(), (int) p4.getX()};
103      int[] y = {(int) p1.getY(), (int) p2.getY(), (int)p3.getY(), (int) p4.getY()};
104
105      return new Polygon(x, y, x.length);
106   }
107}