/
P060_Kumar_Panel.java
147 lines (100 loc) · 3.23 KB
/
P060_Kumar_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
140
141
142
143
144
145
146
147
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* @author Abhishek Kumar
*
*/
public class P060_Kumar_Panel extends JPanel {
//variables
private JLabel firstName;
private JLabel lastName;
private int inputValue;
private JLabel displayNum;
private int counter;
/**Constructor
* @param inputValue-to check the number as even or odd number input
*/
public P060_Kumar_Panel(int inputValue){
try {
this.inputValue = inputValue;
this.setPreferredSize(new Dimension(100,100));
//setting the color as per the input
if(inputValue % 2 == 0){
this.setBackground(Color.WHITE);
}else{
this.setBackground(new Color(173,216,230));
}
//setting the JLabels
firstName = new JLabel("Abhishek");
lastName = new JLabel("Kumar");
firstName.setFont(new Font("Papyrus",Font.PLAIN,15));
firstName.setAlignmentX(Component.CENTER_ALIGNMENT);
lastName.setFont(new Font("Papyrus",Font.PLAIN,15));
lastName.setAlignmentX(Component.CENTER_ALIGNMENT);
//adding the labels in the Jpanel
this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
this.add(firstName);
this.add(lastName);
//calling the countdown Timer
setDisplayNum();
}
catch(Exception e)
{
e.printStackTrace();
}
}
/**
* Set the counter as per the input
*/
private void setDisplayNum() {
try {
//setting the counter
if(inputValue % 2 == 0){
counter = 0;
}else{
counter = 9;
}
//creating JLabel to display the counter
displayNum = new JLabel(String.valueOf(counter));
displayNum.setFont(new Font("Papyrus",Font.PLAIN,15));
this.add(displayNum);
ActionListener taskPerformer = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//logic for increasing/decreasing counter
if(inputValue % 2 == 0 && counter == 9){
counter = 0;
}
else if((inputValue % 2 != 0) && counter == 0){
counter = 9;
}
else if((inputValue % 2 == 0)){
counter++;
}
else{
counter--;
}
displayNum.setText(String.valueOf(counter));
}
};
new Timer(1000, taskPerformer).start();
}
catch(Exception e)
{
e.printStackTrace();
}
}
/**Main method
* @param s
*/
/*public static void main(String [] s)
{
JFrame jFrame = new JFrame();
jFrame.setContentPane(new P060_Kumar_Panel(1));
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.pack();
jFrame.setVisible(true);
}*/
}