From cbb41f8025105e455993db2dee52d4234aed0aeb Mon Sep 17 00:00:00 2001 From: Camil Staps Date: Tue, 26 May 2015 10:12:10 +0200 Subject: Start w14 --- Week14 Route 66/src/OO14route66/Model.java | 60 ++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 Week14 Route 66/src/OO14route66/Model.java (limited to 'Week14 Route 66/src/OO14route66/Model.java') 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 views; + + + /** + * Constructor: create all cars + */ + public Model() { + views = new ArrayList(); + 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(); + } + } +} -- cgit v1.2.3