【问题标题】:How to output a Java Applet using Graphics library?如何使用图形库输出 Java Applet?
【发布时间】: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,如下所示

!https://i.imgur.com/przHRk6.png

【问题讨论】:

  • 1) 小程序通常不会 (2) 在浏览器之外显示。如果您希望它显示为桌面应用程序,为什么不使用FrameJFrame (3) 而不是小程序? 2) 不过话虽如此,小程序可以嵌入到JFrameFrame 中并显示出来。 3) 为什么使用 AWT?请参阅this answer,了解放弃 AWT 组件以支持 Swing 的许多充分理由。 4)见Java Plugin support deprecated和..

标签: java graphics applet awt


【解决方案1】:

在 NetBeansIDE 8.2 中,我只需在 GraphApplet 类中按 F6 即可输出图形。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-23
    • 1970-01-01
    • 2010-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多