/
P072_Mathur_Panel.java
139 lines (126 loc) · 3.65 KB
/
P072_Mathur_Panel.java
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
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
/**
* P072_Mathur_Panel class creates a label with first and and last name displayed in 2 lines
* The timer displays number from 0-9
* The background color is set depending on the value of the parameter sent to the constructor
* @author Garv Mathur
* @version 1.0
*/
public class P072_Mathur_Panel extends JPanel {
private JLabel fullName;
private JLabel clock;
private int counterValue;
private int num;
public final int WIDTH = 100;
public final int HEIGHT = 100;
public final int DELAY = 1000;
public final Color LIGHT_BLUE = new Color(173,216,230);
/**
* Initializes the panel
* @param num
* @return Nothing
*/
public P072_Mathur_Panel(int num){
try{
this.num = num;
setPanelSize();
initializeAddNameLabel();
setBackgroundColor();
setCounterValue();
setClockTimer();
}
catch (Exception e){
System.out.println("Exception in code: " + e.getMessage());
}
}
/**
* This function sets the initial counter value depending on the parameter being odd or even
* @param Nothing
* @return Nothing.
*/
private void setCounterValue() {
if(isEven()){
counterValue = 0;
}
else{
counterValue = 9;
}
}
/**
* Starts the clock timer
* If the initial parameter (num) is set as even the value increments.
* If odd it decrements.
* @param Nothing
* @return Nothing.
*/
private void setClockTimer() {
clock = new JLabel(String.valueOf(counterValue));
clock.setFont(new Font("Papyrus",Font.PLAIN,15));
this.add(clock);
ActionListener taskPerformer = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(isEven() && counterValue == 9){
counterValue = 0;
}
else if(!isEven() && counterValue == 0){
counterValue = 9;
}
else if(isEven() == true){
counterValue = counterValue + 1;
}
else{
counterValue = counterValue -1;
}
clock.setText(String.valueOf(counterValue));
}
};
new Timer(DELAY, taskPerformer).start();
}
/**
* Sets Background color of panel. If odd then sets light blue else sets white
* @param Nothing
* @return Nothing.
*/
private void setBackgroundColor() {
if(isEven()){
this.setBackground(Color.WHITE);
}
else{
this.setBackground(LIGHT_BLUE);
}
}
/**
* Initialize and add Name Label to panel
* @param Nothing
* @return Nothing.
*/
private void initializeAddNameLabel() {
fullName = new JLabel("<html>Garv<br>Mathur</html>");
fullName.setFont(new Font("Papyrus",Font.PLAIN,15));
this.add(fullName);
}
/**
* Set the preferred panel size
* @param Nothing
* @return Nothing.
*/
private void setPanelSize() {
this.setPreferredSize(new Dimension(WIDTH,HEIGHT));
}
/**
* Checks if the initial num value passed in constructor is even or odd
* @param Nothing
* @return true/false.
*/
private boolean isEven(){
return num % 2 == 0;
}
}