-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWavePrint.java
More file actions
57 lines (52 loc) · 1.23 KB
/
Copy pathWavePrint.java
File metadata and controls
57 lines (52 loc) · 1.23 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
import java.awt.*;
import javax.swing.*;
public class WavePrint extends JPanel
{
int SCALEFACTOR = 200;
int cycles;
int points;
double[] sines;
int[] pts;
public void setCycles(int cycles)
{
this.cycles = cycles;
this.points = SCALEFACTOR * cycles * 2;
this.sines = new double[points];
for (int i = 0; i < points; i++)
{
double radians = (Math.PI / SCALEFACTOR) * i;
this.sines[i] = Math.sin(radians);
}
}
public void paintComponent(Graphics g)
{
int maxWidth = getWidth();
double hstep = (double) maxWidth / (double) points;
int maxHeight = getHeight();
pts = new int[points];
for (int i = 0; i < points; i++)
{
pts[i] = (int) (sines[i] * maxHeight / 2 * .75 + maxHeight / 2);
}
g.setColor(Color.BLACK);
for (int i = 1; i < points; i++)
{
int x1 = (int) ((i - 1) * hstep);
int x2 = (int) (i * hstep);
int y1 = pts[i - 1];
int y2 = pts[i];
g.drawLine(x1, y1, x2, y2);
}
}
public static void main(String[] args)
{
JFrame frame = new JFrame("Wave Pattern");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBackground(Color.green);
frame.setSize(250, 250);
WavePrint sw = new WavePrint();
sw.setCycles(5);
frame.add(sw);
frame.setVisible(true);
}
}