Initial Revision
[ohcount] / test / src_dir / java1.java
1 // Program 11.6: A nicer sine wave
2 import java.applet.Applet;    
3 import java.awt.Graphics; 
4
5 public class SineApplet2 extends Applet {
6
7         public void paint(Graphics g) {
8
9                 int i, j1, j2;
10
11                 j1 = yvalue(0);
12                 for (i = 0; i < size().width; i++) {
13                         j2 = yvalue(i+1);
14                         g.drawLine(i, j1 ,i+1, j2);
15                         j1 = j2;
16                 }
17
18         }
19
20         // Given the  xpoint we're given calculate the Cartesian equivalent
21         private int yvalue(int ivalue)  {
22
23                 double xmin = -10.0;
24                 double xmax =  10.0;
25                 double ymin = -1.0;
26                 double ymax =  1.0;
27                 double x, y;
28                 int jvalue;
29
30                 x = (ivalue * (xmax - xmin)/(size().width - 1)) + xmin;
31
32                 // Take the sine of that x 
33                 y = Math.sin(x);
34
35                 // Scale y into window coordinates
36                 jvalue = (int) ((y - ymin)*(size().height - 1)/
37                                 (ymax - ymin));
38
39                 /* Switch jvalue from Cartesian coordinates 
40                          to computer graphics coordinates */   
41                 jvalue = size().height - jvalue;
42
43                 return jvalue;
44
45         }
46
47 }
48