我正在尝试输出一个数学函数,但我不知道如何调用我的类来绘制/计算该函数。
在主课中,我尝试了以下
public class GraphingProgram {
public static void main(String[] args)
{
Applet program = new Applet();
program.setSize(300, 400);
program.setName("Graphing Program");
GraphApplet testFunction = new GraphApplet();
program.add(testFunction);
program.setVisible(true);
}
班级代码
public class GraphApplet extends Applet
{
double f(double x)
{
return (Math.cos(x/5) + Math.sin(x/7) + 2) * getSize().height/ 4;
}
public void paint (Graphics g)
{
for(int x = 0; x< getSize().width; x++)
g.drawLine(x, (int) f(x), x+1, (int) f(x+1));
}
public String getAppletInfo()
{
return "Draw a function graph";
}
}
执行程序时,我们应该期望看到类的函数图。例如,我应该能够在给定的间隔上输出图形 f(x) = cos(x/5) + sin(x/7) + 2,如下所示
!https://i.imgur.com/przHRk6.png
一只斗牛犬
相关分类