Code Examples
A repository of 155 code examples for BeepBeep
AverageSlider.java
1 /*
2  BeepBeep, an event stream processor
3  Copyright (C) 2008-2018 Sylvain Hallé
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published
7  by the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package widgets;
19 
20 import plots.BitmapJFrame;
21 import ca.uqac.lif.cep.Connector;
22 import static ca.uqac.lif.cep.Connector.INPUT;
23 import static ca.uqac.lif.cep.Connector.OUTPUT;
24 import ca.uqac.lif.cep.Processor;
25 import ca.uqac.lif.cep.ProcessorException;
26 import ca.uqac.lif.cep.UtilityMethods;
27 import ca.uqac.lif.cep.functions.ApplyFunction;
28 import ca.uqac.lif.cep.functions.Cumulate;
29 import ca.uqac.lif.cep.functions.CumulativeFunction;
30 import ca.uqac.lif.cep.functions.TurnInto;
31 import ca.uqac.lif.cep.mtnp.DrawPlot;
32 import ca.uqac.lif.cep.mtnp.UpdateTable;
33 import ca.uqac.lif.cep.mtnp.UpdateTableStream;
34 import ca.uqac.lif.cep.tmf.CountDecimate;
35 import ca.uqac.lif.cep.tmf.Fork;
36 import ca.uqac.lif.cep.tmf.Trim;
37 import ca.uqac.lif.cep.tmf.Window;
38 import ca.uqac.lif.cep.util.Numbers;
39 import ca.uqac.lif.cep.widgets.GetWidgetValue;
40 import ca.uqac.lif.cep.widgets.ListenerSource;
41 import ca.uqac.lif.mtnp.plot.gral.Scatterplot;
42 import java.awt.Component;
43 import javax.swing.BorderFactory;
44 import javax.swing.BoxLayout;
45 import javax.swing.JFrame;
46 import javax.swing.JLabel;
47 import javax.swing.JPanel;
48 import javax.swing.JSlider;
49 
50 /**
51  * Display a real-time plot of the values of a slider in a Swing frame.
52  * In this example, a <tt>JFrame</tt> containing a simple slider widget
53  * is polled for its value twice per second. This value is then processed
54  * to display a 2D scatterplot with two data series:
55  * <ul>
56  * <li>The value of the slider at each time point</li>
57  * <li>The average value of the slider for the last 4 time points</li>
58  * </ul>
59  * Here is a screenshot of the two JFrames used in this example. At the left
60  * is the slider the user can move, and at the right, the generated plot that
61  * updates once per second. Notice how the use of an average (green line)
62  * smoothes the jittery motion of the raw values (blue line).
63  * <p>
64  * <img src="./doc-files/widgets/AverageSlider-screenshot.png" width="50%">
65  * <p>
66  * Graphically, the processor chain for this example can be represented
67  * as follows:
68  * <p>
69  * <img src="./doc-files/widgets/AverageSlider.png" alt="Processor graph"></a>
70  * <p>
71  * As one can see, this example is notable for its mix of various processors:
72  * <ul>
73  * <li>{@link ca.uqac.lif.cep.tmp.CountDecimate CountDecimate}</li>
74  * <li>{@link ca.uqac.lif.cep.functions.Cumulate Cumulate}</li>
75  * <li>{@link ca.uqac.lif.cep.mtnp.DrawPlot DrawPlot}</li>
76  * <li>{@link ca.uqac.lif.cep.tmf.Fork Fork}</li>
77  * <li>{@link ca.uqac.lif.cep.functions.ApplyFunction FunctionProcessor}</li>
78  * <li>{@link ca.uqac.lif.cep.GroupProcessor GroupProcessor}</li>
79  * <li>{@link ca.uqac.lif.cep.tmf.Pump Pump}</li>
80  * <li>{@link ca.uqac.lif.cep.tmf.Trim Trim}</li>
81  * <li>{@link ca.uqac.lif.cep.mtnp.UpdateTable UpdateTable}</li>
82  * <li>{@link ca.uqac.lif.cep.Window Window}</li>
83  * </ul>
84  *
85  * @author Sylvain Hallé
86  * @difficulty Medium
87  */
88 public class AverageSlider
89 {
90  public static void main(String[] args) throws ProcessorException
91  {
92  /* Create a window with a slider */
93  JPanel panel = new JPanel();
94  panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
95  JSlider m_slider = new JSlider(JSlider.HORIZONTAL, 0, 100, 30);
96  m_slider.setMajorTickSpacing(20);
97  m_slider.setPaintTicks(true);
98  m_slider.setPaintLabels(true);
99  JLabel slider_label = new JLabel("Value", JLabel.CENTER);
100  slider_label.setAlignmentX(Component.CENTER_ALIGNMENT);
101  panel.add(slider_label);
102  panel.add(m_slider);
103  panel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
104  JFrame m_frame = new JFrame("My Widget Frame");
105  m_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
106  m_frame.add(panel);
107  m_frame.pack();
108  m_frame.setVisible(true);
109 
110  ListenerSource ls = new ListenerSource();
111  m_slider.addChangeListener(ls);
112  ApplyFunction gwv = new ApplyFunction(GetWidgetValue.instance);
113  Connector.connect(ls, gwv);
114  Fork fork = new Fork(3);
115  Connector.connect(gwv, fork);
116  TurnInto one = new TurnInto(1);
117  Connector.connect(fork, 0, one, INPUT);
118  Cumulate counter = new Cumulate(new CumulativeFunction<Number>(Numbers.addition));
119  Connector.connect(one, counter);
120  Trim trim1 = new Trim(4);
121  Connector.connect(counter, trim1);
122  Trim trim2 = new Trim(4);
123  Connector.connect(fork, 1, trim2, INPUT);
124  Window win = new Window(new Average(), 4);
125  Connector.connect(fork, 2, win, INPUT);
126 
127  UpdateTable table = new UpdateTableStream("t", "x", "x̅");
128  Connector.connect(counter, OUTPUT, table, 0);
129  Connector.connect(trim2, OUTPUT, table, 1);
130  Connector.connect(win, OUTPUT, table, 2);
131  CountDecimate decimate = new CountDecimate(2);
132  Connector.connect(table, decimate);
133 
134  DrawPlot plot = new DrawPlot(new Scatterplot());
135  Connector.connect(decimate, plot);
136  BitmapJFrame plot_frame = new BitmapJFrame();
137  Connector.connect(plot, plot_frame);
138 
139  Processor.startAll(plot_frame);
140  UtilityMethods.waitForever();
141  }
142 
143 
144 }
Group processor that computes the cumulative average of a stream of numbers.
Definition: Average.java:55
Display a real-time plot of the values of a slider in a Swing frame.
Generate tables, charts and plots from event streams.
Receives a byte array as an input, and shows it in a Swing window as a picture.