Transcript Applets
Applets
What is an applet?
• Why create applets instead of applications?
– Applets are Java programs that can be embedded in an
HTML document
– In contrast, applications are stand-alone programs.
• What is required to create an applet?
– When you create an applet in Java, you must import
JApplet class to inherit predefined methods.
– Applets don’t have a main(). The web browser looks
for a standard set of methods to begin execution.
(init() and start())
– Create an html document to reference the applet.
Application vs Applets
GUI Application
Applet
• Inherits from JFrame
• Inherits from JApplet
• Has a constructor
• Requires calls to setSize,
setDefaultCloseOperation, and
setVisible to establish window
• Uses an init method that
performs the same operations as
a constructor.
• An applet does not have a
window of its own so it does NOT
require calls to these methods.
• Requires a static main method
to create an instance of the
application.
• There is no main() method -- the
browser creates an instance of
the class automatically.
Environment for Applets
• Compile java source code:
javac Welcome.java
• Create html document to reference class:
1 <html>
Welcome.html
2 <applet code="Welcome.class" width=300 height=30>
3 </applet>
4 </html>
• Load html doc with web browser or appletviewer :
appletviewer Welcome.html
JApplet
• Over 200 methods are needed to make
applets work correctly. We don’t have to
reinvent the wheel.
• Blank functions are provided that we can
override by overloading them in our class.
• We must use the same header for init(),
start(), paint(), stop() and destroy() to call
them during the execution of the applet.
Different Parts of An Applet
Inherit applet methods from class JApplet
JApplet
public void init( )
YourApplet
public void start()
public void paint(Graphics g)
public void paint(Graphics g)
public void init( )
public void stop()
public void start()
public void destroy()
showstatus( String)
JApplet methods that the applet container calls during execution
init( )
Called once by browser when applet is loaded
- initialization of instance variables and GUI components
start( )
Called after init() is finished and every time the browser
returns to HTML page
- starting animations
paint(Graphics g)
-called after init() is finished and start() has started, and every
time the applet needs to be repainted. (ie. window moves.)
-Drawing with the Graphics object
stop ( )
Called when the applet should stop executing (i.e. browser
leaves HTML page.)
Stopping animations
destroy( )
Called when applet is removed from memory (i.e. browser
exists)
- destroy resources allocated to applet
1 // WelcomeApplet.java
2 // A first applet in Java
3 import javax.swing.JApplet;
// import class JApplet
4 import java.awt.Graphics;
// import class Graphics
5
6 public class WelcomeApplet extends JApplet {
7
public void paint( Graphics g )
8
{
9
10
super.paint(g);
g.drawString( "Welcome to Java Programming!", 25, 25 );
}
11 }
1 <html>
2 <applet code="WelcomeApplet.class" width=300 height=30>
3 </applet>
4 </html>