-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSineWave.java
More file actions
92 lines (79 loc) · 1.52 KB
/
SineWave.java
File metadata and controls
92 lines (79 loc) · 1.52 KB
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
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
public class SineWave
{
public static void main ( String[] args )
{
SineFrame frame = new SineFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
/**
A frame with a message panel
*/
class SineFrame extends JFrame
{
public SineFrame()
{
setTitle("SineTest");
setSize(WIDTH,HEIGHT);
// add panel to frame
SinePanel panel = new SinePanel();
Container contentPane = getContentPane();
contentPane.add(panel);
}
public static final int WIDTH = 640;
public static final int HEIGHT = 480;
}
/**
A panel that shows a sine wave
*/
class SinePanel extends JPanel
{
public SinePanel()
{
w = 6;
old_x = 5;
old_y = 240;
analog = false;
}
public void paintComponent( Graphics g )
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.draw(new Line2D.Double(0,240,640,240));
for ( x=5;x<635;x++)
{
y = 240 + 60*Math.sin((x-30)/45);
if ( ( analog || x%6==0 ) )
{
if ( analog )
{
if ( old_x <= x )
g2.draw(new Line2D.Double(old_x,old_y,x,y));
old_x = x; old_y = y;
}
else
{
if ( y < 240 )
{
ul_y = y;
h = 240 - y;
}
else
{
ul_y = 240;
h = y - 240;
}
g2.draw(new Rectangle2D.Double(x-2,ul_y,w,h));
}
}
}
}
double x, y, old_x, old_y;
double ul_y;
double w, h;
boolean analog;
}