View Javadoc

1   /*
2    * @(#) DistributedGisContentSingleImageRenderable.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.Dimension;
17  import java.awt.Graphics2D;
18  import java.awt.MediaTracker;
19  import java.awt.geom.Point2D;
20  import java.awt.geom.Rectangle2D;
21  import java.awt.image.ImageObserver;
22  import java.net.URL;
23  import java.rmi.RemoteException;
24  
25  import nl.tudelft.simulation.dsol.animation.LocatableInterface;
26  import nl.tudelft.simulation.dsol.animation.D2.Renderable2DInterface;
27  import nl.tudelft.simulation.dsol.gui.animation2D.GridPanel;
28  import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
29  import nl.tudelft.simulation.language.d2.Shape;
30  import nl.tudelft.simulation.language.d3.BoundingBox;
31  import nl.tudelft.simulation.language.d3.BoundsUtil;
32  import nl.tudelft.simulation.language.d3.DirectedPoint;
33  import nl.tudelft.simulation.logger.Logger;
34  
35  /***
36   * The DistributedGisContentSingleImageRenderable is used to show the contents
37   * of a single image renderable on a remote animation panel. How and if the
38   * image is displayed depends on the zoom level of the animation panel.
39   * <p>
40   * 
41   * Copyright (c) 2003-2005 Delft University of Technology, Jaffalaan 5, 2628 BX
42   * Delft, the Netherlands. All rights reserved.
43   * 
44   * See for project information <a href="http://www.simulation.tudelft.nl/">
45   * www.simulation.tudelft.nl </a>.
46   * 
47   * The source code and binary code of this software is proprietary information
48   * of Delft University of Technology.
49   * 
50   * @author <a
51   *         href="http://www.tbm.tudelft.nl/webstaf/stijnh/index.htm">Stijn-Pieter
52   *         van Houten </a>
53   * @version $Revision: 1.1 $ $Date: 2005/06/16 12:34:02 $
54   * @since 1.0.8
55   */
56  public class DistributedGisContentSingleImageRenderable extends
57  		DistributedSingleImageRenderable
58  {
59  	/*** the serial version uid */
60  	private static final long serialVersionUID = 11L;
61  
62  	/***
63  	 * constructs a new DistributedGisActorSingleImageRenderable
64  	 * 
65  	 * @param staticLocation the location
66  	 * @param size the size of the image
67  	 * @param simulator the simulator to use
68  	 * @param image the image name
69  	 */
70  	public DistributedGisContentSingleImageRenderable(
71  			final DirectedPoint staticLocation, final Dimension size,
72  			final SimulatorInterface simulator, final URL image)
73  	{
74  		super(staticLocation, size, simulator, image);
75  	}
76  
77  	/***
78  	 * constructs a new DistributedGisActorSingleImageRenderable
79  	 * 
80  	 * @param source the source
81  	 * @param simulator the simulator to use
82  	 * @param image the image name
83  	 */
84  	public DistributedGisContentSingleImageRenderable(
85  			final LocatableInterface source,
86  			final SimulatorInterface simulator, final URL image)
87  	{
88  		super(source, simulator, image);
89  	}
90  
91  	/***
92  	 * constructs a new DistributedGisActorSingleImageRenderable
93  	 * 
94  	 * @param staticLocation the location
95  	 * @param size the size of the image
96  	 * @param simulator the simulator to use
97  	 * @param image the image name
98  	 */
99  	public DistributedGisContentSingleImageRenderable(
100 			final Point2D staticLocation, final Dimension size,
101 			final SimulatorInterface simulator, final URL image)
102 	{
103 		super(staticLocation, size, simulator, image);
104 	}
105 
106 
107 	/***
108 	 * @see nl.tudelft.simulation.dsol.animation.D2.Renderable2DInterface#paint(java.awt.Graphics2D,
109 	 *      java.awt.geom.Rectangle2D, java.awt.Dimension,
110 	 *      java.awt.image.ImageObserver)
111 	 */
112 	public synchronized void paint(final Graphics2D graphics,
113 			final Rectangle2D rectangle, final Dimension dimension,
114 			final ImageObserver observer)
115 	{
116 		GridPanel animationPanel = (GridPanel) observer;
117 		double scale = Renderable2DInterface.Util.getScale(animationPanel
118 				.getExtent(), animationPanel.getSize());
119 
120 		// we only draw our images if their is a certain zoom level
121 		if (scale < /* 0.05063 */0.111)
122 		{
123 			super.setScale(false); // note: setScale(true) is linked to
124 			// the implementation of the getBounds() method of ContentAnimation
125 			this.customPaint(graphics, rectangle, dimension, observer,
126 					new BoundingBox(20, 20, 1.0));
127 		}
128 	}
129 
130 	//
131 	// private methods
132 	//
133 
134 	/***
135 	 * CustomPaint method.
136 	 * 
137 	 * @param graphics the graphics
138 	 * @param extent the extent
139 	 * @param screen the screen
140 	 * @param observer the observer
141 	 * @param boundingBox the boundingbox to use
142 	 */
143 	private synchronized void customPaint(final Graphics2D graphics,
144 			final Rectangle2D extent, final Dimension screen,
145 			final ImageObserver observer, final BoundingBox boundingBox)
146 	{
147 		try
148 		{
149 			DirectedPoint location = this.source.getLocation();
150 			Rectangle2D rectangle = BoundsUtil.getIntersect(this.source
151 					.getLocation(), boundingBox, location.z);
152 			if (!Shape.overlaps(extent, rectangle) && this.translate)
153 			{
154 				return;
155 			}
156 			Point2D screenCoordinates = Renderable2DInterface.Util
157 					.getScreenCoordinates(this.source.getLocation().to2D(),
158 							extent, screen);
159 			// Let's transform
160 			if (this.translate)
161 			{
162 				graphics.translate(screenCoordinates.getX(), screenCoordinates
163 						.getY());
164 			}
165 			double scale = Renderable2DInterface.Util.getScale(extent, screen);
166 			if (this.scale)
167 			{
168 				graphics.scale(1.0 / scale, 1.0 / scale);
169 			}
170 			double angle = -location.getRotZ();
171 			if (this.flip && angle > Math.PI)
172 			{
173 				angle = angle - Math.PI;
174 			}
175 			if (this.rotate && angle != 0.0)
176 			{
177 				graphics.rotate(angle);
178 			}
179 			// Now we paint
180 			this.customPaint(graphics, observer, boundingBox);
181 			// Let's untransform
182 			if (this.rotate && angle != 0.0)
183 			{
184 				graphics.rotate(-angle);
185 			}
186 			if (this.scale)
187 			{
188 				graphics.scale(scale, scale);
189 			}
190 			if (this.translate)
191 			{
192 				graphics.translate(-screenCoordinates.getX(),
193 						-screenCoordinates.getY());
194 			}
195 		} catch (Exception exception)
196 		{
197 			Logger.warning(this, "paint", exception);
198 		}
199 	}
200 
201 	/***
202 	 * @param graphics the graphics
203 	 * @param observer the observer
204 	 * @param boundingBox the boundingbox
205 	 * @throws RemoteException thrown in case of a network error
206 	 */
207 	private void customPaint(final Graphics2D graphics,
208 			final ImageObserver observer, final BoundingBox boundingBox)
209 			throws RemoteException
210 	{
211 		int image = this.selectImage();
212 		if (this.images[image].getImageLoadStatus() != MediaTracker.COMPLETE)
213 		{
214 			return;
215 		}
216 		Dimension size = BoundsUtil.getIntersect(this.source.getLocation(),
217 				boundingBox, this.source.getLocation().z).getBounds().getSize();
218 		Point2D origin = this.resolveOrigin(this.orientation, size);
219 		graphics.translate(origin.getX(), origin.getY());
220 		graphics.scale(0.001, 0.001);
221 		graphics.drawImage(this.images[image].getImage(), 0, 0,
222 				(int) (1000 * size.getWidth()),
223 				(int) (1000 * size.getHeight()), observer);
224 		graphics.scale(1000, 1000);
225 		graphics.translate(-origin.getX(), -origin.getY());
226 	}
227 }