24.1 Introduction Java Powerful, object-oriented language Fun to use for beginners, appropriate for experience programmers Language of choice for Internet and network communications In the Java chapters, we discuss Graphics (and graphical user interfaces [GUI] ) Multimedia Event-driven programming Free implementation at http://java.sun.com
41
Embed
24.1Introduction Java Powerful, object-oriented language Fun to use for beginners, appropriate for experience programmers Language of choice for Internet.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
24.1 Introduction Java Powerful, object-oriented language Fun to use for beginners, appropriate for
experience programmers Language of choice for Internet and network
communications In the Java chapters, we discuss
Graphics (and graphical user interfaces [GUI] ) Multimedia Event-driven programming Free implementation at http://java.sun.com
24.2 Basics of a Typical Java Environment Java Systems
Consist of environment, language, Java Applications Programming Interface (API), Class libraries
Java programs have five phases Edit
Use an editor to type Java program vi or emacs, notepad, Jbuilder, Visual J++ .java extension
Compile Translates program into bytecodes, understood by
Interprets bytecodes in Welcome1.class (.class left out in java command)
Automatically calls main
Output types Command window Dialog box / Windows
24.4 A Simple Program: Printing a Line of Text (VI)
Packages Predefined, related classes grouped by directories on
disk All in directory java or javax, or subdirectories
Referred to collectively as the Java class library or the Java applications programming interface (Java API)
import - locates classes needed to compile program Class JOptionPane
Defined in package called javax.swing Contains classes used for a graphical user interface (GUI)
Facilitates data entry and data output import javax.swing.JOptionPane;
24.4 A Simple Program: Printing a Line of Text (VII)
Class JOptionPane Contains methods that display a dialog box
static method showMessageDialog First argument - null (more Chapter 29) Second argument - string to display
static methods Called using dot operator (.) then method name
JOptionPane.showMessageDialog(arguments);
exit - method of class System Terminates application, required in programs with GUIsSystem.exit( 0 );0 - normal exitnon-zero - signals that error occurred
Class System in package java.lang Automatically imported in every Java program
15
Applets Programming
Enabling Application Delivery Via the Web
16
Introduction
Applets are small Java programs that are embedded in Web pages.
They can be transported over the Internet from one computer (web server) to another (client computers).
They transform web into rich media and support the delivery of applications via the Internet.
Introduction to Applets
Applets are applications that are deployed over the Internet Designed to run inside a browser Are embedded in HTML pages Core part of Java
Not are popular as they were (or forecasted to be) Patchy browser support Can be slow to download Macromedia Flash, etc. offer similar functionality
Introduction to Applets
But do provides a number of benefits… Easy to deploy (“web components”) No need for installation or upgrades Provide more sophisticated functionality
than a web page/form Allow for proprietary client-server
protocols Re-use code from traditional applications Very secure
The Rules for Applets
An applet cannot (usually) do the following: Cannot load libraries or define native
methods Cannot read or write files on the client Cannot make network connections except
to the server it came from Cannot start any program on the client Cannot read certain system properties Cannot ‘pretend’ to be a local application
Applet windows look different
The Rules for Applets
Ensures that an applet cannot damage the client Otherwise opens potential for viruses, security
breaches, trojan horses, etc Applets are considered to be untrusted code
Rules are enforced by a Security Manager Installed by the JVM in the browser
The rules are known as a security policy Alternate policies can be used on request
But only if the user decides to trust the code
21
Applet: Making Web Interactive and Application Delivery Media
HelloHello
Hello Java<app=“Hello”>
4
APPLET Development “hello.java”
AT SUN.COM
The Internet
hello.class AT SUN’S
WEB SERVER
2 31 5
Create Applettag in
HTMLdocument
Accessing from
Your Organisation
The browser createsa new
window and a new thread
and then runs the
code
22
How Applets Differ from Applications
Although both the Applets and stand-alone applications are Java programs, there are certain restrictions are imposed on Applets due to security concerns:
Applets don’t use the main() method, but when they are load, automatically call certain methods (init, start, paint, stop, destroy).
They are embedded inside a web page and executed in browsers. They cannot read from or write to the files on local computer. They cannot communicate with other servers on the network. They cannot run any programs from the local computer. They are restricted from using libraries from other languages.
The above restrictions ensures that an Applet cannot do any damage to the local system.
public class HelloWorldApplet extends Applet { public void paint(Graphics g) { g.drawString ("Hello World of Java!",25,
25); }}
24
Embedding Applet in Web Page
<HTML>
<HEAD><TITLE> Hello World Applet</TITLE></HEAD>
<body><h1>Hi, This is My First Java Applet on the Web!</h1><APPLET CODE="HelloWorldApplet.class" width=500 height=400></APPLET></body>
</HTML>
25
Accessing Web page (runs Applet)
26
Applet Life Cycle
Every applet inherits a set of default behaviours from the Applet class. As a result, when an applet is loaded, it undergoes a series of changes in its state. The applet states include: Initialisation – invokes init() Running – invokes start() Display – invokes paint() Idle – invokes stop() Dead/Destroyed State – invokes destroy()
27
Applet States
Initialisation – invokes init() – only once Invoked when applet is first loaded.
Running – invokes start() – more than once For the first time, it is called automatically by the system
after init() method execution. It is also invoked when applet moves from idle/stop() state
to active state. For example, when we return back to the Web page after temporary visiting other pages.
Display – invokes paint() - more than once It happens immediately after the applet enters into the
running state. It is responsible for displaying output. Idle – invokes stop() - more than once
It is invoked when the applet is stopped from running. For example, it occurs when we leave a web page.
Dead/Destroyed State – invokes destroy() - only once This occurs automatically by invoking destroy() method
when we quite the browser.
28
Applet Life Cycle Diagram
Born
Running Idle
Dead
Begininit()
start()
paint()
stop()
start()destroy()
End
29
Passing Parameters to Applet
<HTML>
<HEAD><TITLE> Hello World Applet</TITLE></HEAD>
<body><h1>Hi, This is My First Communicating Applet on the Web!</h1><APPLET CODE="HelloAppletMsg.class" width=500 height=400> <PARAM NAME="Greetings" VALUE="Hello Friend, How are you?"></APPLET>
Applets are designed to operate in Internet and Web environment. They enable the delivery of applications via the Web.
This is demonstrate by things that we learned in this lecture such as: How do applets differ from applications? Life cycles of applets How to design applets? How to execute applets? How to provide interactive inputs?