Code Examples
A repository of 155 code examples for BeepBeep
MouseHeatmap.java
1 /*
2  BeepBeep, an event stream processor
3  Copyright (C) 2008-2020 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 plots;
19 
20 import javax.swing.JFrame;
21 
22 import ca.uqac.lif.cep.Connector;
23 import ca.uqac.lif.cep.functions.ApplyFunction;
24 import ca.uqac.lif.cep.mtnp.DrawPlot;
25 import ca.uqac.lif.cep.mtnp.UpdateTableArray;
26 import ca.uqac.lif.cep.tmf.CountDecimate;
27 import ca.uqac.lif.cep.tmf.Window;
28 import ca.uqac.lif.cep.widgets.ListenerSource;
29 import ca.uqac.lif.cep.widgets.MouseCoordinates;
30 import ca.uqac.lif.mtnp.plot.gnuplot.HeatMap;
31 import ca.uqac.lif.mtnp.table.FrequencyTable;
32 
33 /**
34  * Displays the trail of the mouse pointer inside a heat map. The program
35  * creates two windows: the first is the "mouse playground", which is a small
36  * empty frame where the mouse movements are recorded; the second is a
37  * window that displays a "heat map", which is a two-dimensional plot made
38  * of rectangular cells, where the color of each cell is associated to a
39  * numerical value. In the present case, each cell represents a rectangular
40  * zone of pixels in the mouse playground, and the numerical value for each
41  * zone is the total number of mouse events that occurred in that zone.
42  * The end result looks like the following screenshot:
43  * <p>
44  * <img src="./doc-files/plots/heatmap.png" alt="Screenshot" />
45  * <p>
46  * One can see the heatmap update as the user moves the mouse in the
47  * playground. Colors that tend towards blue indicate that the mouse has not
48  * spent a lot of time in that zone, while red and yellow colors indicate
49  * the mouse has spent more time. The plot only updates when the mouse moves.
50  */
51 public class MouseHeatmap
52 {
53  /**
54  * The width of the mouse playground
55  */
56  protected static int WIDTH = 320;
57 
58  /**
59  * The height of the mouse playground
60  */
61  protected static int HEIGHT = 200;
62 
63  public static void main(String[] args) throws InterruptedException
64  {
65  /* Create a window where mouse movements will be trapped by
66  * a listener source. */
67  JFrame mouse_playground = new JFrame();
68  mouse_playground.setSize(WIDTH, HEIGHT);
69  mouse_playground.setTitle("Mouse playground");
70  mouse_playground.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
71  ListenerSource listener = new ListenerSource();
72  mouse_playground.addMouseMotionListener(listener);
73  mouse_playground.setVisible(true);
74 
75  /* Extract from each mouse event its x-y coordinates. */
76  ApplyFunction coords = new ApplyFunction(MouseCoordinates.instance);
77  Connector.connect(listener, coords);
78 
79  /* Use the UpdateTable processor that takes as input a single
80  * stream of arrays. */
81  UpdateTableArray update_table = new UpdateTableArray(
82  new FrequencyTable(0, WIDTH, 10, 0, HEIGHT, 10, 1d));
83 
84  /* We encase this processor in a window processor of width 100 */
85  Window window = new Window(update_table, 100);
86  Connector.connect(coords, window);
87 
88  /* To slow down the refreshing of the plot, we keep only one
89  * table for every 20 output by the window processor. This is done
90  * with a CountDecimate processor. */
91  CountDecimate decimate = new CountDecimate(20);
92  Connector.connect(window, decimate);
93 
94  /* Draw a heat map out of the coordinates. */
95  HeatMap plot = new HeatMap();
96  plot.setTitle("Heat map");
97  DrawPlot draw = new DrawPlot(plot);
98  Connector.connect(decimate, draw);
99  BitmapJFrame frame = new BitmapJFrame();
100  frame.getFrame().setLocationRelativeTo(null);
101  Connector.connect(draw, frame);
102  System.out.println("Move your mouse in the window called \"Mouse playground\".");
103  System.out.println("The heatmap will appear in the other window.");
104  System.out.println("Press Ctrl+C or close the window to end.");
105 
106  /* Show the frame. */
107  frame.start();
108 
109  }
110 }
static int HEIGHT
The height of the mouse playground.
JFrame getFrame()
Gets the frame associated to the object.
Displays the trail of the mouse pointer inside a heat map.
Receives a byte array as an input, and shows it in a Swing window as a picture.
static int WIDTH
The width of the mouse playground.