SE450: Animation: Model [26/28] |
Package documentation: myproject.model [private] myproject.util [private]
file:MP.java [source] [doc-public] [doc-private]
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
package myproject.model; /** * Static class for model parameters. */ public class MP { private MP() {} /** Length of cars, in meters */ public static double carLength = 10; /** Length of roads, in meters */ public static double roadLength = 200; /** Maximum car velocity, in meters/second */ public static double maxVelocity = 6; }
file:Model.java [source] [doc-public] [doc-private]
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package myproject.model; import java.util.List; import java.util.ArrayList; import java.util.Observable; import myproject.util.Animator; /** * An example to model for a simple visualization. * The model contains roads organized in a matrix. * See {@link #Model(AnimatorBuilder, int, int)}. */ public class Model extends Observable { private List<Agent> agents; private Animator animator; private boolean disposed; private double time; /** Creates a model to be visualized using the <code>builder</code>. * If the builder is null, no visualization is performed. * The number of <code>rows</code> and <code>columns</code> * indicate the number of {@link Light}s, organized as a 2D * matrix. These are separated and surrounded by horizontal and * vertical {@link Road}s. For example, calling the constructor with 1 * row and 2 columns generates a model of the form: * <pre> * | | * --@--@-- * | | * </pre> * where <code>@</code> is a {@link Light}, <code>|</code> is a * vertical {@link Road} and <code>--</code> is a horizontal {@link Road}. * Each road has one {@link Car}. * * <p> * The {@link AnimatorBuilder} is used to set up an {@link * Animator}. * {@link AnimatorBuilder#getAnimator()} is registered as * an observer of this model. * <p> */ public Model(AnimatorBuilder builder, int rows, int columns) { if (rows < 0 || columns < 0 || (rows == 0 && columns == 0)) { throw new IllegalArgumentException(); } if (builder == null) { builder = new NullAnimatorBuilder(); } this.agents = new ArrayList<Agent>(); setup(builder, rows, columns); this.animator = builder.getAnimator(); super.addObserver(animator); } /** * Run the simulation for <code>duration</code> model seconds. */ public void run(double duration) { if (disposed) throw new IllegalStateException(); for (int i=0; i<duration; i++) { time++; // iterate through a copy because agents may change during iteration... for (Agent a : agents.toArray(new Agent[0])) { a.run(time); } super.setChanged(); super.notifyObservers(); } } /** * Throw away this model. */ public void dispose() { animator.dispose(); disposed = true; } /** * Construct the model, establishing correspondences with the visualizer. */ private void setup(AnimatorBuilder builder, int rows, int columns) { List<Road> roads = new ArrayList<Road>(); Light[][] intersections = new Light[rows][columns]; // Add Lights for (int i=0; i<rows; i++) { for (int j=0; j<columns; j++) { intersections[i][j] = new Light(); builder.addLight(intersections[i][j], i, j); agents.add(intersections[i][j]); } } // Add Horizontal Roads boolean eastToWest = false; for (int i=0; i<rows; i++) { for (int j=0; j<=columns; j++) { Road l = new Road(); builder.addHorizontalRoad(l, i, j, eastToWest); roads.add(l); } eastToWest = !eastToWest; } // Add Vertical Roads boolean southToNorth = false; for (int j=0; j<columns; j++) { for (int i=0; i<=rows; i++) { Road l = new Road(); builder.addVerticalRoad(l, i, j, southToNorth); roads.add(l); } southToNorth = !southToNorth; } // Add Cars for (Road l : roads) { Car car = new Car(); agents.add(car); l.accept(car); } } }
file:Road.java [source] [doc-public] [doc-private]
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
package myproject.model; import java.util.List; import java.util.ArrayList; /** * A road holds cars. */ public class Road { Road() { } // Created only by this package private List<Car> cars = new ArrayList<Car>(); public void accept(Car d) { if (d == null) { throw new IllegalArgumentException(); } cars.add(d); } public List<Car> getCars() { return cars; } }
file:Agent.java [source] [doc-public] [doc-private]
01
02
03
04
05
06
07
08
09
package myproject.model; /** * Interface for active model objects. */ public interface Agent { public void run(double time); }
file:Car.java [source] [doc-public] [doc-private]
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package myproject.model; /** * A car remembers its position from the beginning of its road. * Cars have random velocity and random movement pattern: * when reaching the end of a road, the dot either resets its position * to the beginning of the road, or reverses its direction. */ public class Car implements Agent { Car() { } // Created only by this package private boolean backAndForth = Math.round(Math.random())==1 ? true : false; private double position = (MP.roadLength-MP.carLength) * Math.random (); private double velocity = (int) Math.ceil(Math.random() * MP.maxVelocity); private java.awt.Color color = new java.awt.Color((int)Math.ceil(128 + Math.random()*127),(int)Math.ceil(128 + Math.random()*127),(int)Math.ceil(Math.random()*255)); public double getPosition() { return position; } public java.awt.Color getColor() { return color; } public void run(double time) { if (backAndForth) { if (((position + velocity) < 0) || ((position + velocity) > (MP.roadLength-MP.carLength))) velocity *= -1; } else { if ((position + velocity) > (MP.roadLength-MP.carLength)) position = 0; } position += velocity; } }
file:Light.java [source] [doc-public] [doc-private]
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
package myproject.model; /** * A light has a boolean state. */ public class Light implements Agent { Light() { } // Created only by this package private boolean state; public boolean getState() { return state; } public void run(double time) { if (time%40==0) { state = !state; } } }
file:Animator.java [source] [doc-public] [doc-private]
01
02
03
04
05
06
07
08
09
10
11
12
package myproject.util; import java.util.Observer; /** * An interface for displaying simulations. */ public interface Animator extends Observer { public void dispose(); }
file:AnimatorBuilder.java [source] [doc-public] [doc-private]
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package myproject.model; import myproject.util.Animator; /** * An interface for building a {@link Animator} for this {@link Model}. */ public interface AnimatorBuilder { /** * Returns the {@link Animator}. * This method may be called only once; subsequent calls throw an * {@link IllegalStateException}. */ public Animator getAnimator(); /** * Add the {@link Light} to the display at position <code>i,j</code>. */ public void addLight(Light d, int i, int j); /** * Add the horizontal {@link Road} to the display, west of position <code>i,j</code>. * If <code>eastToWest</code> is true, then road position 0 is the eastmost position. * If <code>eastToWest</code> is false, then road position 0 is the westmost position. */ public void addHorizontalRoad(Road l, int i, int j, boolean eastToWest); /** * Add the vertical {@link Road} to the display, north of position <code>i,j</code>. * If <code>southToNorth</code> is true, then road position 0 is the southmost position. * If <code>southToNorth</code> is false, then road position 0 is the northmost position. */ public void addVerticalRoad(Road l, int i, int j, boolean southToNorth); } /** * Null object for {@link AnimatorBuilder}. */ class NullAnimatorBuilder implements AnimatorBuilder { public Animator getAnimator() { return new NullAnimator(); } public void addLight(Light d, int i, int j) { } public void addHorizontalRoad(Road l, int i, int j, boolean eastToWest) { } public void addVerticalRoad(Road l, int i, int j, boolean southToNorth) { } } /** * Null object for {@link Animator}. */ class NullAnimator implements Animator { public void update(java.util.Observable o, Object arg) { } public void dispose() { } }