Newer
Older
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
//
// JavaLine.java
// (c) Neil Gershenfeld 2/6/03
// demonstrates Java by animating sin(k*x)/k*x
//
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
public class JavaLine extends JApplet implements Runnable {
Thread T;
final int NPTS = 500;
final int NSTEPS = 100;
int point,step;
int x[] = new int[NPTS];
int y[] = new int[NPTS];
class LinePanel extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
super.setBackground(Color.white);
Graphics2D g2d = (Graphics2D) g;
GeneralPath path = new GeneralPath(GeneralPath.WIND_EVEN_ODD,NPTS);
path.moveTo(x[0],y[0]);
for (point = 1; point < (NPTS-1); ++point) {
path.lineTo(x[point],y[point]);
}
g2d.draw(path);
}
}
public void init() {
Container C = getContentPane();
C.add(new LinePanel());
}
public void start() {
if (T == null) {
T = new Thread(this);
T.start();
}
}
public void stop() {
if (T != null) {
T = null;
}
}
public void run() {
double r;
while (true) {
for (step = 1; step < NSTEPS; ++step) {
for (point = 0; point < (NPTS-1); ++point) {
r = 100 * (step*(point+0.5-NPTS/2))/(NPTS*NSTEPS);
x[point] = point;
y[point] = (int) ((NPTS/2) - (NPTS/2)*Math.sin(r)/r);
}
repaint();
try {Thread.sleep(10);}
catch (InterruptedException e) { }
}
}
}
}