aboutsummaryrefslogtreecommitdiff
path: root/Week14 Route 66/src/OO14route66/Model.java
diff options
context:
space:
mode:
Diffstat (limited to 'Week14 Route 66/src/OO14route66/Model.java')
-rw-r--r--Week14 Route 66/src/OO14route66/Model.java60
1 files changed, 60 insertions, 0 deletions
diff --git a/Week14 Route 66/src/OO14route66/Model.java b/Week14 Route 66/src/OO14route66/Model.java
new file mode 100644
index 0000000..10c413f
--- /dev/null
+++ b/Week14 Route 66/src/OO14route66/Model.java
@@ -0,0 +1,60 @@
+package OO14route66;
+
+import java.util.ArrayList;
+import java.util.Observable;
+import javax.swing.JFrame;
+
+/**
+ * OO1route66 initial class
+ * @author Pieter Koopman
+ *
+ * The class model holds all cars in the simulation
+ */
+public class Model extends Observable
+{
+ private final Car [] cars;
+ public static final int
+// DIRECTIONS = 4, // for a crossing
+ DIRECTIONS = 2, // for a single road
+ NUMBEROFCARS = 5 * DIRECTIONS; // total number of cars in system
+
+ private final ArrayList<JFrame> views;
+
+
+ /**
+ * Constructor: create all cars
+ */
+ public Model() {
+ views = new ArrayList<JFrame>();
+ cars = new Car [NUMBEROFCARS];
+ for (int c = 0; c < NUMBEROFCARS; c += 1) {
+ cars[c] = new Car(c);
+ }
+ }
+
+ /**
+ * add the view to this model. It will be repainted upon an update
+ * @param view
+ */
+ public void addView(JFrame view) {
+ views.add(view);
+ }
+
+ /**
+ * get a car from the model
+ * @param i numbers of required car
+ * @return the car itself (not a copy)
+ */
+ public Car getCar(int i) {
+ return cars[i];
+ }
+
+ /**
+ * repaint all views
+ */
+ public void update() {
+ for (JFrame view: views) {
+ view.repaint();
+ }
+ }
+}