Synthia
Generic and flexible data structure generator
Oscilloscope.java
Go to the documentation of this file.
1 /*
2  Synthia, a data structure generator
3  Copyright (C) 2019-2021 Laboratoire d'informatique formelle
4  Université du Québec à Chicoutimi, Canada
5 
6  This program is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Lesser General Public License as published
8  by the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 package examples.oscilloscope;
20 
21 import java.awt.BorderLayout;
22 import java.awt.Color;
23 import java.awt.Dimension;
24 import java.awt.Graphics;
25 import java.awt.Graphics2D;
26 import java.awt.event.ActionEvent;
27 import java.awt.event.ActionListener;
28 import java.util.HashSet;
29 import java.util.List;
30 import java.util.Set;
31 
32 import javax.swing.JFrame;
33 import javax.swing.JPanel;
34 
35 /**
36  * A simple {@link JFrame} that simulates the operation of an oscilloscope.
37  * This class produces a window, showing a square grid on which two-dimensional
38  * points can be added and displayed. The class is used to display the
39  * patterns produced by the various pickers showcased in this package. An
40  * example of the display produced by the oscilloscope is:
41  * <p>
42  * <img src="./doc-files/oscilloscope/Lissajous_r_6_5_p_pi_4.png" alt="Oscilloscope window" />
43  *
44  * @ingroup Examples
45  */
46 public class Oscilloscope extends JFrame
47 {
48  /**
49  * Dummy UID.
50  */
51  private static final long serialVersionUID = 1L;
52 
53  protected final Screen m_screen;
54 
55  protected static final Color s_darkGreen = new Color(0, 192, 0);
56 
57  public Oscilloscope()
58  {
59  super();
60  m_screen = new Screen();
61  add(m_screen, BorderLayout.CENTER);
62  setTitle("Oscilloscope");
63  setSize(300, 300);
64  setLocationRelativeTo(null);
65  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
66  }
67 
68  public Oscilloscope addPoints(List<float[]> points)
69  {
70  for (float[] p : points)
71  {
72  // We center the screen and flip it upside down
73  m_screen.addPoint((p[0] + 1) / 2f, (1 - p[1]) / 2f);
74  }
75  return this;
76  }
77 
78  protected static class Screen extends JPanel implements ActionListener
79  {
80  /**
81  * Dummy UID.
82  */
83  private static final long serialVersionUID = 1L;
84 
85  protected Set<Point> m_points;
86 
87  public Screen()
88  {
89  super();
90  m_points = new HashSet<Point>();
91  this.setPreferredSize(new Dimension(300, 300));
92  }
93 
94  protected void addPoint(float x, float y)
95  {
96  Point p = new Point(x, y);
97  m_points.add(p);
98  }
99 
100  private void doDrawing(Graphics g)
101  {
102  double width = this.getWidth();
103  double height = this.getHeight();
104  Graphics2D g2d = (Graphics2D) g;
105  g2d.setPaint(Color.black);
106  g2d.fillRect(0, 0, (int) width, (int) height);
107  g2d.setPaint(Color.darkGray);
108  for (int i = 0; i <= height; i += height / 6)
109  {
110  g2d.drawLine(0, i, (int) width, i);
111  }
112  for (int i = 0; i <= width; i += width / 6)
113  {
114  g2d.drawLine(i, 0, i, (int) height);
115  }
116  for (Point p : m_points)
117  {
118  int x = (int) (width * p.getX());
119  int y = (int) (height * p.getY());
120  if (x % ((int) (width / 6)) == 0 || y % ((int) (height / 6)) == 0)
121  {
122  g2d.setPaint(s_darkGreen);
123  }
124  else
125  {
126  g2d.setPaint(Color.green);
127  }
128  g2d.drawLine(x, y, x, y);
129  }
130  }
131 
132  @Override
133  public void paintComponent(Graphics g)
134  {
135  super.paintComponent(g);
136  doDrawing(g);
137  }
138 
139  @Override
140  public void actionPerformed(ActionEvent e)
141  {
142  repaint();
143  }
144  }
145 
146  protected static class Point
147  {
148  float m_x;
149 
150  float m_y;
151 
152  public Point(float x, float y)
153  {
154  super();
155  m_x = x;
156  m_y = y;
157  }
158 
159  public float getX()
160  {
161  return m_x;
162  }
163 
164  public float getY()
165  {
166  return m_y;
167  }
168  }
169 }
examples.oscilloscope.Oscilloscope.s_darkGreen
static final Color s_darkGreen
Definition: Oscilloscope.java:55
examples.oscilloscope.Oscilloscope.addPoints
Oscilloscope addPoints(List< float[]> points)
Definition: Oscilloscope.java:68
examples.oscilloscope.Oscilloscope.Oscilloscope
Oscilloscope()
Definition: Oscilloscope.java:57
examples.oscilloscope.Oscilloscope
A simple JFrame that simulates the operation of an oscilloscope.
Definition: Oscilloscope.java:46
examples.oscilloscope.Oscilloscope.m_screen
final Screen m_screen
Definition: Oscilloscope.java:53