View Javadoc

    1   /*
    2    * @(#) Renderable2DInterface.java Sep 6, 2003
    3    *
    4    * Copyright (c) 2003 Delft University of Technology Jaffalaan 5, 2628 BX Delft,
    5    * the Netherlands All rights reserved.
    6    *
    7    * This software is proprietary information of Delft University of Technology
    8    * The code is published under the General Public License
    9    */
    10  package nl.tudelft.simulation.dsol.animation.D2;
    11
    12  import java.awt.Dimension;
    13  import java.awt.Graphics2D;
    14  import java.awt.geom.Point2D;
    15  import java.awt.geom.Rectangle2D;
    16  import java.awt.image.ImageObserver;
    17  import java.rmi.RemoteException;
    18
    19  import nl.tudelft.simulation.dsol.animation.LocatableInterface;
    20
    21  /***
    22   * The Renderable2D interface defines the basic interface for 2d animation. This
    23   * is a hard-to-use interface. It is implemented by the easy-to-use Renderable2D
    24   * class <br>
    25   * (c) copyright 2003 <a href="http://www.simulation.tudelft.nl"> Delft
    26   * University of Technology </a>, the Netherlands. <br>
    27   * See for project information <a href="http://www.simulation.tudelft.nl">
    28   * www.simulation.tudelft.nl </a> <br>
    29   * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General Public
    30   * License (GPL) </a>, no warranty <br>
    31   *
    32   * @version 1.0 06.09.2003 <br>
    33   * @author <a href="http://www.tbm.tudelft.nl/webstaf/peterja/index.htm">Peter
    34   *         Jacobs </a>
    35   */
    36  public interface Renderable2DInterface
    37  {
    38  	/***
    39  	 * paints the object on a 2D graphics object
    40  	 *
    41  	 * @param graphics the graphics object
    42  	 * @param extent the extent of the panel
    43  	 * @param screen the screen of the panel
    44  	 * @param observer the observer of the renderableInterface
    45  	 */
    46  	void paint(final Graphics2D graphics, final Rectangle2D extent,
    47  			final Dimension screen, final ImageObserver observer);
    48
    49  	/***
    50  	 * gets the source of this renderable
    51  	 *
    52  	 * @return Locatable the source
    53  	 */
    54  	LocatableInterface getSource();
    55
    56  	/***
    57  	 * does the shape contain the point
    58  	 *
    59  	 * @param pointWorldCoordinates the point in world coordinates. Default
    60  	 *        implementation is to intersect the 3D bounds on location.z and to
    61  	 *        return the bounds2D of this intersect.
    62  	 * @param extent the extent of the panel.
    63  	 * @param screen the screen of the panel.
    64  	 * @return whether the point is in the shape
    65  	 */
    66  	boolean contains(Point2D pointWorldCoordinates, final Rectangle2D extent,
    67  			Dimension screen);
    68
    69  	/***
    70  	 * destroys this editable. How to do this must be implemented by the modeler
    71  	 *
    72  	 * @throws RemoteException RemoteException
    73  	 */
    74  	void destroy() throws RemoteException;
    75
    76  	/***
    77  	 * A Util <br>
    78  	 * (c) copyright 2003 <a href="http://www.simulation.tudelft.nl">Delft
    79  	 * University of Technology </a>, the Netherlands. <br>
    80  	 * See for project information <a
    81  	 * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a>
    82  	 * <br>
    83  	 * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General
    84  	 * Public License (GPL) </a>, no warranty <br>
    85  	 *
    86  	 * @version 1.0 Mar 1, 2004 <br>
    87  	 * @author <a
    88  	 *         href="http://www.simulation.tudelft.nl/people/jacobs.html">Peter
    89  	 *         Jacobs </a>
    90  	 */
    91  	static class Util
    92  	{
    93
    94  		/***
    95  		 * constructs a new Util
    96  		 */
    97  		protected Util()
    98  		{
    99  			//constructs a new transform
    100 		}
    101
    102 		/***
    103 		 * returns the scale of a screen compared to an extent. The scale can
    104 		 * only be computed if the xScale and yScale are equal. If this is not
    105 		 * the case, Double.NaN is returned. In order to overcome estimation
    106 		 * errors, this equality is computed with Math.abs(yScale-xScale)
    107 		 * <0.005xScale. If the height or the width of the screen are <0
    108 		 * Double.NaN is returned.
    109 		 *
    110 		 * @param extent the extent
    111 		 * @param screen the screen
    112 		 * @return double the scale. Can return Double.NaN
    113 		 */
    114 		public static double getScale(final Rectangle2D extent,
    115 				final Dimension screen)
    116 		{
    117 			if (screen.getHeight() <= 0 || screen.getWidth() <= 0)
    118 			{
    119 				return Double.NaN;
    120 			}
    121 			return extent.getWidth() / screen.getWidth();
    122 		}
    123
    124 		/***
    125 		 * computes the visible extent
    126 		 *
    127 		 * @param extent the extent
    128 		 * @param screen the screen
    129 		 * @return a new extent or null if parameters are null or screen is
    130 		 *         invalid ( <0)
    131 		 */
    132 		public static Rectangle2D computeVisibleExtent(
    133 				final Rectangle2D extent, final Dimension screen)
    134 		{
    135 			if (extent == null || screen == null || screen.getHeight() <= 0
    136 					|| screen.getWidth() <= 0)
    137 			{
    138 				return null;
    139 			}
    140 			double xScale = extent.getWidth() / screen.getWidth();
    141 			double yScale = extent.getHeight() / screen.getHeight();
    142 			Rectangle2D result = (Rectangle2D) extent.clone();
    143 			if (xScale >= yScale)
    144 			{
    145 				result.setRect(result.getCenterX() - 0.5 * yScale
    146 						* screen.getWidth(), result.getY(), yScale
    147 						* screen.getWidth(), result.getHeight());
    148 			} else
    149 			{
    150 				result.setRect(result.getX(), result.getCenterY() - 0.5
    151 						* xScale * screen.getHeight(), result.getWidth(),
    152 						xScale * screen.getHeight());
    153 			}
    154 			return result;
    155 		}
    156
    157 		/***
    158 		 * returns the frame xy-coordinates of a point in world coordinates. If
    159 		 * parameters are invalid (i.e. screen.size <0) a null value is
    160 		 * returned. If parameter combinations (i.e !extent.contains(point)) are
    161 		 * invalid a null value is returned.
    162 		 *
    163 		 * @param worldCoordinates the world coordinates
    164 		 * @param extent the extent of this
    165 		 * @param screen the screen
    166 		 * @return Point2D (x,y) on screen. Can be null
    167 		 */
    168 		public static Point2D getScreenCoordinates(
    169 				final Point2D worldCoordinates, final Rectangle2D extent,
    170 				final Dimension screen)
    171 		{
    172 			double scale = 1.0 / Renderable2DInterface.Util.getScale(extent,
    173 					screen);
    174 			double x = (worldCoordinates.getX() - extent.getMinX()) * scale;
    175 			double y = screen.getHeight()
    176 					- (worldCoordinates.getY() - extent.getMinY()) * scale;
    177 			return new Point2D.Double(x, y);
    178 		}
    179
    180 		/***
    181 		 * returns the frame xy-coordinates of a point in screen coordinates. If
    182 		 * parameters are invalid (i.e. screen.size <0) a null value is
    183 		 * returned. If parameter combinations (i.e !screen.contains(point)) are
    184 		 * invalid a null value is returned.
    185 		 *
    186 		 * @param screenCoordinates the screen coordinates
    187 		 * @param extent the extent of this
    188 		 * @param screen the screen
    189 		 * @return Point2D (x,y) on screen
    190 		 */
    191 		public static Point2D getWorldCoordinates(
    192 				final Point2D screenCoordinates, final Rectangle2D extent,
    193 				final Dimension screen)
    194 		{
    195 			double scale = Renderable2DInterface.Util.getScale(extent, screen);
    196 			double x = (screenCoordinates.getX()) * scale + extent.getMinX();
    197 			double y = ((screen.getHeight() - screenCoordinates.getY()))
    198 					* scale + extent.getMinY();
    199 			return new Point2D.Double(x, y);
    200 		}
    201 	}
    202 }