Question? Leave a message!




Java applets

Java applets 15
Java applets SwIG Jing He Outline  What is Java  Java Applications  Java Applets  Java Applets Securities  SummaryWhat is Java  Java was conceived by James Gosling at Sun Microsystems Inc. in 1991  Java is platform independent language  Java programming is a/an objectoriented programming. Object oriented language The world around us consists of objects. e.g. the ATMObject oriented language The world around us consists of objects. Let the program consist of objects.Object oriented language The program consist of objects. Objects of the same kind form a class. E.g. class ATM or class Money.Object oriented language The program consist of objects. Objects of the same kind form a class. Each object has some methods. Money withdrawMoney(ATMCard card,int amount) (Objects in the same class have the same methods.)Object oriented language A method of the ATM class: parameters Money withdrawMoney(ATMCard card,int amount) type of return value name of the type of the parameter parameter myPurse.addMoney(theATM. withdrawMoney(myATMCard,1000));Object oriented language more ideas borrowed from the real world: encapsulation – you do not need to know how the ATM works inside. inheritance – you can easily create class ATMWithClocks extending class ATM. The new class inherits the methods of the ATM class.Java Architecture source code JVM Compiler byte code Computer programmer userportability security Java Architecure source code JVM Compiler Computer byte code speed programmer userWhy Java • simple • portable • secure • free • slowSo What’s Java Good For Web applications Java Applet Java Applet ServerLearning Java book, lectures • language • libraries documentation http://java.sun.com/docs/ examples on the web (problem – often old version of Java)How are Java Applications written  HelloWorld.java: public class HelloWorld public static void main (String args) System.out.println(“Hello, World”);  Compile HelloWorld.java javac HelloWorld.java Output: HelloWorld.class  Run java HelloWorld 15 Output: Hello, WorldBuilding JAVA Application  Prepare the file HelloWorld.java using an editor  Invoke the compiler: javac HelloWorld.java  This creates HelloWorld.class  Run the java interpreter: java HelloWorldWhat is an appletPIG PIGLETAPPLE APPLETWhat is an applet  An applet is a small Java program that is embedded and ran in some other Java interpreter program such as  a Java technologyenabled browser  Sun’s applet viewer program called appletviewer Applet Netscape(JVM) Loads HTML file Loads Java Applet TCP/IP (socket) TCP/IP (HTTP) Client Internet TCP/IP (HTTP) Server TCP/IP (socket) HTML files(Applet Tags) Applets Web ServerApplets, web page, client, server server host browser host web server browser reqeust for myWebPage.html myWebPage.html myWebPage.html HelloWorld.class ... request for applet code=HelloWorld.class/applet HelloWorldclass ... HelloWorld.class HelloWorld.classApplet Execution 1  An applet program is a written as a inheritance of the java.Applet class  There is no main() method in an Applet.   An applet uses AWT for graphicsApplet Execution 2  Life Cycle of an Applet:  init: This method is intended for whatever initialization is needed for an applet.  start: This method is automatically called after init method. It is also called whenever user returns to the page containing the applet after visiting other pages.  stop: This method is automatically called whenever the user moves away from the page containing applets. This method can be used to stop an animation.  destroy: This method is only called when the browser shuts down normally.  Ref: http://java.sun.com/docs/books/tutorial/deployment/applet/index.html/Applet Execution 3  The applet is running and rendered on the web page.  Every Applet needs to implement one or more of the init(), the start( ) and the paint( ) methods.  At the end of the execution, the stop( ) method is invoked, followed by the destroy( ) method to deallocate the applet’s resources.Applet life cycle  browser visits page containing an applet  browser calls init on that applet, once init()  browser calls start on that applet start()  browser goes away from that page  browser calls stop on that applet do some work  browser comes back to that page stop()  browser calls start again on that applet destroy()  browser shuts down  browser calls destroy on the applet, onceHTML tags for applets 1 APPLET // the beginning of the HTML applet code CODE="demoxx.class" // the actual name of the applet (usually a 'class' file) CODEBASE="demos/" // the location of the applet (relative as here, or a full URL) NAME=“SWE622" // the name of the instance of the applet on this page WIDTH="100" // the physical width of the applet on the page HEIGHT="50" // the physical height of the applet on the page ALIGN="Top" // align the applet within its page space (top, bottom, center) HTML tags for applets 2 APPLET CODE=“test.class" CODEBASE="example/" WIDTH=460 HEIGHT=160 NAME="buddy" PARAM NAME="imageSource" VALUE="images/Beans" PARAM NAME="backgroundColor" VALUE="0xc0c0c0" PARAM NAME="endImage" VALUE=10 /APPLET Applet's Attributes Attribute Explanation Example Code Name of class file Code=“applet0.class” Width Width of applet Width=300 height Height of applet Height=60 Codebase Applet’s Directory Codebase=“/applets” alt Alternate text if Alt=“menu applet” applet not available name Name of the applet Name=“appletExam” Align(top,left,right,bottom) Justify the applet with text Align=“right”HTML html head title Hello World Applet /title /head body applet code="HelloWorld.class” width=300 height=200 /applet /body /htmlYour Turn  You first applet  “Hello World”History of an Applet  Edit java source code html  notepad Hello.java  notepad Hello.html  Compile source to ByteCodes  javac Hello.java  produces Hello.class  View applet (Java Virtual Machine)  appletviewer Hello.html  browser Hello.htmlhtml body applet code=“Hello.class” width=300 height=300 /applet /body /htmlhtml body applet code=“Hello.class” width=300 height=300 /applet /body /html Save as Hello.htmlimport java.applet.Applet; import java.awt.; public class Hello extends Applet public void init() repaint(); public void paint(Graphics g) g.drawString(“Hello World”,30,30); where to draw it what to draw Save as Hello.javaHistory of an Applet  Edit java source code html  notepad Hello.java  notepad Hello.html  Compile source to ByteCodes  javac Hello.java  produces Hello.class  View applet (Java Virtual Machine)  appletviewer Hello.html  browser Hello.htmlHistory of an Applet  Edit java source code html  notepad Hello.java  notepad Hello.html  Compile source to ByteCodes  javac Hello.java  produces Hello.class  View applet (Java Virtual Machine)  appletviewer Hello.html  browser Hello.htmlThe coordinate system The applet is drawn in a rectangle, which consists of pixels. widthThe coordinate system Each pixel has a coordinate (x,y) (0,0) (width,0) xSample Graphics methods  A Graphics is something you can paint on g.drawString(“Hello”, 20, 20); Hello g.drawRect(x, y, width, height); g.fillRect(x, y, width, height); g.drawOval(x, y, width, height); g.fillOval(x, y, width, height); g.setColor(Color.red);Applet Security For security reasons, applets that are loaded over the network have several restrictions.  an applet cannot ordinarily read or write files on the computer that it's executing on.  an applet cannot make network connections except to the host that it came from.  Ref: http://java.sun.com/docs/books/tutorial/deployment/applet/index.html/What are the disadvantages of applets  Applets can’t run any local executable programs  Applets can’t with any host other than the originating server  Applets can’t read/write to local computer’s file systemWhat are the disadvantages of applets (Cont’d)  Applets can’t find any information about the local computer  All javacreated popup windows carry a warning message  Stability depends on stability of the client’s web server  Performance directly depend on client’s machineWhat are the advantages of applets  Automatically integrated with HTML; hence, resolved virtually all installation issues.  Can be accessed from various platforms and various javaenabled web browsers.  Can provide dynamic, graphics capabilities and visualizations  Implemented in Java, an easytolearn OO programming languageWhat are the advantages of applets (Cont’d)  Alternative to HTML GUI design  Safe Because of the security built into the core Java language and the applet structure, you don’t have to worry about bad code causing damage to someone’s system  Can be launched as a standalone web application independent of the host web serverSummary  An applet is a Java class  Its code is downloaded from a web server  It is invoked by a browser when it scans a web page and encounters a class specified with the APPLET tag  For security reason, the execution of an applet is normally subject to restrictions:  applets cannot access files in the file system on the client host  Applets cannot make network connection exception to the server host from which it originated Resources  http://java.sun.com/  Javatm 2 Platform, Standard Edition v1.6.1  java, javac, jar, jre, etc.  Any platform... FREE  Online documentation and tutorials  http://www.eclipse.org/  Integrated development environment (IDE) for nothing in particular  Javatm development tools (JDT) (comes with Eclips)  Project management  Editor  Incremental compiler  CVS support  C/C++ extension in progress  AspectJ support  Windows, Linux, and Mac.... FREE
sharer
Presentations
Free
Document Information
Category:
Presentations
User Name:
GraceRogers
User Type:
Professional
Country:
Greece
Uploaded Date:
12-07-2017