0% found this document useful (0 votes)
52 views24 pages

Chapter 2 Java Applet

This document provides an overview of a 3 credit hour Java Programming course with 5 ECTS credits. It includes 2 lecture hours, 3 lab hours, and 2 tutorial hours per week. The chapter discusses Java applets, which are programs that run within web browsers. It covers the basics of creating an applet class by extending the Applet class and overriding key methods like init(), paint(), and others. Examples are provided to demonstrate how to write a simple "Hello World" style applet and how to display text. Security considerations for applets are also outlined, as they run within a restricted "sandbox" environment in the browser.

Uploaded by

eyobeshete16
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
52 views24 pages

Chapter 2 Java Applet

This document provides an overview of a 3 credit hour Java Programming course with 5 ECTS credits. It includes 2 lecture hours, 3 lab hours, and 2 tutorial hours per week. The chapter discusses Java applets, which are programs that run within web browsers. It covers the basics of creating an applet class by extending the Applet class and overriding key methods like init(), paint(), and others. Examples are provided to demonstrate how to write a simple "Hello World" style applet and how to display text. Security considerations for applets are also outlined, as they run within a restricted "sandbox" environment in the browser.

Uploaded by

eyobeshete16
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 24

Course Title: Java Programming.

Credit Hour: 3 hrs.


ECTS: 5 [2 Lecture , 3 Lab and 2 Tutorial Hours]

Java Programming CoSc3053 1


Chapter 2

Java Applets
Java programs without main method

Java Programming CoSc3053 2


Chapter Outline
 Overview of Java Applets
 Java Applets Vs Java Application

Java Programming CoSc3053 3


Introduction
▪ Java language offers an option to write two kinds of programs:
– Programs that run as a “stand-alone” applications &
– Programs as applets that are dependent on a controlling program
such as a Web browser.
▪ The big difference between both is:
– Applications contain enough code to work on their own
– Applets need a controlling program to tell the applet when to do
what.
▪ An applet has no means of starting execution because it does not
have a main() method.

Java Programming CoSc3053 4


Introduction
▪ Applets are Java programs that can be embedded in HTML
documents.
▪ When a browser loads a web page containing an applet, the applet
downloads into the web browser and executes.
▪ The browser that executes an applet is known as the applet container.
▪ The JDK includes the appletviewer applet container for testing
applets before you embed them in a web page.

Java Programming CoSc3053 5


Applet Basics
▪ You need to subclass the Applet (Japplet) class.
– By doing this, you inherit quite a bit of applet functionality that is
built-in to the Applet class.
▪ The following listing provides a detailed look at the components of
the Applet class that are inherited when you implement it.

Java Programming CoSc3053 6


Applet inheritance Hierarchy

Java Programming CoSc3053 7


Applet basics…
▪ The Applet class is a descendant of the Container class;
– Thus, it can hold other objects.
▪ You do not need to create a panel first to place objects in panel,
because the Applet class extends the Panel class.
▪ Because the Container class is derived from the Component class,
we have the ability to respond to events, grab and display images, and
display text among many other things.

Java Programming CoSc3053 8


Applet basics…
▪ Let us see some of the key methods you have to access:
– setBackground(Color): Sets the background color.
– setFont(Font): Sets the font of the component.
– setForeground(Color): Sets the foreground color.
– show(): Shows the component.
– setColor(Color): sets the color for the image to be draw.

Java Programming CoSc3053 9


Applet life cycle
▪ Five methods in the Applet class give you the framework on which
you build any applet: init, start, paint, stop, destroy.
▪ public void init():
– This method is used for whatever initialization is needed for your
applet.
– This works much like a constructor - it is automatically called by
the system when Java launches the applet for the first time.
– Common actions in an applet include processing PARAM values
and adding user interface components.

Java Programming CoSc3053 10


Applet life cycle…
▪ public void start():
– This method is automatically called after the browser calls the init
method.
– It is also called whenever the user returns to the page containing
the applet after having gone off to other pages.
▪ public void paint:
– Invoked immediately after the start() method, and also any time
the applet needs to repaint itself in the browser.
– The paint() method is actually inherited from the java.awt.

Java Programming CoSc3053 11


Applet life cycle…
▪ public void stop():
– This method is automatically called when the user moves off the
page on which the applet sits.
– It can, therefore, be called repeatedly in the same applet.
– Its purpose is to give you a chance to stop a time-consuming
activity from slowing down the system when the user is not
paying attention to the applet.
– You should not call this method directly.
– If your applet does not perform animation, play audio files, or
perform calculations in a thread, you do not usually need this
method.
Java Programming CoSc3053 12
Applet life cycle…
▪ public void destroy():
– Java guarantees to call this method when the browser shuts down
normally.
– Since applets are meant to live on an HTML page, you do not
need to worry about destroying the panel.
– This will happen automatically when the browser shuts down.
– What you do need to put in the destroy method is the code for
reclaiming any non-memory-dependent resources such as graphics
contexts that you may have consumed.
– Of course, Java calls the stop method before calling the destroy
method if the applet is still active.
Java Programming CoSc3053 13
Applet vs Application…

Java Programming CoSc3053 14


Writing your first applet program
▪ The following program a simple applet program that displays a wel-come
message.
import java.awt.Graphics;
import java.applet.*;

public class WelcomeToApplet extends Applet {


public void init() {
setBackground(Color.black);
setForeground(Color.green);
}
public void paint(Graphics g){
g.drawString("Welcome to applet programming.", 100, 100);
}
}
Java Programming CoSc3053 15
Writing your first applet program
▪ The following program a simple applet program that displays a wel-
come message.
▪ Create HTML WelcomeToApplet.html and add the following

<html>
<applet
code = "WelcomeApplet.class"
width = "300" height = "45">
If your browser was Java-enabled, a
"Hello, World" message would appear here.
</applet>
</html>

Java Programming CoSc3053 16


Writing your first applet program
▪ How to run an Applet?
▪ There are two ways to run an applet
1. By Browser <html> file.
2. By appletViewer tool (for testing purpose).

Java Programming CoSc3053 17


Writing your first applet program
▪ How to run an Applet?
▪ There are two ways to run an applet
1. By Browser <html> file.
2. By appletViewer tool (for testing purpose).
1. By appletViewer
 Step 1. save java applet class with name .java

Java Programming CoSc3053 18


Writing your first applet program
1. By appletViewer
 Step 2. Open cmd

 Now Applet is started

Java Programming CoSc3053 19


Example
▪ Write a java Applet to display your name.
// filename: DisplayNameApplet.java
// import necessary libraries for an applet
import java.applet.Applet;
import java.awt.Graphics;
public class DisplayNameApplet extends Applet {
public void paint(Graphics g) {
g.drawString("Bedasa Wayessa", 50, 25);
}
}
/* HTML file
<applet code="MyApplet" width=200 height=60> </applet>
*/

Java Programming CoSc3053 20


Security basics with applets
▪ Because applets are designed to be loaded from a remote site and
then executed locally, security becomes vital.
▪ If a user enables Java in the browser, the browser will download all
the applet code on the web page and execute it immediately.
▪ The user never gets a chance to confirm or to stop individual applets
from running.
▪ For this reason, applets (unlike applications) are restricted in what
they can do.
▪ The applet security manager throws a SecurityException whenever an
applet attempts to violate one of the access rules.

Java Programming CoSc3053 21


Security basics…
▪ Applets can do the following on all platforms:
– They can show images and play sounds,
– Get keystrokes and mouse clicks from the user, and
– Send user input back to the host from which they were loaded.
▪ The restricted execution environment for applets is often called the
"sandbox."
▪ Applets playing in the "sandbox" cannot alter the user's system or spy
on it.

Java Programming CoSc3053 22


Security basics…
▪ In particular, when running in the sandbox:
– Applets can never run any local executable program.
– Applets cannot communicate with any host other than the server from
which they were downloaded; that server is called the originating host.
– Applets cannot read from or write to the local computer's file system.
– Applets cannot find out any information about the local computer,
except for the Java version used, the name and version of the operating
system, and the characters used to separate files (for instance, / or \),
paths (such as : or ;), and lines (such as \n or \r\n).
– In particular, applets cannot find out the user's name, e-mail address, and
so on.
– All windows that an applet pops up carry a warning message.

Java Programming CoSc3053 23


End of Chapter 2

Java Programming CoSc3053 49

You might also like