aboutsummaryrefslogtreecommitdiff
path: root/Week14 Route 66/src/com/camilstaps/route66/RoadView.java
blob: 660d44ea5d05cc30105a003b71bbf0098e1cf80a (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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
126
127
/*
 * The MIT License (MIT)
 * 
 * Copyright (c) 2015 Camil Staps <info@camilstaps.nl>
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package com.camilstaps.route66;

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 * OO13route66 animation
 *
 * Yields a graphical view on all cars
 * 
 * @author Pieter Koopman, Camil Staps
 */
public class RoadView extends JFrame
{
    JPanel panel;                               // the panel to draw the cars
    public static final int WINDOWSIZE = 600;   // the window size
    private final Model model;                  // the model knows the position of cars

    /**
     * A subclass of JPanel to draw the cars.
     * This is a bit of a hack to ensure that this panel is painted correctly.
     */
    private class CarPanel extends JPanel {
        /**
         * Constructor: just call the constructor of the base class
         */
        public CarPanel() {
            super();
        }
        /**
         * paint the roads and cars
         * @param g Graphics to paint on
         */
        @Override
        public void paint (Graphics g) {
            paintRoad(g);
            paintCars(g);
        }
    }
    /**
     * The constructor of RoadView
     * @param model
     */
    RoadView(Model model) {
        super("Route66 traffic simulator");
        this.model = model;
        setSize(WINDOWSIZE, WINDOWSIZE);
        setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        panel = new CarPanel();
        add(panel);
    }            
    /**
     * Paint the view of the model graphically
     * @param g
     */
    @Override
    public void paint(Graphics g) {
        super.paint(g);
        panel.repaint();
    }
    /**
     * Paint all cars
     * @param g graphics to draw on
     */
    private void paintCars (Graphics g) {
        for (int i = 0; i < Model.NUMBEROFCARS; i += 1) {
            model.getCar(i).paint(g);
        }
    }

    /**
     * Paint the roads
     * @param g graphics to draw on
     */
    private void paintRoad (Graphics g) {
        final int left = getStartCrossing();
        final int width = getEndCrossing() - left;
        g.setColor(Color.white);    // background
        g.fillRect(0, 0, WINDOWSIZE, WINDOWSIZE);
        g.setColor(Color.darkGray); // streets
        g.fillRect(0, left, WINDOWSIZE, width);
        if (Model.DIRECTIONS > 2) {  // paint a crossing if there are 4 directions
            g.fillRect(left, 0, width, WINDOWSIZE);
        }
    }
    
    /**
     * Get the relevant half of the coordinate where the crossing starts
     * @return 
     */
    public static int getStartCrossing() {
        return (WINDOWSIZE / 2) - Car.CARWIDTH - 4;
    }
    
    /**
     * Get the relevant half of the coordinate where the crossing ends
     * @return 
     */
    public static int getEndCrossing() {
        return getStartCrossing() + 2 * Car.CARWIDTH + 8;
    }
}