OOP2

Clock.java

1
package be.uhasselt.oo2.mvc.app.clock;
2
3
import java.awt.event.WindowAdapter;
4
import java.awt.event.WindowEvent;
5
import javax.swing.BoxLayout;
6
import javax.swing.JFrame;
7
8
/**
9
 * Main user interface for the application. Makes sure the model, views and
10
 * controllers are connected to each other.
11
 * @author jvermeulen
12
 */
13
public class Clock {
14
    // The clock data (i.e., the Model).
15
    private ClockModel mModel;
16
    
17
    // A digital view of the clock.
18
    private ClockDigitalView mDigitalView;
19
    
20
    // A toolbar for controlling the clock.
21
    private ClockTools mTools;
22
    
23
    // An analog view of the clock.
24
    private final ClockAnalogView mAnalogView;
25
    
26
    public Clock(int hour, int minute, int second) {
27
        // Create the data model.
28
        mModel = new ClockModel();
29
30
        // Create the digital clock view.
31
        mDigitalView = new ClockDigitalView(mModel, null);
32
        mModel.addObserver(mDigitalView);
33
34
        // Create the analog clock view.
35
        mAnalogView = new ClockAnalogView(mModel, null);
36
        mModel.addObserver(mAnalogView);
37
        
38
        // Create the clock tools view.
39
        mTools = new ClockTools(mModel, null);
40
        mModel.addObserver(mTools);
41
        
42
        // Set the time.
43
        mModel.setTime(hour, minute, second);
44
45
        // Start the clock ticking.
46
        mModel.start();
47
    }
48
    
49
    public void createGUI() {
50
        JFrame frame = new JFrame("Clock");
51
        frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
52
        frame.getContentPane().add(mDigitalView.getUI());
53
        frame.getContentPane().add(mAnalogView);
54
        frame.getContentPane().add(mTools.getUI());                
55
        frame.addWindowListener(new WindowAdapter() {
56
            @Override
57
            public void windowClosing(WindowEvent e) {
58
                System.exit(0);
59
            }
60
        });
61
        frame.pack();
62
        frame.setVisible(true);
63
    }
64
    
65
    private static void createAndShowGUI() {
66
        Clock clock = new Clock(9, 10, 22);
67
        clock.createGUI();
68
    }
69
    
70
    public static void main(String[] args) {
71
        //Schedule a job for the event-dispatching thread:
72
        //creating and showing this application's GUI.
73
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
74
            @Override
75
            public void run() {
76
                createAndShowGUI();
77
            }
78
        });
79
    }
80
}
81