【发布时间】:2019-09-20 08:38:45
【问题描述】:
我正在尝试输出一个数学函数,但我不知道如何调用我的类来绘制/计算该函数。
在主类中,我尝试了以下
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,如下所示
【问题讨论】:
-
1) 小程序通常不会 (2) 在浏览器之外显示。如果您希望它显示为桌面应用程序,为什么不使用
Frame或JFrame(3) 而不是小程序? 2) 不过话虽如此,小程序可以嵌入到JFrame的Frame中并显示出来。 3) 为什么使用 AWT?请参阅this answer,了解放弃 AWT 组件以支持 Swing 的许多充分理由。 4)见Java Plugin support deprecated和..