View Javadoc

1   /*
2    * @(#) GisGraphicsRenderable.java Dec 14, 2004
3    * 
4    * 
5    * Copyright (c) 2003-2005 Delft University of Technology, Jaffalaan 5, 2628 BX
6    * Delft, the Netherlands. All rights reserved.
7    * 
8    * See for project information <a href="http://www.simulation.tudelft.nl/">
9    * www.simulation.tudelft.nl </a>.
10   * 
11   * The source code and binary code of this software is proprietary information
12   * of Delft University of Technology.
13   */
14  package org.gscg.gameleader.animation2D;
15  
16  import java.awt.Dimension;
17  import java.awt.Graphics2D;
18  import java.awt.geom.Point2D;
19  import java.awt.geom.Rectangle2D;
20  import java.awt.image.ImageObserver;
21  import java.io.IOException;
22  import java.io.ObjectOutputStream;
23  import java.io.Serializable;
24  import java.rmi.RemoteException;
25  
26  import javax.media.j3d.Bounds;
27  
28  import nl.tudelft.simulation.dsol.animation.LocatableInterface;
29  import nl.tudelft.simulation.dsol.animation.D2.Renderable2DInterface;
30  import nl.tudelft.simulation.dsol.context.ContextUtil;
31  import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
32  import nl.tudelft.simulation.language.d3.BoundingBox;
33  import nl.tudelft.simulation.language.d3.BoundsUtil;
34  import nl.tudelft.simulation.language.d3.DirectedPoint;
35  import nl.tudelft.simulation.logger.Logger;
36  
37  /***
38   * The GisGraphicsRenderable is useful in combination with a GIS based map as a
39   * background image. Based on the zoom level of an animation panel, choices can
40   * be made what to show, or what not. It is comparable with showing layers when
41   * using a GIS map.
42   * <p>
43   * 
44   * Copyright (c) 2003-2005 Delft University of Technology, Jaffalaan 5, 2628 BX
45   * Delft, the Netherlands. All rights reserved.
46   * 
47   * See for project information <a href="http://www.simulation.tudelft.nl/">
48   * www.simulation.tudelft.nl </a>.
49   * 
50   * The source code and binary code of this software is proprietary information
51   * of Delft University of Technology.
52   * 
53   * @author <a
54   *         href="http://www.tbm.tudelft.nl/webstaf/stijnh/index.htm">Stijn-Pieter
55   *         van Houten </a>
56   * @version $Revision: 1.1 $ $Date: 2005/06/16 12:34:02 $
57   * @since 1.0.8
58   */
59  public abstract class GisGraphicsRenderable implements Renderable2DInterface
60  {
61  	/*** the locatable */
62  	protected Locatable locatable = null;
63  
64  	/*** the simulator */
65  	private SimulatorInterface simulator = null;
66  
67  	/***
68  	 * constructs a new GisGraphicsRenderable; this emtpy constructor is needed
69  	 * for persistency reasons
70  	 */
71  	public GisGraphicsRenderable()
72  	{
73  		super();
74  	}
75  
76  	/***
77  	 * constructs a new GisGraphicsRenderable
78  	 * 
79  	 * @param location the location
80  	 * @param simulator the simulator to use
81  	 * @param contextName the name of the context to use, may be null
82  	 */
83  	public GisGraphicsRenderable(final DirectedPoint location,
84  			final SimulatorInterface simulator, final String contextName)
85  	{
86  		super();
87  		this.locatable = new Locatable(location, new BoundingBox(0.0, 0.0, 0.0));
88  		this.simulator = simulator;
89  		this.bindToContext(contextName);
90  	}
91  
92  	/***
93  	 * binds the renderable to the context
94  	 * 
95  	 * @param contextName the name of the context to use, may be null
96  	 */
97  	protected void bindToContext(final String contextName)
98  	{
99  		if (contextName == null)
100 		{
101 			ContextUtil.bindToContext(this.simulator, "/animation/2D", this);
102 		} else
103 		{
104 			ContextUtil.bindToContext(contextName, this);
105 		}
106 	}
107 
108 	/***
109 	 * @see nl.tudelft.simulation.dsol.animation.D2.Renderable2DInterface#paint(java.awt.Graphics2D,
110 	 *      java.awt.geom.Rectangle2D, java.awt.Dimension,
111 	 *      java.awt.image.ImageObserver)
112 	 */
113 	public abstract void paint(final Graphics2D graphics,
114 			final Rectangle2D rectangle, final Dimension dimension,
115 			final ImageObserver observer);
116 
117 	/***
118 	 * @see nl.tudelft.simulation.dsol.animation.D2.Renderable2DInterface
119 	 *      #contains(java.awt.geom.Point2D, java.awt.geom.Rectangle2D,
120 	 *      java.awt.Dimension)
121 	 */
122 	public boolean contains(final Point2D pointWorldCoordinates,
123 			final Rectangle2D extent, final Dimension screen)
124 	{
125 		try
126 		{
127 			Rectangle2D intersect = BoundsUtil.getIntersect(this.locatable
128 					.getLocation(), this.locatable.getBounds(), this.locatable
129 					.getLocation().z);
130 			if (intersect == null)
131 			{
132 				throw new NullPointerException(
133 						"empty intersect!: location.z is not in bounds. This is probably due to a modeling error. See the javadoc off LocatableInterface.");
134 			}
135 			return intersect.contains(pointWorldCoordinates);
136 		} catch (RemoteException remoteException)
137 		{
138 			Logger.severe(this, "contains", remoteException);
139 			return false;
140 		}
141 	}
142 
143 	/***
144 	 * destroys an RenderableObject by unsubscribing it from the context.
145 	 */
146 	public void destroy()
147 	{
148 		try
149 		{
150 			nl.tudelft.simulation.naming.context.ContextUtil
151 					.unbindFromContext(this);
152 		} catch (Throwable throwable)
153 		{
154 			Logger.warning(this, "finalize", throwable);
155 		}
156 	}
157 
158 	/***
159 	 * @see nl.tudelft.simulation.dsol.animation.D2.Renderable2DInterface#getSource()
160 	 */
161 	public LocatableInterface getSource()
162 	{
163 		return this.locatable;
164 	}
165 
166 	//
167 	// private methods
168 	//
169 	/***
170 	 * writes a serializable method to stream
171 	 * 
172 	 * @param out the outputstream
173 	 * @throws IOException on IOException
174 	 */
175 	private synchronized void writeObject(final ObjectOutputStream out)
176 			throws IOException
177 	{
178 		out.defaultWriteObject();
179 	}
180 
181 	/***
182 	 * reads a serializable method from stream
183 	 * 
184 	 * @param in the inputstream
185 	 * @throws IOException on IOException
186 	 */
187 	private synchronized void readObject(final java.io.ObjectInputStream in)
188 			throws IOException
189 	{
190 		try
191 		{
192 			in.defaultReadObject();
193 		} catch (Exception exception)
194 		{
195 			throw new IOException(exception.getMessage());
196 		}
197 	}
198 
199 	/***
200 	 * A private implementation of the locatable interface.
201 	 * <p>
202 	 * (c) copyright 2005 <a href="http://www.simulation.tudelft.nl">Delft
203 	 * University of Technology </a>, the Netherlands. <br>
204 	 * See for project information <a
205 	 * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
206 	 * 
207 	 * Copyright (c) 2003-2005 Delft University of Technology, Jaffalaan 5, 2628 BX
208 	 * Delft, the Netherlands. All rights reserved.
209 	 * 
210 	 * See for project information <a href="http://www.simulation.tudelft.nl/">
211 	 * www.simulation.tudelft.nl</a>.
212 	 * 
213 	 * The source code and binary code of this software are proprietary information 
214 	 * of Delft University of Technology.
215 	 *
216 	 * @author <a href="http://www.tbm.tudelft.nl/webstaf/stijnh/index.htm">Stijn-Pieter van Houten</a>
217 	 * @version $Revision: 1.1 $ $Date: 2005/06/16 12:34:02 $
218 	 * @since 1.1.3
219 	 */
220 	protected class Locatable implements LocatableInterface, Serializable
221 	{
222 		/*** the serial version uid */
223 		private static final long serialVersionUID = 11L;
224 
225 		/*** the location */
226 		private DirectedPoint location = null;
227 
228 		/*** the bounds */
229 		private transient BoundingBox bounds = null;
230 
231 		/***
232 		 * constructs a new Locatable
233 		 * 
234 		 * @param location the location
235 		 * @param bounds the bounds
236 		 */
237 		public Locatable(final DirectedPoint location, final BoundingBox bounds)
238 		{
239 			super();
240 			this.location = location;
241 			this.bounds = bounds;
242 		}
243 
244 		/***
245 		 * @see nl.tudelft.simulation.dsol.animation.LocatableInterface#getLocation()
246 		 */
247 		public DirectedPoint getLocation() throws RemoteException
248 		{
249 			return this.location;
250 		}
251 
252 		/***
253 		 * @see nl.tudelft.simulation.dsol.animation.LocatableInterface#getBounds()
254 		 */
255 		public Bounds getBounds() throws RemoteException
256 		{
257 			return this.bounds;
258 		}
259 
260 		//
261 		// private methods
262 		//
263 		/***
264 		 * writes a serializable method to stream
265 		 * 
266 		 * @param out the outputstream
267 		 * @throws IOException on IOException
268 		 */
269 		private synchronized void writeObject(final ObjectOutputStream out)
270 				throws IOException
271 		{
272 			out.defaultWriteObject();
273 		}
274 
275 		/***
276 		 * reads a serializable method from stream
277 		 * 
278 		 * @param in the inputstream
279 		 */
280 		private synchronized void readObject(final java.io.ObjectInputStream in)
281 		{
282 			try
283 			{
284 				in.defaultReadObject();
285 				// custome deserialize methods
286 				this.bounds = new BoundingBox(0.0, 0.0, 0.0);
287 			} catch (Exception exception)
288 			{
289 				exception.printStackTrace();
290 			}
291 		}
292 	}
293 }