The problem is that sin(x)
has values from -1.0 up to 1.0.
When these are changed to int
type
in the statement
int startY = (int)Math.sin( x );
they will almost always be zero.
To use the drawLine()
method, the endpoints of
the line need to be positive integers.
But what we want to graph consists of negative and positive
numbers smaller than one!
To deal with this problem you need to scale the computed values into graphics coordinates. Say that the applet is 600 pixels wide, and that this is supposed to represent the range of 0.0 to 2*PI. So we have:
actual x value integer X to fit applet width 0.0 0 2*PI 599
The floating point number x
can be changed into the
correct integer X
by using a linear equation:
X = x * 599/(2*PI)
This may look awful, but it is just algebra.