View Javadoc

1   /*
2    * @(#) DistributedGisActorSingleImageRenderable.java Dec 09, 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.Color;
17  import java.awt.Dimension;
18  import java.awt.Graphics2D;
19  import java.awt.MediaTracker;
20  import java.awt.geom.Point2D;
21  import java.awt.geom.Rectangle2D;
22  import java.awt.image.ImageObserver;
23  import java.net.URL;
24  import java.rmi.RemoteException;
25  
26  import nl.tudelft.simulation.dsol.animation.LocatableInterface;
27  import nl.tudelft.simulation.dsol.animation.D2.Renderable2DInterface;
28  import nl.tudelft.simulation.dsol.gui.animation2D.GridPanel;
29  import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
30  import nl.tudelft.simulation.language.d2.Shape;
31  import nl.tudelft.simulation.language.d3.BoundingBox;
32  import nl.tudelft.simulation.language.d3.BoundsUtil;
33  import nl.tudelft.simulation.language.d3.DirectedPoint;
34  import nl.tudelft.simulation.logger.Logger;
35  
36  /***
37   * The DistributedGisActorSingleImageRenderable is used to show a single image
38   * renderable on a remote animation panel. How and if the image is displayed
39   * depends on the zoom level of the 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
52   *         href="http://www.tbm.tudelft.nl/webstaf/stijnh/index.htm">Stijn-Pieter
53   *         van Houten </a>
54   * @version $Revision: 1.4 $ $Date: 2005/08/10 11:23:30 $
55   * @since 1.0.0
56   */
57  public class DistributedGisActorSingleImageRenderable extends
58  		DistributedSingleImageRenderable implements
59  		DistributedRenderable2DInterface
60  {
61  	/*** the serial version uid */
62  	private static final long serialVersionUID = 11L;
63  
64  	/*** the classname of the actor */
65  	private String actorClassName = null;
66  
67  	/*** the name of the actor */
68  	private String name = null;
69  
70  	/***
71  	 * constructs a new DistributedGisActorSingleImageRenderable
72  	 * 
73  	 * @param staticLocation the location
74  	 * @param size the size of the image
75  	 * @param simulator the simulator to use
76  	 * @param image the image name
77  	 * @param actorClassName the actor class name
78  	 * @param name the actor name
79  	 */
80  	public DistributedGisActorSingleImageRenderable(
81  			final DirectedPoint staticLocation, final Dimension size,
82  			final SimulatorInterface simulator, final URL image,
83  			final String actorClassName, final String name)
84  	{
85  		super(staticLocation, size, simulator, image);
86  		this.actorClassName = actorClassName;
87  		this.name = name;
88  	}
89  
90  	/***
91  	 * constructs a new DistributedGisActorSingleImageRenderable
92  	 * 
93  	 * @param source the source
94  	 * @param simulator the simulator to use
95  	 * @param image the image name
96  	 * @param actorClassName the actor class name
97  	 * @param name the actor name
98  	 */
99  	public DistributedGisActorSingleImageRenderable(
100 			final LocatableInterface source,
101 			final SimulatorInterface simulator, final URL image,
102 			final String actorClassName, final String name)
103 	{
104 		super(source, simulator, image);
105 		this.actorClassName = actorClassName;
106 		this.name = name;
107 
108 	}
109 
110 	/***
111 	 * constructs a new DistributedGisActorSingleImageRenderable
112 	 * 
113 	 * @param staticLocation the location
114 	 * @param size the size of the image
115 	 * @param simulator the simulator to use
116 	 * @param image the image name
117 	 * @param actorClassName the actor class name
118 	 * @param name the actor name
119 	 */
120 	public DistributedGisActorSingleImageRenderable(
121 			final Point2D staticLocation, final Dimension size,
122 			final SimulatorInterface simulator, final URL image,
123 			final String actorClassName, final String name)
124 	{
125 		super(staticLocation, size, simulator, image);
126 		this.actorClassName = actorClassName;
127 		this.name = name;
128 	}
129 
130 	/***
131 	 * @see nl.tudelft.simulation.dsol.animation.D2.Renderable2DInterface#paint(java.awt.Graphics2D,
132 	 *      java.awt.geom.Rectangle2D, java.awt.Dimension,
133 	 *      java.awt.image.ImageObserver)
134 	 */
135 	public synchronized void paint(final Graphics2D graphics,
136 			final Rectangle2D rectangle, final Dimension dimension,
137 			final ImageObserver observer)
138 	{
139 		GridPanel animationPanel = (GridPanel) observer;
140 
141 		double scale = Renderable2DInterface.Util.getScale(animationPanel
142 				.getExtent(), animationPanel.getSize());
143 		Point2D screenLocation = null;
144 		try
145 		{
146 			screenLocation = Renderable2DInterface.Util.getScreenCoordinates(
147 					super.source.getLocation().to2D(), animationPanel
148 							.getExtent(), animationPanel.getSize());
149 		} catch (RemoteException remoteException)
150 		{
151 			Logger.severe(this, "paint", remoteException);
152 		}
153 
154 		// we only draw our images if there is a certain zoom level
155 		if (scale < 0.05063)
156 		{
157 			super.setScale(false); // note: setScale(true) is linked to
158 			this.customPaint(graphics, rectangle, dimension, observer,
159 					new BoundingBox(30.0, 30.0, 1.0));
160 			// and write the name of the actor on the screen
161 			graphics.setColor(Color.BLACK);
162 			graphics.drawString(this.name, (int) screenLocation.getX(),
163 					(int) screenLocation.getY() + 25);
164 		} else if (scale < 0.111)
165 		{
166 			super.setScale(false); // note: setScale(true) is linked to
167 			this.customPaint(graphics, rectangle, dimension, observer,
168 					new BoundingBox(20.0, 20.0, 1.0));
169 
170 			// and write the name of the actor on the screen
171 			graphics.setColor(Color.BLACK);
172 			graphics.drawString(this.name, (int) screenLocation.getX() + 10,
173 					(int) screenLocation.getY());
174 
175 		} else
176 		{
177 			// lookup the kind of actor
178 			if (this.actorClassName
179 					.equalsIgnoreCase("org.gscg.gameactors.GameSupplier"))
180 			{
181 				graphics.setColor(Color.YELLOW);
182 			} else if (this.actorClassName
183 					.equalsIgnoreCase("org.gscg.gameactors.GameManufacturer")
184 					|| this.actorClassName
185 							.equalsIgnoreCase("org.gscg.gameactors.GameManufacturerInteractive"))
186 			{
187 				graphics.setColor(Color.BLUE);
188 			} else if (this.actorClassName
189 					.equalsIgnoreCase("org.gscg.gameactors.GameDistributorInteractive")
190 					|| this.actorClassName
191 							.equalsIgnoreCase("org.gscg.gameactors.GameDistributor"))
192 			{
193 				graphics.setColor(Color.RED);
194 			} else if (this.actorClassName
195 					.equalsIgnoreCase("org.gscg.gameactors.GameMarket"))
196 			{
197 				graphics.setColor(Color.GREEN);
198 			} else
199 			// undefined actor..
200 			{
201 				graphics.setColor(Color.BLACK);
202 				Logger.severe(this, "paint",
203 						"We don't know what color to choose for actor class: "
204 								+ super.source.getClass());
205 			}
206 
207 			// we draw a circle
208 			graphics.fillOval((int) screenLocation.getX(), (int) screenLocation
209 					.getY(), 8, 8);
210 		}
211 	}
212 
213 	//
214 	// private methods
215 	//
216 
217 	/***
218 	 * CustomPaint method.
219 	 * 
220 	 * @param graphics the graphics
221 	 * @param extent the extent
222 	 * @param screen the screen
223 	 * @param observer the observer
224 	 * @param boundingBox the boundingbox to use
225 	 */
226 	private synchronized void customPaint(final Graphics2D graphics,
227 			final Rectangle2D extent, final Dimension screen,
228 			final ImageObserver observer, final BoundingBox boundingBox)
229 	{
230 		try
231 		{
232 			DirectedPoint location = this.source.getLocation();
233 			Rectangle2D rectangle = BoundsUtil.getIntersect(this.source
234 					.getLocation(), boundingBox, location.z);
235 			if (!Shape.overlaps(extent, rectangle) && this.translate)
236 			{
237 				return;
238 			}
239 			Point2D screenCoordinates = Renderable2DInterface.Util
240 					.getScreenCoordinates(this.source.getLocation().to2D(),
241 							extent, screen);
242 			// Let's transform
243 			if (this.translate)
244 			{
245 				graphics.translate(screenCoordinates.getX(), screenCoordinates
246 						.getY());
247 			}
248 			double scale = Renderable2DInterface.Util.getScale(extent, screen);
249 			if (this.scale)
250 			{
251 				graphics.scale(1.0 / scale, 1.0 / scale);
252 			}
253 			double angle = -location.getRotZ();
254 			if (this.flip && angle > Math.PI)
255 			{
256 				angle = angle - Math.PI;
257 			}
258 			if (this.rotate && angle != 0.0)
259 			{
260 				graphics.rotate(angle);
261 			}
262 			// Now we paint
263 			this.customPaint(graphics, observer, boundingBox);
264 			// Let's untransform
265 			if (this.rotate && angle != 0.0)
266 			{
267 				graphics.rotate(-angle);
268 			}
269 			if (this.scale)
270 			{
271 				graphics.scale(scale, scale);
272 			}
273 			if (this.translate)
274 			{
275 				graphics.translate(-screenCoordinates.getX(),
276 						-screenCoordinates.getY());
277 			}
278 		} catch (Exception exception)
279 		{
280 			Logger.warning(this, "paint", exception);
281 		}
282 	}
283 
284 	/***
285 	 * @param graphics the graphics
286 	 * @param observer the observer
287 	 * @param boundingBox the boundingbox
288 	 * @throws RemoteException thrown in case of a network error
289 	 */
290 	private void customPaint(final Graphics2D graphics,
291 			final ImageObserver observer, final BoundingBox boundingBox)
292 			throws RemoteException
293 	{
294 		int image = this.selectImage();
295 		if (this.images[image].getImageLoadStatus() != MediaTracker.COMPLETE)
296 		{
297 			return;
298 		}
299 		Dimension size = BoundsUtil.getIntersect(this.source.getLocation(),
300 				boundingBox, this.source.getLocation().z).getBounds().getSize();
301 		Point2D origin = this.resolveOrigin(this.orientation, size);
302 		graphics.translate(origin.getX(), origin.getY());
303 		graphics.scale(0.001, 0.001);
304 		graphics.drawImage(this.images[image].getImage(), 0, 0,
305 				(int) (1000 * size.getWidth()),
306 				(int) (1000 * size.getHeight()), observer);
307 		graphics.scale(1000, 1000);
308 		graphics.translate(-origin.getX(), -origin.getY());
309 	}
310 }