Docsity

Object Oriented Methodology - Applets, Study notes of Object Oriented Programming

Toggle sidebar
bg1
Applets
•An applet is a special kind of Java program that a browser
enabled with Java technology can download from the
internet and run.
•An applet is typically embedded inside a web page and runs
in the context of a browser.
•An applet must be a subclass of the java.applet.Applet class.
The Applet class provides the standard interface between the
applet and the browser environment.
•Swing provides a special subclass of the Applet class called
javax.swing.JApplet.
•The JApplet class should be used for all applets that use
Swing components to construct their graphical user
interfaces (GUIs).
1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23

Partial preview of the text

Download Object Oriented Methodology - Applets and more Study notes Object Oriented Programming in PDF only on Docsity!

Applets

  • (^) An applet is a special kind of Java program that a browser

enabled with Java technology can download from the

internet and run.

  • (^) An applet is typically embedded inside a web page and runs

in the context of a browser.

  • (^) An applet must be a subclass of the java.applet.Applet class.

The Applet class provides the standard interface between the

applet and the browser environment.

  • (^) Swing provides a special subclass of the Applet class called

javax.swing.JApplet.

  • (^) The JApplet class should be used for all applets that use

Swing components to construct their graphical user

interfaces (GUIs).

1

Applets

There are some important differences between an applet and a

standalone Java application, including the following:

  • (^) An applet is a Java class that extends the java.applet.Applet class.
  • (^) A main() method is not invoked on an applet, and an applet class

will (typically) not define main().

  • (^) Applets are designed to be embedded within an HTML page.
  • (^) When a user views an HTML page that contains an applet, the

code for the applet is downloaded to the user’s machine.

  • (^) A user must have a JVM on his or her machine. The JVM can be

either a plug-in of the Web browser or a separate runtime

environment.

  • (^) The JVM on the user’s machine creates an instance of the applet

class and invokes various methods during the applet’s lifetime.

2

The java.applet.Applet Class

  • (^) An applet is a Java class; if the applet is to be viewed in a Web
browser, the class must extend the java.applet.Applet class.
  • (^) The Applet class provides a common interface so that a Web
browser can communicate with the applet.
  • (^) The intersting note about the Applet class is that it extends
java.awt.Panel.
  • (^) Panel is the simplest container class. A panel provides

space in which an application can attach any other

component, including other panels.

11/27/2019 4

Panel Container for Applet 5 Object Component Container The Class Hierarchy depicts that Panel inherit their common attributes and behavior. Class Panel ( package.java.awt) is subclass of Container and Applet is subclass of Panel Panel Applet

HTML embedding Applet

  • (^) Three attributes of the tag that are required:
    • (^) Code: The name of the applet class for this applet.
    • (^) Width: The width in pixels of the applet.
    • (^) Height: The height in pixels of the applet.
  • (^) For example, if the name of your applet class is

com.world.MyApplet, the following HTML embeds an instance

of MyApplet in a Web page:

  • (^) The size of the applet will be 400 pixels wide and 500 pixels

high.

11/27/2019 7

HelloWorldApplet Class

  • (^) Few comments about this applet: This Class is Container & adds a button. There is no main() & Most of the code of the HelloWorldApplet class appears in the init() method, which is overriding the init() method from the parent class Applet. The Web browser invokes init() immediately after it creates an instance of HelloWorldApplet Class.  (^) We could have used a constructor, but we wanted to demonstrate the init() method.
  • (^) Within init(), the layout of the applet is changed to BorderLayout. (It had FlowLayout by default.) The event handling is done in the ensuing PrintHello class. Study the following code, and try to determine what this applet does.
  • (^) A PrintHello object is listening for an ActionEvent from the Go button. 8

Applet

  • (^) When a user views an HTML page that contains an applet,

the code for the applet is downloaded to the user’s machine.

 A user must have a JVM on his or her machine. The JVM can

be either a plug-in of the Web browser or a separate runtime

environment.

 The JVM on the user’s machine creates an instance of the

applet class and invokes various methods during the applet’s

lifetime.

11/27/2019 10

Applet

  • (^) This applet is displayed in a Web page named hello.html using the tag.
  • (^) The HTML looks similar to:

Enter your name and click the button.

11/27/2019 11

Applet embedding

  • (^) An applet can actually be embedded within any other application, not just a Web browser.
  • (^) If your applet is going to be embedded in a Web page, it must extend the Applet class. If your applet is going to be embedded in some other application, extending Applet is not required.
  • (^) On the same line, we may develop Swing Applets using JApplet.
  • (^) JApplet class is subclass of Applet so it inherites all the methods of Applet.
  • (^) The purpose of the JApplet class is to provide support for Swing.
  • (^) Probably the biggest difference between an applet and a JApplet is how components are added to them.
  • (^) A JApplet has three panes, much like a JFrame, and components are added to the content pane of the Japplet. 13
  • 11/27/2019

Applet Life Cycle

• An applet can react to major events in the

following ways:

It can initialize itself.

It can start running.

It can stop running.

It can perform a final cleanup , in preparation

for being unloaded.

11/27/2019 16

Life Cycle methods of an Applet

  • (^) The five applet methods are called by the applet container from the time the applet is loaded into the browser to the time that it is terminated by the browser.
  • (^) These methods correspond to various aspects of an applet’s life cycle.
  • (^) These methods are inherited from class Japplet. that the browser invokes these methods on your applet.

1) public void init().

2) public void start().

3) public void stop().

4) public void destroy().

5) public void paint(Graphics g).

17

When a user views a Web page that contains an applet, the following sequence of events occurs regarding the life cycle of the applet:

  1. The Web browser downloads the necessary bytecode and JAR file from the Web server where the code is located. (This Web server is referred to as the code base.)
  2. The browser creates an instance of the Applet class, invoking the default constructor.
  3. The applet is displayed in the Web page, with the location and size of the applet determined by the HTML.
  4. The browser invokes the init() method on the applet.
  5. The browser invokes the start() method on the applet.
  6. The browser invokes the paint() method on the applet.
  7. The applet is now live and running within the Web page.
  8. The browser calls paint() whenever the applet needs to repaint itself.
  9. The browser invokes the stop() method when the user leaves the Web page or the applet is about to be destroyed.
  10. The browser invokes the destroy() method just before destroying the applet. 19

Applet & HTML

  • (^) If a Web browser does not understand the tag, it will ignore the tag and display any HTML that appears within the opening and closing tags.
  • (^) For example, the following HTML displays a message that the user is unable to see the applet that was intended to appear:

Your browser does not support applets!

To view this page correctly, you will need to find a Web browser that provides support for applets, or install the Java Plug-in.

11/27/2019 20