如何使用图形库输出 Java Applet?
How to output a Java Applet using Graphics library?
我正在尝试输出数学函数,但我不知道如何调用 class 那个 draws/calculates 函数。
在主要 class 中,我尝试了以下方法
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);
}
Class代码
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";
}
}
执行程序时,我们应该会看到class的函数图。例如,我应该能够在给定的时间间隔内输出图形 f(x) = cos(x/5) + sin(x/7) + 2,如下所示
在 NetBeansIDE 8.2 中,我只需在 GraphApplet class 中按 F6 即可输出图形。
我正在尝试输出数学函数,但我不知道如何调用 class 那个 draws/calculates 函数。
在主要 class 中,我尝试了以下方法
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);
}
Class代码
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";
}
}
执行程序时,我们应该会看到class的函数图。例如,我应该能够在给定的时间间隔内输出图形 f(x) = cos(x/5) + sin(x/7) + 2,如下所示
在 NetBeansIDE 8.2 中,我只需在 GraphApplet class 中按 F6 即可输出图形。