19 package examples.oscilloscope;
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;
32 import javax.swing.JFrame;
33 import javax.swing.JPanel;
51 private static final long serialVersionUID = 1L;
55 protected static final Color
s_darkGreen =
new Color(0, 192, 0);
62 setTitle(
"Oscilloscope");
64 setLocationRelativeTo(
null);
65 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
70 for (
float[] p : points)
73 m_screen.addPoint((p[0] + 1) / 2f, (1 - p[1]) / 2f);
78 protected static class Screen
extends JPanel implements ActionListener
83 private static final long serialVersionUID = 1L;
85 protected Set<Point> m_points;
90 m_points =
new HashSet<Point>();
91 this.setPreferredSize(
new Dimension(300, 300));
94 protected void addPoint(
float x,
float y)
96 Point p =
new Point(x, y);
100 private void doDrawing(Graphics g)
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)
110 g2d.drawLine(0, i, (
int) width, i);
112 for (
int i = 0; i <= width; i += width / 6)
114 g2d.drawLine(i, 0, i, (
int) height);
116 for (Point p : m_points)
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)
126 g2d.setPaint(Color.green);
128 g2d.drawLine(x, y, x, y);
133 public void paintComponent(Graphics g)
135 super.paintComponent(g);
140 public void actionPerformed(ActionEvent e)
146 protected static class Point
152 public Point(
float x,
float y)