Download presentation
Presentation is loading. Please wait.
Published byDamian Black Modified over 9 years ago
1
Intro to Applets
2
Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML page Applications and applets share many common programming features, although they differ slightly in some respects Every application must have a main method Java applets do not need a main method
3
Applets in Java An applet is a program that runs in the appletviewer (a test utility for applets is included in J2SDK) or a Web browser such as Netscape or Internet Explorer. The appletviewer (or browser) executes an applet when an HTML document containing the applet is opened in the appletviewer (or browser).
4
The Applet Class public class MyApplet extends Applet { public void init() {... } public void start() {... } other methods }
5
The JApplet Class To use swing components, it is necessary to create a Java Applet that extends javax.swing.Applet JApplet inherits all the methods from the Applet class import java.applet.*; public class Welcome extends Applet{ }
6
Browser Calling Applet Methods
7
The init method Invoked when the applet is first loaded and again if the applet is reloaded. public void init( ) { } Common functions implemented in this method include loading images, setting up user-interface components, and getting parameters from the tag in the HTML page.
8
Writing Applets Always extends the JApplet class, which is a subclass of Applet for Swing components. Override init(), start(), stop(), and destroy() if necessary. By default, these methods are empty. Add your own methods and data if necessary. Applets are always embedded in an HTML page.
9
To Create an Applet import javax.swing.*; import java.awt.*; import java.awt.event.*; Where the awt packages refer to Abstract Windowing Tools. These packages contain classes of GUI as well as graphical components that you will need to create your screens.
10
The awt package To make things “happen” in an applet, we need a way to place text and/or graphics on the screen and to receive input from the user. While Swing does provide means in which to do this, many browsers do not yet recognize Swing. The awt package provides many different classes and methods to handle screen I/O.
11
The awt package Applets sometimes make use of the paint method of the java.awt.Component class public void paint (Graphics g ) called automatically (after init method) to “paint” the screen you must supply the instructions for what is to be painted and where it goes! Notice the parameter list ‘Graphics g’
12
The Graphics Class (found in package java.awt.*;) Graphics is a class, g is an instance of that class One method of the Graphics class is: drawString (String str, int x, int y) The text to be displayed The horizontal screen location (in pixels) where the text is to be displayed The vertical screen location (in pixels) where the text is to be displayed
13
A Simple Applet import java.awt.*; import javax.swing.*; public class HelloWorld extends JApplet { public void paint(Graphics g) { g.drawString("Hello World", 50, 100); }
14
Flow of Control in JApplet: The init and paint methods public void init ( ) To initialize variables and references automatically called when the applet begins executing guaranteed to be the first method called public void paint ( Graphics g ) called after init( ) and when needed thereafter can be forced by using repaint( )
15
Viewing Applets <applet code = “Welcome.class” width= 300 height = 100>
16
Applications vs. Applets Similarities Since they both are subclasses of the Container class, all the user interface components, layout managers, and event-handling features are the same for both classes. Differences Applications are invoked by the Java interpreter, and applets are invoked by the Web browser. Applets have security restrictions Web browser creates graphical environment for applets, GUI applications are placed in a frame.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.