Post on 12-Jan-2016
Comp2513Comp2513
Java and AppletsJava and Applets
Daniel L. Silver, Ph.D.Daniel L. Silver, Ph.D.
2001 Daniel L. Silver 2
ObjectivesObjectives
To introduce the Java programming To introduce the Java programming language and its fit with the Weblanguage and its fit with the Web
To discuss the use of Applets as a part of To discuss the use of Applets as a part of the E-Commerce infrastructurethe E-Commerce infrastructure
References: Chapter 3References: Chapter 3
2001 Daniel L. Silver 3
OutlineOutline Java an Object Oriented Programming Java an Object Oriented Programming
languagelanguage Why Java and the Web?Why Java and the Web? Java AppletsJava Applets
2001 Daniel L. Silver 4
Programming LanguagesProgramming Languages A programming language adheres to a A programming language adheres to a
specified syntax that is accepted by either specified syntax that is accepted by either an interpreter or a compiler.an interpreter or a compiler.
What is the difference between an What is the difference between an interpreter and a compiler?interpreter and a compiler?
Basic code
Interpreter Compiler
C code
Operating System
Computer Hardware
*.exe
2001 Daniel L. Silver 5
Function-Oriented ProgrammingFunction-Oriented Programming
Traditional programming languages are known as Traditional programming languages are known as function-oriented. Why?function-oriented. Why?– Consider the movement of data from function to Consider the movement of data from function to
functionfunction– Data and processing are considered separatelyData and processing are considered separately
What are some examples of function-oriented What are some examples of function-oriented languages?languages?– COBOLCOBOL– BasicBasic– FortranFortran– CC
• Prone to misuse of data and process elements• Not supportive of encapsulation, cohesion and loose coupling
2001 Daniel L. Silver 6
Object-Oriented ProgrammngObject-Oriented Programmng
Combines data and processes together into Combines data and processes together into objects objects
An object is an entity that can contain data An object is an entity that can contain data (attributes, properties) and can manipulate (attributes, properties) and can manipulate data using functions (methods)data using functions (methods)
An object has state and behaviourAn object has state and behaviour What are some examples of OO lang.?What are some examples of OO lang.? A simple example … Hello_app.javaA simple example … Hello_app.java
Attributes
Methods
2001 Daniel L. Silver 7
Java: An OOP LanguageJava: An OOP Language
Java is a relatively new language Java is a relatively new language ““Green”, 1991 Sun Microsystems – dev. for Green”, 1991 Sun Microsystems – dev. for
use in consumer devices such as intelligent use in consumer devices such as intelligent TV controllers TV controllers – Object Oriented but simpler than C++Object Oriented but simpler than C++– Architecture neutralArchitecture neutral– Real-time remote applicationsReal-time remote applications– Portable, Reliable and SecurePortable, Reliable and Secure
2001 Daniel L. Silver 8
Java and the Java and the Java Virtual MachineJava Virtual Machine
program.class
Java Virtual Machine Compiler
program.java
Operating System
Computer Hardware
2001 Daniel L. Silver 9
Java Translation and ExecutionJava Translation and Execution
Java sourcecode
Java compiler
Java (JVM) Interpreter
Bytecode compiler
Java bytecode
Machine code
hello_app.java
hello_app.class
hello_app.exe
Efficientbecause of bytecode
javac
java
2001 Daniel L. Silver 10
Java and the WebJava and the Web
In 1995 the first HotJava browser was In 1995 the first HotJava browser was demoed at SunWorld exhibitiondemoed at SunWorld exhibition
It could download programs called It could download programs called applets applets from a the web and run them locallyfrom a the web and run them locally
Provided animation and interactionProvided animation and interaction ““Write once, run anywhere” Write once, run anywhere” By 1996 both Netscape and MS supported By 1996 both Netscape and MS supported
Java within their browsersJava within their browsers
2001 Daniel L. Silver 11
Java and the WebJava and the Web
Java has been designed for the WebJava has been designed for the Web It has two attributes that make it suitable:It has two attributes that make it suitable:
– Security:Security: An assortment of security features An assortment of security features that guarantee that no evil applets can be that guarantee that no evil applets can be written and assist in the writing of good codewritten and assist in the writing of good code
– Portability:Portability: Applications and Applets can Applications and Applets can run on Windows, Unix, Linux, Mac, IBM mid-run on Windows, Unix, Linux, Mac, IBM mid-range and mainframerange and mainframe
2001 Daniel L. Silver 12
Java Applet SecurityJava Applet Security
Programs that come from over the network can be Programs that come from over the network can be malicious (destroy data on your PC)malicious (destroy data on your PC)
Java was designed to prevent malicious behaviourJava was designed to prevent malicious behaviour Two primary safety features:Two primary safety features:
– SignaturesSignatures – an applet can be signed and a browser can – an applet can be signed and a browser can be set up to accept only trusted applet authorsbe set up to accept only trusted applet authors
– Secuirty PriviledgesSecuirty Priviledges – by default an applet runs in the – by default an applet runs in the “sandbox” where I/O is limited to the keyboard/mouse “sandbox” where I/O is limited to the keyboard/mouse and the display, trusted applets can be give higher level and the display, trusted applets can be give higher level priveledges (e.g. disk I/O )priveledges (e.g. disk I/O )
2001 Daniel L. Silver 13
Java and the WebJava and the Web
Reasons why Java has become the fastest Reasons why Java has become the fastest growing programming language of all time:growing programming language of all time:– It is an object-oriented languageIt is an object-oriented language– Contains a vast library of software (object Contains a vast library of software (object
classes and methods) classes and methods) » Java Development Kit (J2SE SDK)Java Development Kit (J2SE SDK)
» Objects that have been developed and testedObjects that have been developed and tested
» Imported for use at the beginning of a program Imported for use at the beginning of a program
– A good first language to learn ... Why?A good first language to learn ... Why?
2001 Daniel L. Silver 14
Java Applications Java Applications vsvs Applets Applets
There are two classes of Java programs:There are two classes of Java programs: Applications - such as Hello_app.javaApplications - such as Hello_app.java
– I/O is by default with console and character orientedI/O is by default with console and character oriented– Graphical I/O is an option that is commonly takenGraphical I/O is an option that is commonly taken
Applets – as we shall seeApplets – as we shall see– Run by a Web browser using an imbedded Java Run by a Web browser using an imbedded Java
interpreterinterpreter– Graphical I/O – characters, diagramsGraphical I/O – characters, diagrams
2001 Daniel L. Silver 15
Java Translation and ExecutionJava Translation and Execution
Java sourcecode
Java compiler
Java appletbytecode
Internet
Internet
Java (JVM)Interpreter
Web Browser
Client
Server
abc.java
abc.class HTTP ServerApp Server
2001 Daniel L. Silver 16
Java AppletsJava Applets
A Simple Example:A Simple Example: HelloApplet.javaHelloApplet.java / / HelloApplet.htmlHelloApplet.html … … Nothing is passed to the applet from the HTMLNothing is passed to the applet from the HTML Anatomy of an Applet – fundamental methods:Anatomy of an Applet – fundamental methods:
– init() – invoked once when applet is first loadedinit() – invoked once when applet is first loaded
– start() – invoked each time applet becomes visiblestart() – invoked each time applet becomes visible
– stop() – invoked each time applet becomes invisiblestop() – invoked each time applet becomes invisible
– paint() – display of text and graphicspaint() – display of text and graphics
– destroy() – invoked once when applet is exiteddestroy() – invoked once when applet is exited
2001 Daniel L. Silver 17
Java AppletsJava Applets
Parameter Passing Example:Parameter Passing Example: FirstApplet.java / FirstApplet.html …FirstApplet.java / FirstApplet.html … <PARAM> tag is used to pass parameters from <PARAM> tag is used to pass parameters from
HTML to Java program at run time HTML to Java program at run time <PARAM> has two attributes:<PARAM> has two attributes:
– NAME - name of parameter being passedNAME - name of parameter being passed
– VALUE – value of parameter being passedVALUE – value of parameter being passed
- e.g.:e.g.:<PARAM NAME="message" VALUE="Message from HTML File"><PARAM NAME="message" VALUE="Message from HTML File">
2001 Daniel L. Silver 18
Java AppletsJava Applets
The Graphics object class allows you to do The Graphics object class allows you to do many things: many things: – setColor()setColor()– fillRect()fillRect()– drawLine()drawLine()– drawRect()drawRect()– drawOval()drawOval()– drawImage()drawImage()
2001 Daniel L. Silver 19
Java AppletsJava Applets
Threads and Event Handling Example:Threads and Event Handling Example: AnimationApplet.java / AnimationApplet.java /
AnimationApplet.html …AnimationApplet.html … Thread – a portion of a Java program that Thread – a portion of a Java program that
executes independently, e.g.:executes independently, e.g.:– Thread 1 - allows animation to occur on the Thread 1 - allows animation to occur on the
browser windowbrowser window– Thread 2 – captures input from the keyboard Thread 2 – captures input from the keyboard
2001 Daniel L. Silver 20
Java AppletsJava Applets
Threads and Event Handling Example:Threads and Event Handling Example: Event Handling is used within programs to Event Handling is used within programs to
associate events such as mouse clicks to associate events such as mouse clicks to revelent portions of coderevelent portions of code
In AnimationApplet.java a mouseDown() In AnimationApplet.java a mouseDown() event is used to start and stop the scrolling event is used to start and stop the scrolling messagemessage
2001 Daniel L. Silver 21
Java AppletsJava Applets
A more advanced example – link.htmlA more advanced example – link.html– Provides sources of further information on Java Provides sources of further information on Java
and Appletsand Applets Notice how in link.java:Notice how in link.java:
– <param> is used in this example<param> is used in this example– showDocument(theURL,targetFrame) is used to showDocument(theURL,targetFrame) is used to
link to a URLlink to a URL
THE ENDTHE END
danny.silver@acadiau.cadanny.silver@acadiau.ca