16
Intro to Applets

Intro to Applets

Embed Size (px)

DESCRIPTION

Intro to Applets. 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 - PowerPoint PPT Presentation

Citation preview

Page 1: Intro to Applets

Intro to Applets

Page 2: Intro to Applets

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

Page 3: Intro to Applets

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

Page 4: Intro to Applets

The Applet Class

public class MyApplet extends Applet{ public void init() { ... }

public void start() { ... } other methods}

Page 5: Intro to Applets

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{

}

Page 6: Intro to Applets

Browser Calling Applet Methods

Page 7: Intro to Applets

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 <applet> tag in the HTML page.

Page 8: Intro to Applets

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 anHTML page.

Page 9: Intro to Applets

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.

Page 10: Intro to Applets

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.

Page 11: Intro to Applets

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’

Page 12: Intro to Applets

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

Page 13: Intro to Applets

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); }

}

Page 14: Intro to Applets

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( )

Page 15: Intro to Applets

Viewing Applets

<html><head><title></head><body>

<appletcode = “Welcome.class”width= 300height = 100>

</applet></body></html>

Page 16: Intro to Applets

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.