下面是一段java代码,运行结果如图11-8所示。
程序代码如下:
(1)Java.awt.Graphics;
(2)Java.applet.Applet;
// ////////////////////////////////////////////
// Display y=sin(x)
public class J_DrawSin (2) Applet
{
public void paint(Graphics g)
{
double d, tx;
int x, y, x0, y0;
d=Math.PI/100; // Set step(Set the unit in x direction)
x0=y0=0;
(3)(tx=0, x=20; tx<2*Math.PI; tx+=d,x++)
{
y=120-(int)(Math.sin(tx)*50+60);
if(x>20)
g.drawLine(x0, y0, x, y);
x0=x;
y0=y;
}
g.drawstring("y=sin(x)", 10, 70);
} //End of method: paint
} //End of class: J_HelloApplet
<!-----------------------AppletExample.html------------------->
<HTML>
<HEAD>
<TITLE>
An applet Example ---- Hello Applet!
</TITLE>
</HEAD>
<BODY>
<(4)CODE="J_DrawSin.class" WIDTH=300 HEIGHT=120>
</APPLET>
</BODY>
</HTML>
此段代码属于java小应用程序,请就你对APPLET的理解,加以阐述。 |
参考答案:
Applet也叫小应用程序,是一种在浏览器环境下运行的Java程序,不能通过直接输入一条命令来启动。为编写Applet,必须创建一个public的类,且这个类必须是 java.applet.Applet的子类。要运行Applet时,必须先创建一个HTML文件,在该文件中通过<applet></applet>指明要运行的Applet程序名,用浏览器打开这个HTML文件,即可以浏览器中装入并运行该Applet程序。