/
P083_Palkar_Panel.java
149 lines (125 loc) · 3.73 KB
/
P083_Palkar_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
148
//package swing;
//Lab 1
import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JLabel;
import javax.swing.JPanel;
/*
* @author: Tejasi Palkar
* @version: 1.0
* @createdOn: 23 January, 2018
*
* This file creates a JPanel, the panel is painted with a color-white(for a even ID author)
* and Blue(for an odd ID author) and contains name of the author and a counter that runs
* from 0-9/9-0 added as labels depending on the type of ID of the author
*
*/
public class P083_Palkar_Panel extends JPanel{
/* Preset variable for the counter
* */
public static int counter=0;
/*Private members for the class
* */
private JLabel Name;
private JLabel Counter;
private Color bgColor;
/*
* Constructor for the class
*/
public P083_Palkar_Panel(int ID) {
/*
* If the ID is even , initialises background color to white and counter is set to -1
* Otherwise , background color value is set to blue and counter decreases from 9 to 0 and hence
* counter is set to 10
* */
if(ID%2==0)
{
bgColor=Color.white;
counter=-1;
CreatePanel(bgColor);
}
else
{
bgColor=new Color(173, 216, 230);
counter=10;
CreatePanel(bgColor);
}
}
/*
* creates a panel with set value of bacgkround color(white or blue), having predefined size and color passed as parameter
* Arranges the labels on the panel,sets the size and font such that the labels are located on the
* center of the panel
*
*/
private void CreatePanel(Color bgColor)
{
setSize(100,100);
setLayout(new GridBagLayout());
setBackground(bgColor);
Name =new JLabel("<html>Tejasi<br>Palkar</html>");
Name.setFont(new Font("Papyrus", Font.PLAIN, 15));
Counter = new JLabel(String.valueOf(counter));
Counter.setFont(new Font("Papyrus", Font.PLAIN, 15));
GridBagConstraints GbcObj= new GridBagConstraints();
GbcObj.gridx=0;
GbcObj.gridy=0;
add(Name,GbcObj);
GbcObj.gridx=0;
GbcObj.gridy=1;
add(Counter,GbcObj);
RunTimer(bgColor);
}
/*
* Method call to run the timer from 0 to 9 if background color is white
* and from 9 to 0 if the background color is blue.The counter value keeps changing every second
* The run method is kept within try and catch to handle exception at the runtime
*
*/
private void RunTimer(Color bgColor2)
{
// TODO Auto-generated method stub
try {
TimerTask updateCounter = new TimerTask()
{
public void run()
{
if(bgColor2==Color.WHITE)
{
counter = (counter+1)%10;
}
else
{
if(counter>0)
counter = (counter-1)%10;
else
counter=9;
}
Counter.setText(String.valueOf(counter));
}
};
Timer t1 = new Timer();
long delay = 5L;
long timeLapse = 1000L;
t1.scheduleAtFixedRate(updateCounter, delay, timeLapse);
}
catch(Exception e)
{
e.printStackTrace();//Displays in message the cause of runtime error
}
}
//Main method to test the above class' methods
/*
public static void main(String[] args)
{
JFrame F = new JFrame("Frame_Palkar");
P083_Palkar_Panel obj1 = new P083_Palkar_Panel(83);
F.setVisible(true);
F.add(obj1);
F.pack();
F.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}*/
}