View Javadoc

1   /*
2    * @(#) DiscreteBall.java Oct 30, 2003
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  
11  package nl.tudelft.simulation.dsol.tutorial.section44;
12  
13  import java.awt.geom.Point2D;
14  import java.net.URL;
15  import java.rmi.RemoteException;
16  
17  import nl.tudelft.simulation.dsol.SimRuntimeException;
18  import nl.tudelft.simulation.dsol.animation.D2.SingleImageRenderable;
19  import nl.tudelft.simulation.dsol.animation.interpolation.InterpolationInterface;
20  import nl.tudelft.simulation.dsol.animation.interpolation.LinearInterpolation;
21  import nl.tudelft.simulation.dsol.formalisms.devs.SimEvent;
22  import nl.tudelft.simulation.dsol.simulators.DEVSSimulatorInterface;
23  import nl.tudelft.simulation.jstats.distributions.DistNormal;
24  import nl.tudelft.simulation.jstats.streams.StreamInterface;
25  import nl.tudelft.simulation.language.d3.DirectedPoint;
26  import nl.tudelft.simulation.language.io.URLResource;
27  
28  /***
29   * @author peter
30   */
31  public class DiscreteBall extends Ball
32  {
33  	/*** the simulator */
34  	private DEVSSimulatorInterface simulator = null;
35  
36  	/*** the start time */
37  	private double startTime = Double.NaN;
38  
39  	/*** the stop time */
40  	private double stopTime = Double.NaN;
41  
42  	/*** the interpolator */
43  	private InterpolationInterface interpolator = null;
44  
45  	/***
46  	 * constructs a new DiscreteBall
47  	 * 
48  	 * @param simulator the simulator
49  	 * @throws RemoteException on remote failure
50  	 * @throws SimRuntimeException on schedule failure
51  	 */
52  	public DiscreteBall(final DEVSSimulatorInterface simulator)
53  			throws RemoteException, SimRuntimeException
54  	{
55  		super();
56  		this.simulator = simulator;
57  		URL image = URLResource.getResource("/nl/tudelft/simulation/dsol/"
58  				+ "tutorial/section44/images/customer.jpg");
59  		new SingleImageRenderable(this, simulator, image);
60  		this.next();
61  	}
62  
63  	/***
64  	 * next movement
65  	 * 
66  	 * @throws RemoteException on network failure
67  	 * @throws SimRuntimeException on simulation failure
68  	 */
69  	private void next() throws RemoteException, SimRuntimeException
70  	{
71  		StreamInterface stream = this.simulator.getReplication().getStream(
72  				"default");
73  		this.origin = this.destination;
74  		this.rotZ = 2 * Math.PI * Math.random();
75  		this.destination = new DirectedPoint(new Point2D.Double(-100
76  				+ stream.nextInt(0, 200), -100 + stream.nextInt(0, 200)),
77  				this.rotZ);
78  		this.startTime = this.simulator.getSimulatorTime();
79  		this.stopTime = this.startTime
80  				+ Math.abs(new DistNormal(stream, 9, 1.8).draw());
81  		this.interpolator = new LinearInterpolation(startTime, stopTime,
82  				origin, destination);
83  		this.simulator.scheduleEvent(new SimEvent(this.stopTime, this, this,
84  				"next", null));
85  	}
86  
87  	/***
88  	 * @see nl.tudelft.simulation.dsol.animation.LocatableInterface
89  	 *      #getLocation()
90  	 */
91  	public DirectedPoint getLocation() throws RemoteException
92  	{
93  		if (this.interpolator != null)
94  		{
95  			return this.interpolator.getLocation(this.simulator
96  					.getSimulatorTime());
97  		}
98  		return this.origin;
99  	}
100 }