aboutsummaryrefslogtreecommitdiff
path: root/Week14 Route 66/src/OO14route66/Model.java
blob: 10c413f9894008d9ef00e4fa7dbf04f107142616 (plain) (blame)
1
2
3
4
5
6
7
8
9
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
50
51
52
53
54
55
56
57
58
59
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();
        }
    }
}