-
Notifications
You must be signed in to change notification settings - Fork 0
/
PublisherListenerSignalExample.java
157 lines (111 loc) · 3.11 KB
/
PublisherListenerSignalExample.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
155
156
157
package pl.softech.stackoverflow.pattern;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
public class PublisherListenerSignalExample {
private interface Listener {
void onEventRecieved(Event e);
}
private static class Event {
private double distance;
private double atten;
public Event(double distance, double atten) {
this.distance = distance;
this.atten = atten;
}
public double getDistance() {
return distance;
}
public double getAtten() {
return atten;
}
}
private static class GUI {
public static double d;
}
private static class Mover {
public static double instantDistance(double animpos) {
return 0;
}
}
private static double distance, animpos;
private static List<Listener> listeners = new LinkedList<>();
private static void fire(Event event) {
for(Listener l : listeners) {
l.onEventRecieved(event);
}
}
public static void addListener(Listener l) {
listeners.add(l);
}
public static double calculateSignal(double d, double f) {
GUI.d = Mover.instantDistance(animpos); // needs to be displayed
double atten = 20 * (Math.log10(d)) + 20 * (Math.log10(f)) + 32.44; // would
// also
// like
// to
// display
// this
fire(new Event(d, atten));
return atten;
}
public static void main(String[] args) {
addListener(new Listener() {
@Override
public void onEventRecieved(Event e) {
System.out.println(String.format("distance=%f atten=%f", e.getDistance(), e.getAtten()));
}
});
final JTextArea textArea_3 = new JTextArea();
textArea_3.setEditable(false);
textArea_3.setBounds(147, 481, 215, 32);
final JTextArea textArea_4 = new JTextArea();
textArea_4.setEditable(false);
textArea_4.setBounds(147, 527, 215, 27);
addListener(new Listener() {
@Override
public void onEventRecieved(final Event e) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
textArea_3.setText(""+e.getDistance()); //would have to be converted to String
textArea_4.setText(""+e.getAtten()); //would have to be converted to String
}
});
}
});
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
frame.setContentPane(panel);
panel.add(textArea_3);
panel.add(textArea_4);
frame.pack();
frame.setVisible(true);
}
});
Thread worker = new Thread(new Runnable() {
@Override
public void run() {
Random rnd = new Random();
while(true) {
calculateSignal(rnd.nextDouble(), rnd.nextDouble());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
worker.setDaemon(true);
worker.start();
}
}