-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleGui.java
154 lines (132 loc) · 3.73 KB
/
SimpleGui.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
149
150
151
152
153
154
import javax.swing.*;
public class SimpleGui extends JFrame{
private static final int MAX_NEIGHBORS_NUM = 21;
private static final int PANEL_WIDTH = 120;
private static final int PANEL_HEIGHT = 200;
// GUI elements
private JFrame _frame;
private JPanel[] _motePanels;
private JLabel[] _moteTitles, _tempTitles, _medTitles, _avgTitles;
private JLabel[] _tempValues, _medValues, _avgValues;
private int _numOfMotes;
public SimpleGui()
{
init();
}
private void init()
{
_numOfMotes = 0;
_frame = new JFrame("Sensor network data");
_frame.setSize(800,600);
_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
_motePanels = new JPanel[MAX_NEIGHBORS_NUM+1];
_moteTitles = new JLabel[MAX_NEIGHBORS_NUM+1];
_tempTitles = new JLabel[MAX_NEIGHBORS_NUM+1];
_medTitles = new JLabel[MAX_NEIGHBORS_NUM+1];
_avgTitles = new JLabel[MAX_NEIGHBORS_NUM+1];
_tempValues = new JLabel[MAX_NEIGHBORS_NUM+1];
_medValues = new JLabel[MAX_NEIGHBORS_NUM+1];
_avgValues = new JLabel[MAX_NEIGHBORS_NUM+1];
for(int i=1;i<=MAX_NEIGHBORS_NUM;i++)
{
_motePanels[i] = new JPanel();
_motePanels[i].setLayout(new BoxLayout(_motePanels[i],BoxLayout.PAGE_AXIS));
_motePanels[i].setSize(PANEL_WIDTH,PANEL_HEIGHT);
// _motePanels[i].setLocation((i <= 10) ? (i-1)*PANEL_WIDTH : (i-11)*PANEL_WIDTH,
// (i <= 10) ? 0 : PANEL_HEIGHT);
_moteTitles[i] = new JLabel("Mote " + i);
_tempTitles[i] = new JLabel("Temperature:");
_tempValues[i] = new JLabel("0");
_medTitles[i] = new JLabel("Median:");
_medValues[i] = new JLabel("0");
_avgTitles[i] = new JLabel("Average:");
_avgValues[i] = new JLabel("0");
_tempValues[i].setVisible(true);
_medValues[i].setVisible(true);
_motePanels[i].add(_moteTitles[i]);
_motePanels[i].add(_tempTitles[i]);
_motePanels[i].add(_tempValues[i]);
_motePanels[i].add(_medTitles[i]);
_motePanels[i].add(_medValues[i]);
_motePanels[i].add(_avgTitles[i]);
_motePanels[i].add(_avgValues[i]);
_motePanels[i].setVisible(false);
_frame.add(_motePanels[i]);
}
_frame.setVisible(true);
}
public void setMeasuredTemperature(int moteId, float temperature)
{
_tempValues[moteId].setText(String.valueOf(temperature));
makeVisible(_tempValues[moteId], _motePanels[moteId]);
}
public void setMedian(int moteId, float median)
{
_medValues[moteId].setText(String.valueOf(median));
makeVisible(_medValues[moteId], _motePanels[moteId]);
}
public void setSignAverage(int moteId, float signAvg)
{
}
public void setAvgIterations(int moteId, float avgIters)
{
}
public void setMedIterations(int moteId, float medIters)
{
}
public void setEpsilon(int moteId, float epsilon)
{
}
public void reportLost(int moteId)
{
_motePanels[moteId].setVisible(false);
_numOfMotes--;
align();
}
private void makeVisible(JLabel label, JPanel panel)
{
if(!label.isVisible() || !panel.isVisible())
{
_numOfMotes++;
if(_numOfMotes <=10)
{
panel.setLocation(PANEL_WIDTH*(_numOfMotes-1),0);
}
else
{
panel.setLocation(PANEL_WIDTH*(_numOfMotes-11),PANEL_HEIGHT);
}
panel.setVisible(true);
label.setVisible(true);
}
}
private void align()
{
int moteCount = 0;
for(int i=1;i<MAX_NEIGHBORS_NUM;i++)
{
if(_motePanels[i].isVisible())
{
moteCount++;
if(moteCount <= 10)
{
_motePanels[i].setLocation(PANEL_WIDTH*(moteCount-1), 0);
}
else
{
_motePanels[i].setLocation(PANEL_WIDTH*(moteCount-1), 0);
}
}
}
}
public static void main3(String[] args) throws InterruptedException
{
SimpleGui gui = new SimpleGui();
gui.setMeasuredTemperature(3, 30);
gui.setMedian(3, 40);
gui.setMeasuredTemperature(6, 30);
gui.setMedian(6, 40);
Thread.sleep(2000);
// gui.reportLost(3);
}
}