View Javadoc

1   /*
2    * @(#) ImageRenderable.java Apr 30, 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.MediaTracker;
15  import java.awt.geom.Point2D;
16  import java.awt.image.ImageObserver;
17  import java.net.URL;
18  import java.rmi.RemoteException;
19  import java.util.HashMap;
20  import java.util.Map;
21  
22  import javax.swing.ImageIcon;
23  
24  import nl.tudelft.simulation.dsol.animation.LocatableInterface;
25  import nl.tudelft.simulation.dsol.animation.StaticLocation;
26  import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
27  import nl.tudelft.simulation.language.d3.BoundingBox;
28  import nl.tudelft.simulation.language.d3.BoundsUtil;
29  import nl.tudelft.simulation.language.d3.DirectedPoint;
30  
31  /***
32   * An abstract class for state-dependent image renderables.
33   * <p>
34   * (c) copyright 2003 <a href="http://www.simulation.tudelft.nl">Delft
35   * University of Technology </a>, the Netherlands. <br>
36   * See for project information <a
37   * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
38   * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General Public
39   * License (GPL) </a>, no warranty <br>
40   * 
41   * @author <a href="http://www.tbm.tudelft.nl/webstaf/peterja/index.htm">Peter
42   *         Jacobs </a>
43   * @version 1.2 Apr 30, 2004
44   * @since 1.4
45   */
46  public abstract class ImageRenderable extends Renderable2D
47  {
48  	/*** the cache of images */
49  	private static Map cache = new HashMap();
50  
51  	/*** LEFT-BOTTOM location */
52  	public static final short LB = -4;
53  
54  	/*** CENTER-BOTTOM location */
55  	public static final short CB = -3;
56  
57  	/*** RIGHT-BOTTOM location */
58  	public static final short RB = -2;
59  
60  	/*** LEFT-CENTER location */
61  	public static final short LC = -1;
62  
63  	/*** CENTER-CENTER location */
64  	public static final short CC = 0;
65  
66  	/*** RIGHT-CENTER location */
67  	public static final short RC = 1;
68  
69  	/*** LEFT-TOP location */
70  	public static final short LT = 2;
71  
72  	/*** CENTER-TOP location */
73  	public static final short CT = 3;
74  
75  	/*** RIGHT-TOP location */
76  	public static final short RT = 4;
77  
78  	/*** the images to be used */
79  	protected ImageIcon[] images = null;
80  
81  	/*** the origin of the image */
82  	protected short orientation = ImageRenderable.CC;
83  
84  	/***
85  	 * constructs a new ImageRenderable
86  	 * 
87  	 * @param source the source to be animated.
88  	 * @param simulator the simulator to be used.
89  	 * @param images the image urls.
90  	 */
91  	public ImageRenderable(final LocatableInterface source,
92  			final SimulatorInterface simulator, final URL[] images)
93  	{
94  		super(source, simulator);
95  		this.images = new ImageIcon[images.length];
96  		for (int i = 0; i < images.length; i++)
97  		{
98  			if (ImageRenderable.cache.containsKey(images[i]))
99  			{
100 				this.images[i] = (ImageIcon) ImageRenderable.cache
101 						.get(images[i]);
102 			} else
103 			{
104 				this.images[i] = new ImageIcon(images[i]);
105 				ImageRenderable.cache.put(images[i], this.images[i]);
106 			}
107 		}
108 		this.setOrientation(ImageRenderable.CC);
109 	}
110 
111 	/***
112 	 * constructs a new ImageRenderable
113 	 * 
114 	 * @param staticLocation the static location of the set of images
115 	 * @param size the size of the images in world coordinates.
116 	 * @param simulator the simulator to be used
117 	 * @param images the images to display.
118 	 */
119 	public ImageRenderable(final DirectedPoint staticLocation, final Dimension size,
120 			final SimulatorInterface simulator, final URL[] images)
121 	{
122 		this(new StaticLocation(staticLocation, new BoundingBox(
123 				size.getWidth(), size.getHeight(), 0.0)), simulator, images);
124 	}
125 
126 	/***
127 	 * constructs a new ImageRenderable
128 	 * 
129 	 * @param staticLocation the static location of the set of images
130 	 * @param size the size of the images in world coordinates.
131 	 * @param simulator the simulator to be used
132 	 * @param images the images to display.
133 	 */
134 	public ImageRenderable(final Point2D staticLocation, final Dimension size,
135 			final SimulatorInterface simulator, final URL[] images)
136 	{
137 		this(new StaticLocation(new DirectedPoint(staticLocation), new BoundingBox(
138 				size.getWidth(), size.getHeight(), 0.0)), simulator, images);
139 	}
140 
141 	/***
142 	 * @see nl.tudelft.simulation.dsol.animation.D2.Renderable2D#paint(java.awt.Graphics2D,
143 	 *      java.awt.image.ImageObserver)
144 	 */
145 	public void paint(final Graphics2D graphics, final ImageObserver observer)
146 			throws RemoteException
147 	{
148 		int image = this.selectImage();
149 		if (this.images[image].getImageLoadStatus() != MediaTracker.COMPLETE)
150 		{
151 			return;
152 		}
153 		Dimension size = BoundsUtil.getIntersect(this.source.getLocation(),
154 				this.source.getBounds(), this.source.getLocation().z)
155 				.getBounds().getSize();
156 		Point2D origin = this.resolveOrigin(this.orientation, size);
157 		graphics.translate(origin.getX(), origin.getY());
158 		graphics.scale(0.001, 0.001);
159 		graphics.drawImage(this.images[image].getImage(), 0, 0,
160 				(int) (1000 * size.getWidth()),
161 				(int) (1000 * size.getHeight()), observer);
162 		graphics.scale(1000, 1000);
163 		graphics.translate(-origin.getX(), -origin.getY());
164 	}
165 
166 	/***
167 	 * selects the image. This methods makes the ImageRenderable state
168 	 * dependent. One is required to return the index number of the images[]
169 	 * which has to be drawn.
170 	 * 
171 	 * @return int the current (state-dependent) image.
172 	 */
173 	public abstract int selectImage();
174 
175 	/***
176 	 * @param orientation The orientation to set.
177 	 */
178 	public void setOrientation(final short orientation)
179 	{
180 		this.orientation = orientation;
181 	}
182 
183 	/***
184 	 * @return Returns the images.
185 	 */
186 	public ImageIcon[] getImages()
187 	{
188 		return this.images;
189 	}
190 
191 	/***
192 	 * resolves the origin of the image
193 	 * 
194 	 * @param orientation the orientation (CC,..)
195 	 * @return Point2D the location
196 	 * @param size the size of the image.
197 	 */
198 	protected Point2D resolveOrigin(final short orientation,
199 			final Dimension size)
200 	{
201 		Point2D imageOrigin = new Point2D.Double(0.0, 0.0);
202 		switch (orientation)
203 		{
204 			case ImageRenderable.LB :
205 				imageOrigin.setLocation(imageOrigin.getX(), imageOrigin.getY()
206 						- size.getHeight());
207 				return imageOrigin;
208 			case ImageRenderable.CB :
209 				imageOrigin.setLocation(imageOrigin.getX() - 0.5
210 						* size.getWidth(), imageOrigin.getY()
211 						- size.getHeight());
212 				return imageOrigin;
213 			case ImageRenderable.RB :
214 				imageOrigin.setLocation(imageOrigin.getX() - 1.0
215 						* size.getWidth(), imageOrigin.getY()
216 						- size.getHeight());
217 				return imageOrigin;
218 			case ImageRenderable.LC :
219 				imageOrigin.setLocation(imageOrigin.getX(), imageOrigin.getY()
220 						- 0.5 * size.getHeight());
221 				return imageOrigin;
222 			case ImageRenderable.CC :
223 				imageOrigin.setLocation(imageOrigin.getX() - 0.5
224 						* size.getWidth(), imageOrigin.getY() - 0.5
225 						* size.getHeight());
226 				return imageOrigin;
227 			case ImageRenderable.RC :
228 				imageOrigin.setLocation(imageOrigin.getX() - 1.0
229 						* size.getWidth(), imageOrigin.getY() - 0.5
230 						* size.getHeight());
231 				return imageOrigin;
232 			case ImageRenderable.LT :
233 				imageOrigin.setLocation(imageOrigin.getX(), imageOrigin.getY());
234 				return imageOrigin;
235 			case ImageRenderable.CT :
236 				imageOrigin.setLocation(imageOrigin.getX() - 0.5
237 						* size.getWidth(), imageOrigin.getY());
238 				return imageOrigin;
239 			case ImageRenderable.RT :
240 				imageOrigin.setLocation(imageOrigin.getX() - 1.0
241 						* size.getWidth(), imageOrigin.getY());
242 				return imageOrigin;
243 			default :
244 				throw new IllegalArgumentException("unknown origin location");
245 		}
246 	}
247 }