1 / 9

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?

lilia
Download Presentation

Applets

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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 htmldocument to reference the applet.

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

  4. 1 <html> 2 <applet code="Welcome.class" width=300 height=30> 3 </applet> 4 </html> 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 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 JApplet public void init( ) YourApplet public void start() public void paint(Graphics g) public void paint(Graphics g) public void stop() public void init( ) public void destroy() public void start() showstatus( String)

  7. JApplet methods that the applet container calls during execution

  8. 1 <html> 2 <applet code="WelcomeApplet.class" width=300 height=30> 3 </applet> 1 // WelcomeApplet.java 4 </html> 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 }

More Related