如何使用图形库输出 Java Applet?

我正在尝试输出一个数学函数,但我不知道如何调用我的类来绘制/计算该函数。


在主课中,我尝试了以下


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


拉丁的传说
浏览 100回答 1
1回答

一只斗牛犬

在 NetBeansIDE 8.2 中,我只需在 GraphApplet 类中按 F6 即可输出图形。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java