View Javadoc

1   /*
2    * @(#) DistributedGisRenderable2D.java Apr 29, 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.BufferedImage;
21  import java.awt.image.ImageObserver;
22  import java.io.IOException;
23  import java.io.ObjectOutputStream;
24  import java.net.URL;
25  import java.rmi.RemoteException;
26  
27  import javax.media.j3d.Bounds;
28  
29  import nl.javel.gisbeans.map.MapInterface;
30  import nl.javel.gisbeans.map.mapfile.MapFileXMLParser;
31  import nl.tudelft.simulation.dsol.animation.LocatableInterface;
32  import nl.tudelft.simulation.language.d3.BoundingBox;
33  import nl.tudelft.simulation.language.d3.CartesianPoint;
34  import nl.tudelft.simulation.language.d3.DirectedPoint;
35  import nl.tudelft.simulation.logger.Logger;
36  
37  /***
38   * This renderable draws CAD/GIS objects. This distributed version is made to
39   * show these kind of objects on a remote animation panel.
40   * <p>
41   * 
42   * Copyright (c) 2003-2005 Delft University of Technology, Jaffalaan 5, 2628 BX
43   * Delft, the Netherlands. All rights reserved.
44   * 
45   * See for project information <a href="http://www.simulation.tudelft.nl/">
46   * www.simulation.tudelft.nl </a>.
47   * 
48   * The source code and binary code of this software is proprietary information
49   * of Delft University of Technology.
50   * 
51   * @author <a href="http://www.tbm.tudelft.nl/webstaf/peterja/index.htm">Peter
52   *         Jacobs </a>
53   * @version $Revision: 1.1 $ $Date: 2005/06/16 12:34:02 $
54   * @since 1.0.8
55   */
56  public class DistributedGisRenderable2D implements
57  		DistributedRenderable2DInterface, LocatableInterface
58  {
59  	/*** the serial version uid */
60  	private static final long serialVersionUID = 11L;
61  
62  	/*** the map to display */
63  	private MapInterface map = null;
64  
65  	/*** the image cached image */
66  	private BufferedImage image = null;
67  
68  	/*** the cached extent */
69  	private Rectangle2D extent = new Rectangle2D.Double();
70  
71  	/*** the cached screenSize */
72  	private Dimension screenSize = new Dimension();
73  
74  	/*** the location of the image */
75  	private DirectedPoint location = null;
76  
77  	/*** the bounds of the image */
78  	private transient Bounds bounds = null;
79  
80  	/*** the url */
81  	private URL url = null;
82  
83  	/***
84  	 * constructs a new GisRenderable2D
85  	 * 
86  	 * @param mapFile the mapfile to use.
87  	 */
88  	public DistributedGisRenderable2D(final URL mapFile)
89  	{
90  		super();
91  		this.url = mapFile;
92  		try
93  		{
94  			this.map = MapFileXMLParser.parseMapFile(mapFile);
95  			this.location = new DirectedPoint(new CartesianPoint(this.extent
96  					.getCenterX(), this.extent.getCenterY(), -Double.MIN_VALUE));
97  			this.bounds = new BoundingBox(this.extent.getWidth(), this.extent
98  					.getHeight(), 0.0);
99  		} catch (Exception exception)
100 		{
101 			Logger.warning(this, "<init>", exception);
102 		}
103 	}
104 
105 	/***
106 	 * @see nl.tudelft.simulation.dsol.animation.D2.Renderable2DInterface
107 	 *      #paint(java.awt.Graphics2D, java.awt.geom.Rectangle2D,
108 	 *      java.awt.Dimension, java.awt.image.ImageObserver)
109 	 */
110 	public void paint(final Graphics2D graphics, final Rectangle2D extent,
111 			final Dimension screen, final ImageObserver observer)
112 	{
113 		try
114 		{
115 			// is the extent or the screensize still the same
116 			if (extent.equals(this.extent) && screen.equals(this.screenSize))
117 			{
118 				graphics.drawImage(this.image, 0, 0, null);
119 				return;
120 			}
121 			this.map.setExtent((Rectangle2D) extent.clone());
122 			this.map.getImage().setSize(screen);
123 			this.cacheImage();
124 			this.paint(graphics, extent, screen, observer);
125 		} catch (Exception exception)
126 		{
127 			Logger.warning(this, "paint", exception);
128 		}
129 	}
130 
131 	/***
132 	 * @see nl.tudelft.simulation.dsol.animation.D2.Renderable2DInterface#getSource()
133 	 */
134 	public LocatableInterface getSource()
135 	{
136 		return this;
137 	}
138 
139 	/***
140 	 * @see nl.tudelft.simulation.dsol.animation.LocatableInterface#getBounds()
141 	 */
142 	public Bounds getBounds()
143 	{
144 		return this.bounds;
145 	}
146 
147 	/***
148 	 * @see nl.tudelft.simulation.dsol.animation.LocatableInterface#getLocation()
149 	 */
150 	public DirectedPoint getLocation()
151 	{
152 		return this.location;
153 	}
154 
155 	/***
156 	 * caches the GIS map by creating an image. This prevents continuous
157 	 * rendering.
158 	 * 
159 	 * @throws Exception on graphicsProblems and network connection failures.
160 	 */
161 	private void cacheImage() throws Exception
162 	{
163 		this.image = new BufferedImage((int) this.map.getImage().getSize()
164 				.getWidth(), (int) this.map.getImage().getSize().getHeight(),
165 				BufferedImage.TYPE_4BYTE_ABGR);
166 		Graphics2D bg = this.image.createGraphics();
167 		this.map.drawMap(bg);
168 		bg.dispose();
169 		this.screenSize = (Dimension) this.map.getImage().getSize().clone();
170 		this.extent = this.map.getExtent();
171 		this.location = new DirectedPoint(new CartesianPoint(this.extent
172 				.getCenterX(), this.extent.getCenterY(), -Double.MIN_VALUE));
173 		this.bounds = new BoundingBox(this.extent.getWidth(), this.extent
174 				.getHeight(), 0.0);
175 	}
176 
177 	/***
178 	 * destroys an RenderableObject by unsubscribing it from the context.
179 	 */
180 	public void destroy()
181 	{
182 		try
183 		{
184 			nl.tudelft.simulation.naming.context.ContextUtil
185 					.unbindFromContext(this);
186 		} catch (Throwable throwable)
187 		{
188 			Logger.warning(this, "finalize", throwable);
189 		}
190 	}
191 
192 	/***
193 	 * @see nl.tudelft.simulation.dsol.animation.D2.Renderable2DInterface#contains(java.awt.geom.Point2D,
194 	 *      java.awt.geom.Rectangle2D, java.awt.Dimension)
195 	 */
196 	public boolean contains(final Point2D pointWorldCoordinates,
197 			final Rectangle2D extent, final Dimension screen)
198 	{
199 		return false;
200 	}
201 
202 	/***
203 	 * @see org.gscg.gameleader.animation2D.DistributedRenderable2DInterface#setLocation(nl.tudelft.simulation.language.d3.DirectedPoint)
204 	 */
205 	public void setLocation(final DirectedPoint location)
206 			throws RemoteException
207 	{
208 		this.location = location;
209 	}
210 
211 
212 	//
213 	// private methods
214 	//
215 	/***
216 	 * writes a serializable method to stream
217 	 * 
218 	 * @param out the outputstream
219 	 * @throws IOException on IOException
220 	 */
221 	private synchronized void writeObject(final ObjectOutputStream out)
222 			throws IOException
223 	{
224 		out.writeObject(this.url);
225 	}
226 
227 	/***
228 	 * reads a serializable method from stream
229 	 * 
230 	 * @param in the inputstream
231 	 */
232 	private synchronized void readObject(final java.io.ObjectInputStream in)
233 	{
234 		try
235 		{
236 			this.url = (URL) in.readObject();
237 			this.map = MapFileXMLParser.parseMapFile(this.url);
238 			this.location = new DirectedPoint(new CartesianPoint(this.extent
239 					.getCenterX(), this.extent.getCenterY(), -Double.MIN_VALUE));
240 			this.bounds = new BoundingBox(this.extent.getWidth(), this.extent
241 					.getHeight(), 0.0);
242 		} catch (Exception exception)
243 		{
244 			Logger.severe(this, "readObject", exception);
245 		}
246 	}
247 }