Download A sample Java applet

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Java Applet
A Java applet class that cannot run standalone. It must be called by
a html page. To run a Java applet class, you may either use a Java
enabled browser or appletviewer that inside the Java System
Development Kit.
A sample Java applet
<html>
<head>
<title>hello</title>
</head>
<body>
<applet code=test.class
</applet>
</body>
</html>
width=200
height=200>
Java compiler
(input *.java
output *.class)
test.java
import java.applet.Applet;
import java.awt.*;
Each public class should
have its own file. (public
test class match test.java)
public class test extends Applet {
public void paint(Graphics g)
{
g.drawString("Hello, World!",10,100);
}
}
CS303 Advanced Topics in Internet S ystems, Lecture1, Page 1/8
Output
Hello, World!
In the above example, a Java applet class activates by an applet tag
in the html page. In the html page, the applet tag calls an external
Java class “hello.class”. hello.class is the complied product of
hello.java. Moreover, the name hello.java should match the public
class hello.
The first two lines are import statements that are the same as
#include statement in C language.
The public paint function is the basic display function in
AWT(Abstract Window Toolkit). AWT is a graphic user interface
library in JAVA. This is the same as MFC in Visual C++, OWL in
Borland C++.
Applet Life Cycle
Init
Start
Stop / quit
Stop
restart
Event loop,
if the
applet is
valid.
clean up
Destroy
CS303 Advanced Topics in Internet S ystems, Lecture1, Page 2/8
import java.applet.Applet;
import java.awt.*;
public class hello extends Applet {
String output;
public hello() {
output="Constructor";
System.out.println("Constructor");
repaint();
}
public void init() {
output+=" Initialzation";
System.out.println("Initialzation");
repaint();
}
public void start() {
output+=" Start";
System.out.println("Start");
repaint();
}
public void stop() {
output+=" Stop";
System.out.println("Stop");
repaint();
}
CS303 Advanced Topics in Internet S ystems, Lecture1, Page 3/8
public void destory() {
output+=" Destory";
System.out.println("Destory");
repaint();
}
public void paint(Graphics g) {
g.drawString(output,10,100);
}
}
Output
D:\project\hello>appletviewer vj3b0.html
Constructor
Initialzation
Start
Stop  Stop
Start  Start again
Stop  Quit
D:\project\hello>
constructor
When a class is created, it calls the constructor.
init
To initialize the applet each time it is load or reloaded.
start
CS303 Advanced Topics in Internet S ystems, Lecture1, Page 4/8
To start the applet’s execution, such as when the applet is
loaded or when the user revisits a page that contains the applet.
stop
To stop the applet’s execution, for example, when the user
leaves the applet’s page or quits the browser.
destory
To perform a final cleanup in preparation for unloading.
AWT Graphic Demo (test.java)
import java.applet.Applet;
import java.awt.*;
public class test extends Applet {
public void paint(Graphics g)
{
showStatus("AWT Graphic Demo"); // set status bar
setBackground(Color.blue);
setForeground(Color.cyan);
// set background
// set foreground color
g.drawString("This is a string.",10,10);
// output string
g.drawLine(10,30,100,30);
// draw line
g.drawRect(10,40,90,80);
// draw rectangle
g.drawRoundRect(10,130,90,50,20,20);// draw round rectangle
g.draw3DRect(10,190,90,50,true);
// draw 3D rectangle
g.drawOval(10,250,30,50);
// draw oval
int x[]={10,20,40,60};
int y[]={310,340,310,340};
g.drawPolygon(x,y,4);
// draw polygon
g.fillRect(10,350,60,50);
// fill rectangle
CS303 Advanced Topics in Internet S ystems, Lecture1, Page 5/8
g.fillRoundRect(120,10,60,50,20,20); // fill round rectangle
g.fill3DRect(120,70,90,50,true);
// fill 3D rectangle
g.fillOval(120,130,30,50);
// fill oval
int x1[]={130,140,160,180};
int y1[]={210,240,210,240};
g.fillPolygon(x1,y1,4);
// fill polygon
}
}
CS303 Advanced Topics in Internet S ystems, Lecture1, Page 6/8
Display an image
The function getImage() may read an external graphic file then
create a graphic image. getDocumentBase() returns the current
application path. drawImage() in the paint function can display the
image to the Java virtual machine. The Java language supports
either GIF image or JPEG image.
import java.applet.Applet;
import java.awt.*;
public class test extends Applet {
Image img;
public void init() {
img=getImage(getDocumentBase(),"12 apostle.jpg");
}
public void paint(Graphics g) {
g.drawImage(img,0,0,300,300,this);
}
}
CS303 Advanced Topics in Internet S ystems, Lecture1, Page 7/8
Play a sound file
The Java supports *.au sound file only. You may convert your
sound file to au format by audiotool application.
import java.applet.*;
import java.awt.*;
public class test extends Applet {
AudioClip sound;
public void init()
{
sound=getAudioClip(getDocumentBase(),"bark.au");
sound.play();
}
}
CS303 Advanced Topics in Internet S ystems, Lecture1, Page 8/8