-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClockUI.java
More file actions
153 lines (128 loc) · 4.14 KB
/
ClockUI.java
File metadata and controls
153 lines (128 loc) · 4.14 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package clock;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.Toolkit;
import javax.swing.*;
public class ClockUI implements ClockObserver{
private JFrame frame;
private JButton plusButton;
private JButton minusButton;
private JButton changeModeButton;
private JButton cancelButton;
private JTextPane hourBox;
private JTextPane minuteBox;
private JTextPane secondBox;
private Controller controller;
private final int TIMERDELAY = 1000;
public ClockUI(String name, Controller controller){
frame = new JFrame(name);
plusButton = new JButton("+");
minusButton = new JButton("-");
changeModeButton = new JButton("Change Mode");
cancelButton = new JButton("Cancel");
hourBox = new JTextPane();
hourBox.setEditable(false);
minuteBox = new JTextPane();
minuteBox.setEditable(false);
secondBox = new JTextPane();
secondBox.setEditable(false);
this.controller = controller;
this.controller.setUI(this);
}
public void setButtonVisible(boolean isVisible){
plusButton.setVisible(isVisible);
minusButton.setVisible(isVisible);
cancelButton.setVisible(isVisible);
}
public void setHourBoxActivated(){
hourBox.setBackground(Color.GREEN);
minuteBox.setBackground(Color.WHITE);
secondBox.setBackground(Color.WHITE);
}
public void setMinuteBoxActivated(){
hourBox.setBackground(Color.WHITE);
minuteBox.setBackground(Color.GREEN);
secondBox.setBackground(Color.WHITE);
}
public void setSecondBoxActivated(){
hourBox.setBackground(Color.WHITE);
minuteBox.setBackground(Color.WHITE);
secondBox.setBackground(Color.GREEN);
}
public void setTimeBoxesUnactivated(){
hourBox.setBackground(Color.WHITE);
minuteBox.setBackground(Color.WHITE);
secondBox.setBackground(Color.WHITE);
}
private void initComponents(){
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel timePanel = new JPanel();
JPanel buttonPanel = new JPanel();
buttonPanel.add(plusButton);
buttonPanel.add(minusButton);
buttonPanel.add(changeModeButton);
buttonPanel.add(cancelButton);
timePanel.add(hourBox);
timePanel.add(minuteBox);
timePanel.add(secondBox);
hourBox.setMargin(new Insets(10,30,10,30));
minuteBox.setMargin(new Insets(10,30,10,30));
secondBox.setMargin(new Insets(10,30,10,30));
frame.getContentPane().add(timePanel, BorderLayout.CENTER);
frame.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
setButtonVisible(false);
setTimeBoxesUnactivated();
}
private void refreshData(){
hourBox.setText(String.valueOf(controller.getHour()));
minuteBox.setText(String.valueOf(controller.getMinute()));
secondBox.setText(String.valueOf(controller.getSecond()));
}
private void startTimer(){
Timer timer = new Timer(TIMERDELAY, controller);
timer.setActionCommand(EventCommand.TIMERTICK);
timer.start();
}
private void setButtonListener(){
plusButton.setActionCommand(EventCommand.INCREMENT);
plusButton.addActionListener(controller);
minusButton.setActionCommand(EventCommand.DECREMENT);
minusButton.addActionListener(controller);
changeModeButton.setActionCommand(EventCommand.CHANGEMODE);
changeModeButton.addActionListener(controller);
cancelButton.setActionCommand(EventCommand.CANCEL);
cancelButton.addActionListener(controller);
}
public void start(){
initComponents();
setButtonListener();
refreshData();
startTimer();
initWindow();
}
public void initWindow() {
frame.setSize(350, 150);
Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
int x = (int) ((dimension.getWidth() - frame.getWidth()) / 2);
int y = (int) ((dimension.getHeight() - frame.getHeight()) / 2);
frame.setLocation(x, y);
frame.setVisible(true);
}
@Override
public void update() {
refreshData();
}
public static void main(String[] args) {
Clock clock = new Clock();
Controller controller = new Controller(clock);
ClockUI ui = new ClockUI("SimpleClock", controller);
clock.resigterObserver(ui);
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
ui.start();
}
});
}
}