Presentation is loading. Please wait.

Presentation is loading. Please wait.

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,

Similar presentations


Presentation on theme: "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,"— Presentation transcript:

1 Applets

2 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.

3 Application vs Applets GUI 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.

4 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 2 3 4 Welcome.html

5 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.

6 Different Parts of An Applet Inherit 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()

7 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

8 1 2 3 4 1// WelcomeApplet.java 2// A first applet in Java 3import javax.swing.JApplet; // import class JApplet 4import java.awt.Graphics; // import class Graphics 5 6public 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}

9


Download ppt "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,"

Similar presentations


Ads by Google