0% found this document useful (0 votes)
15 views14 pages

Chapter 2 - Java Applet

This document provides an overview of Java Applets, which are Java programs embedded in HTML documents and executed in web browsers. It explains the differences between applets and applications, highlighting that applets do not require a main method and inherit from the JApplet class. Additionally, it covers the JApplet life cycle and includes examples of applet code and functionalities.

Uploaded by

09mulumbet24
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views14 pages

Chapter 2 - Java Applet

This document provides an overview of Java Applets, which are Java programs embedded in HTML documents and executed in web browsers. It explains the differences between applets and applications, highlighting that applets do not require a main method and inherit from the JApplet class. Additionally, it covers the JApplet life cycle and includes examples of applet code and functionalities.

Uploaded by

09mulumbet24
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Salale University

Computer science program


Java Programming
Chapter 2
Java Applet
Java Applet
Overview of Java Applets
• Applets are Java programs that are typically embedded in HTML
(HyperText Markup Language) documents—also called web pages.
• When a Java-enabled web browser loads a web page containing an
applet, the applet downloads into the browser and executes.
• It can contain any number of components, such as buttons, text fields,
and pictures, and it can respond to user-initiated events, such as mouse
clicks or keyboard presses.
• Many of an applet’s behaviors come from methods that reside in a
Java class named JApplet—the programmer can create additional
behaviors.
Cont’d
• JApplet is a Swing class from which you can inherit.
• A JApplet is a Component, and it is also a Container.
• When you create a JApplet, you gain access to over 200 methods
through inheritance.

Figure: Inheritance hierarchy of the JApplet class


Cont’d
You can import the JApplet class to a program by using one of the following
statements:
import javax.swing.JApplet;
import javax.swing.*;
Example:
import java.awt.Graphics; // program uses class Graphics
import javax.swing.JApplet; // program uses class JApplet
public class WelcomeApplet extends JApplet
// draw text on applet’s background
public void paint( Graphics g )
{
// call superclass version of method paint
super.paint( g );
// draw a String at x-coordinate 25 and y-coordinate 25
g.drawString( "Welcome to Java Programming!", 25, 25 );
}
}
Java Applets Vs Java Application
Applications and applets share many common
programming features, although they differ slightly in
some aspects. For example,
• Every application must have a main method, which is
invoked by the Java interpreter.
• Java applets, on the other hand, do not need a main
method. They run in the Web browser environment.
• Every applet is a subclass of java.applet.Applet.
• The Applet class is an AWT class and is not designed to
work with Swing components.
• To use Swing components in Java applets, you need to
create a Java applet that extends javax.swing.JApplet,
which is a subclass of java.applet.Applet.
Cont’d
A Java applet is like a Java application in several ways:
You save both applets and applications with a .java file extension.
You compile both applets and applications into bytecode using the
javac command, and the bytecode is stored in a file with a .class file
extension.
Both applets and applications can contain any number of methods you
define and any number of variables and constants.
Both can contain decisions, loops, arrays, and all the other java
language elements.
Both can (and applets almost always do) contain GUI elements such as
buttons and labels, and event listeners that respond to user-initiated
actions.
Cont’d
An applet also is different from an application in several ways:
 Unlike applications, applets descend from the JApplet class.
Unlike applications, applets run from another application. You do not
use the java command to execute an applet.
Unlike applications, applets do not contain a main() method. In this
chapter, you will learn about the methods every applet contains.
Unlike an application that uses a JFrame, you do not set a default close
operation for a JApplet.
Applets cannot delete, read, or create files on the user’s system.
Applets cannot run any other program on the user’s system.
Cont’d
• Every Java GUI program you have developed can be converted into an applet by replacing JFrame with
JApplet and deleting the main method.
• Below Figure (a) shows a Java GUI application program, which can be converted into a Java applet as shown
in Figure (b).
Cont’d

the complete code for the applet:

import javax.swing.*;
public class DisplayLabel extends JApplet {
public DisplayLabel() {
add(new JLabel("Great!", JLabel.CENTER));
}
}
Working with JApplet
Components
Using Japplets you can do the following:
o Change the font and color of labels.
o Use layout managers.
o Add multiple GUI components.
o Change the background color.
o Add listeners for user events.
o Add images and sounds.
example

import javax.swing.*; con.add(pressMe);


import java.awt.*; con.setLayout(new FlowLayout());
import java.awt.event.*; con.setBackground(Color.YELLOW);
import java.awt.Color; pressMe.addActionListener(this);
public class JHello2 extends JApplet implements ActionListener answer.addActionListener(this);
{ }
JLabel greeting = new JLabel("Hello. Who are you?"); public void actionPerformed(ActionEvent e)
Font font1 = new Font("Teen", Font.BOLD, 36); {
Font font2 = new Font("Teen", Font.ITALIC, 48); String name = answer.getText();
JTextField answer = new JTextField(10); con.remove(greeting);
JButton pressMe = new JButton("Press me"); con.remove(pressMe);
JLabel personalGreeting = new JLabel(" "); con.remove(answer);
Container con = getContentPane(); personalGreeting.setText("Hello, " + name + "! ");
public void init() con.add(personalGreeting);
{ con.setBackground(Color.PINK);
greeting.setFont(font1); validate();
personalGreeting.setFont(font2); }
con.add(greeting); }
con.add(answer);
Understanding the JApplet Life Cycle
• Applets are popular because they are easy to use in Web pages. Because applets
execute in a browser, the JApplet class contains methods that are automatically called
by the browser.
• The paint() method is always called after the init() and start() methods execute.
• The four methods that are automatically called in a JApplet are init(), start(), stop(),
and destroy().
The init() Method
• Called once by the applet container when an applet is loaded for execution.
• This method initializes an applet.
• Typical actions performed here are initializing fields, creating GUI components,
loading sounds to play, loading images to display
The start() Method
• Called by the applet container after method init completes execution.
• In addition, if the user browses to another website and later returns to the applet’s
HTML page, method start is called again.
Cont’d

The stop() Method


• When a user leaves a Web page (perhaps by minimizing a window or traveling to
a different Web page), the stop() method is invoked.
The destroy() Method
• The destroy() method is called when the user closes the browser or Applet Viewer.
• Closing the browser or Applet Viewer releases any resources the JApplet might
have allocated
Exercise
• Create a JApplet that asks a user to enter a password into a JTextField
and to then press Enter. Compare the password to ‘psw123’; if it
matches exactly, display “Access Granted”. If not, display “Access
Denied”. Save the file as JPasswordA.java.

You might also like