• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

drawing a sine function

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
}
}
 
Donny Frank-Rice
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Never mind. I messed up with my line adding points.
Thanks anyway.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic