View Javadoc

1   /*
2    * @(#) GisRenderable2D.java Apr 29, 2004
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.BufferedImage;
17  import java.awt.image.ImageObserver;
18  import java.net.URL;
19  
20  import javax.media.j3d.Bounds;
21  
22  import nl.javel.gisbeans.map.MapInterface;
23  import nl.javel.gisbeans.map.mapfile.MapFileXMLParser;
24  import nl.tudelft.simulation.dsol.animation.LocatableInterface;
25  import nl.tudelft.simulation.dsol.context.ContextUtil;
26  import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
27  import nl.tudelft.simulation.language.d3.BoundingBox;
28  import nl.tudelft.simulation.language.d3.DirectedPoint;
29  import nl.tudelft.simulation.language.d3.CartesianPoint;
30  import nl.tudelft.simulation.logger.Logger;
31  
32  /***
33   * This renderable draws CAD/GIS objects.
34   * <p>
35   * (c) copyright 2003 <a href="http://www.simulation.tudelft.nl">Delft
36   * University of Technology </a>, the Netherlands. <br>
37   * See for project information <a
38   * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
39   * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General Public
40   * License (GPL) </a>, no warranty <br>
41   * 
42   * @author <a href="http://www.tbm.tudelft.nl/webstaf/peterja/index.htm">Peter
43   *         Jacobs </a>
44   * @version 1.2 Apr 29, 2004
45   * @since 1.4
46   */
47  public class GisRenderable2D implements Renderable2DInterface,
48  		LocatableInterface
49  {
50  	/*** the map to display */
51  	protected MapInterface map = null;
52  
53  	/*** the image cached image */
54  	protected BufferedImage image = null;
55  
56  	/*** the cached extent */
57  	protected Rectangle2D extent = new Rectangle2D.Double();
58  
59  	/*** the cached screenSize */
60  	protected Dimension screenSize = new Dimension();
61  
62  	/*** the location of the image */
63  	protected DirectedPoint location = null;
64  
65  	/*** the bounds of the image */
66  	protected Bounds bounds = null;
67  
68  	/***
69  	 * constructs a new GisRenderable2D
70  	 * 
71  	 * @param simulator the simulator.
72  	 * @param mapFile the mapfile to use.
73  	 */
74  	public GisRenderable2D(final SimulatorInterface simulator, final URL mapFile)
75  	{
76  		super();
77  		try
78  		{
79  			this.map = MapFileXMLParser.parseMapFile(mapFile);
80  			this.location = new DirectedPoint(new CartesianPoint(this.extent.getCenterX(),
81  					this.extent.getCenterY(), -Double.MIN_VALUE));
82  			this.bounds = new BoundingBox(this.extent.getWidth(), this.extent
83  					.getHeight(), 0.0);
84  			simulator.getReplication().getRunControl().getTreatment()
85  					.getProperties().put("animationPanel.extent",
86  							this.map.getExtent());
87  			ContextUtil.bindToContext(simulator, "/animation/2D", this);
88  		} catch (Exception exception)
89  		{
90  			Logger.warning(this, "<init>", exception);
91  		}
92  	}
93  
94  	/***
95  	 * @see nl.tudelft.simulation.dsol.animation.D2.Renderable2DInterface
96  	 *      #paint(java.awt.Graphics2D, java.awt.geom.Rectangle2D,
97  	 *      java.awt.Dimension, java.awt.image.ImageObserver)
98  	 */
99  	public void paint(final Graphics2D graphics, final Rectangle2D extent,
100 			final Dimension screen, final ImageObserver observer)
101 	{
102 		try
103 		{
104 			//is the extent or the screensize still the same
105 			if (extent.equals(this.extent) && screen.equals(this.screenSize))
106 			{
107 				graphics.drawImage(this.image, 0, 0, null);
108 				return;
109 			}
110 			this.map.setExtent((Rectangle2D) extent.clone());
111 			this.map.getImage().setSize(screen);
112 			this.cacheImage();
113 			this.paint(graphics, extent, screen, observer);
114 		} catch (Exception exception)
115 		{
116 			Logger.warning(this, "paint", exception);
117 		}
118 	}
119 
120 	/***
121 	 * @see nl.tudelft.simulation.dsol.animation.D2.Renderable2DInterface#getSource()
122 	 */
123 	public LocatableInterface getSource()
124 	{
125 		return this;
126 	}
127 
128 	/***
129 	 * @see nl.tudelft.simulation.dsol.animation.LocatableInterface#getBounds()
130 	 */
131 	public Bounds getBounds()
132 	{
133 		return this.bounds;
134 	}
135 
136 	/***
137 	 * @see nl.tudelft.simulation.dsol.animation.LocatableInterface#getLocation()
138 	 */
139 	public DirectedPoint getLocation()
140 	{
141 		return this.location;
142 	}
143 
144 	/***
145 	 * caches the GIS map by creating an image. This prevents continuous
146 	 * rendering.
147 	 * 
148 	 * @throws Exception on graphicsProblems and network connection failures.
149 	 */
150 	private void cacheImage() throws Exception
151 	{
152 		this.image = new BufferedImage((int) this.map.getImage().getSize()
153 				.getWidth(), (int) this.map.getImage().getSize().getHeight(),
154 				BufferedImage.TYPE_4BYTE_ABGR);
155 		Graphics2D bg = this.image.createGraphics();
156 		this.map.drawMap(bg);
157 		bg.dispose();
158 		this.screenSize = (Dimension) this.map.getImage().getSize().clone();
159 		this.extent = this.map.getExtent();
160 		this.location = new DirectedPoint(new CartesianPoint(this.extent.getCenterX(),
161 				this.extent.getCenterY(), -Double.MIN_VALUE));
162 		this.bounds = new BoundingBox(this.extent.getWidth(), this.extent
163 				.getHeight(), 0.0);
164 	}
165 
166 	/***
167 	 * destroys an RenderableObject by unsubscribing it from the context.
168 	 */
169 	public void destroy()
170 	{
171 		try
172 		{
173 			nl.tudelft.simulation.naming.context.ContextUtil
174 					.unbindFromContext(this);
175 		} catch (Throwable throwable)
176 		{
177 			Logger.warning(this, "finalize", throwable);
178 		}
179 	}
180 
181 	/***
182 	 * @see nl.tudelft.simulation.dsol.animation.D2.Renderable2DInterface#contains(java.awt.geom.Point2D,
183 	 *      java.awt.geom.Rectangle2D, java.awt.Dimension)
184 	 */
185 	public boolean contains(final Point2D pointWorldCoordinates,
186 			final Rectangle2D extent, final Dimension screen)
187 	{
188 		return false;
189 	}
190 }