Upload
erick-francis
View
220
Download
1
Embed Size (px)
Citation preview
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 AppletsGUI Application
• Inherits from JFrame
• Has a constructor
• Requires calls to setSize, setDefaultCloseOperation, and setVisible to establish window
• Requires a static main method to create an instance of the application.
Applet• Inherits from JApplet
• 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.
• 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:
• Load html doc with web browser or appletviewer : appletviewer Welcome.html
1 <html>
2 <applet code="Welcome.class" width=300 height=30>
3 </applet>
4 </html>
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 AppletInherit applet methods from class JApplet
YourApplet
JApplet
public void init( )
public void start()
public void paint(Graphics g)
public void stop()
public void destroy()
showstatus( String)
public void paint(Graphics g)
public void init( )
public void start()
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
JApplet methods that the applet container calls during execution
1 <html>
2 <applet code="WelcomeApplet.class" width=300 height=30>
3 </applet>
4 </html>
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 { super.paint(g);
9 g.drawString( "Welcome to Java Programming!", 25, 25 );
10 }
11 }