posted 20 years ago
I am working on a program that is supposed to draw the function f(x) = Sin(x). However, the output of the program is a horizontal line. Could somebody give me a tip as to where to look? Thanks a bunch in advance.
Here is the code:
class drawSine extends JPanel
{
double f(double x)
{
return 50*Math.sin((x/100.0)*2*Math.PI);
}
// Draw the function
public void drawFunction()
{
repaint();
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
// Draw x axis
g.drawLine(10, 100, 390, 100);
// Draw y axis
g.drawLine(200, 30, 200, 390);
// Draw arrows on x axis
g.drawLine(390, 100, 370, 90);
g.drawLine(390, 100, 370, 110);
// Draw arrows on y axis
g.drawLine(200, 30, 190, 50);
g.drawLine(200, 30, 210, 50);
// Draw x, y
g.drawString("X", 370, 70);
g.drawString("Y", 220, 40);
//draw labels for -2PI, 0 and 2PI
g.drawString("-2\u03c0", 100, 110);
g.drawString("0", 200, 110);
g.drawString("2\u03c0", 300, 110);
// Draw function
Polygon p = new Polygon();
double scaleFactor = 0.01;
for (int x=-100; x <= 100; x++)
{
p.addPoint(x+200, 200-(int)(scaleFactor*f(x)));
}
// Draw a polygon line by connecting the points in the polygon
g.drawPolyline(p.xpoints, p.ypoints, p.npoints);
}
}