Presentation is loading. Please wait.

Presentation is loading. Please wait.

13-1 Programming in Java Java Applet Lecture13. 13-2 Programming in Java 1. Introduction to Applet 2. Running of Applet 3. Applet tags in HTML 4. Environment.

Similar presentations


Presentation on theme: "13-1 Programming in Java Java Applet Lecture13. 13-2 Programming in Java 1. Introduction to Applet 2. Running of Applet 3. Applet tags in HTML 4. Environment."— Presentation transcript:

1 13-1 Programming in Java Java Applet Lecture13

2 13-2 Programming in Java 1. Introduction to Applet 2. Running of Applet 3. Applet tags in HTML 4. Environment of Applet running 5. Both Application and Applet Objectives

3 13-3 Programming in Java Java Technologies

4 13-4 Programming in Java Brower Java Virtual Machine Server class files Server delivers HTML data stream (text and tags) Java applet is referenced in the HTML Browser parses data and displays on Client Browser requests applet class file(s) Browser loads applet into JVM JVM executes applet code How Java Applets work(1)

5 13-5 Programming in Java How Java Applets work(2)

6 13-6 Programming in Java Applet - A Java program that is referenced by a Web page and runs inside a Java-enabled Web browser Application - A stand-along Java program that runs via the “java” command Page Brower VM Applet VM Application Java Applet & Java Application(1)

7 13-7 Programming in Java FeatureApplication Applet Starting the Program Java classFileName (from a command prompt) Browser Web Page Browser Open HTML File Appletviewer htmlFileName Parameters from command prompt(args[0]…) from HTML file (PARAM…) Has a main() YESNO Supports GUI YES Security Restriction NO YES Java Applet & Java Application(2)

8 13-8 Programming in Java Some applets possibilities: Communicate with web server Dynamic content generation "Real-time" information Advantages: - Full GUI control possible - Good security model - Complete programming language - Component reuse possible Disadvantage: - Not all browsers support all Java levels - Users may have Java support disabled - Class downloads can be time-consuming - Security model can be limiting Java Applet Characteristics

9 13-9 Programming in Java Restrictions and Security(1)

10 13-10 Programming in Java Restrictions and Security(2)

11 13-11 Programming in Java What is Sandbox?

12 13-12 Programming in Java Restrictions and Security(3)

13 13-13 Programming in Java Restrictions and Security(4) No built-in notion of trusted code,each applet runs with separate permissions

14 13-14 Programming in Java In Java2, an object of the java.security.Policy class now represents the security policy for all kinds of Java programs Signed Charles applet Unsigned Charles applet Native program … R/W /tmp/ Connect and receive URL R/W /tmp/ and /home/ … Code Permission Permissions and Policy Files (1)

15 13-15 Programming in Java Permissions and Policy Files(2)

16 13-16 Programming in Java Applet Creation and Running Browers which support Java(HotJava 、 Netscape Navigator 、 IE) Applet viewer(\jdk\bin\) – –Applet viewer [option] urls… – –Only can read tags such as OBJECT 、 EMBED 、 APPLET ( can’t read other HTML tags ) import javax.swing.*; public class Hello extends JApplet{ …} Applet creation JApplet creation import java.applet.*; public class Hello extends Applet{ …}

17 13-17 Programming in Java Applet Classes javax.swing.JFrame java.lang.Object java.awt.Component java.awt.Container java.awt.Windowsjava.awt.Panel java.awt.Framejava.applet.Applet javax.swing.JApplet AppletStub AppletClip AppletContext Each Applet inherits from java.applet.Applet/javax.swing.JApplet

18 13-18 Programming in Java init() -Invoked at applet downloaded and implement to transform paras from HTML to Applet (initialization) start() -Invoked at viewing the page, and could be invoked again and again stop() -Invoked at leaving the page, and could be invoked again and again also destroy() -Invoked at closing the browser Applet Lifecycle(1)

19 13-19 Programming in Java Constructor start stop destroy init Loading an Applet Creates an instance by invoking the constructor Browser invokes init() method Browser invokes start() method Leaving and returning to the page Browser invokes stop() method Browser invokes start() method Reloading the page stop() - suspend any activity destroy() - free resources (threads stopped) Ctor-Create new Applet (init() start()) Applet Lifecycle(2)

20 13-20 Programming in Java Necessary Tags Applet’s Tag in HTML <APPLET CODE= WIDTH= HEIGT= > Three information about Applet - -Name of Class file - -Location of Class file - -How to representation Applet in page Applet Tags in HTML(1)

21 13-21 Programming in Java Other Applet’s Tag Applet Tags in HTML(2) <APPLET [CODEBASE = codebaseURL] CODE=appletClassFile [ALT=alternativeText] [NAME=appletInstanceName] WIDTH = pixels HEIGHT = pixels [ALIGN = alignment ] [VSPACE = pixels] [HSPACE = pixels]> [ ] …… [HTML displayed in the absence of Java]

22 13-22 Programming in Java <APPLET CODE = “CalculatorApplet.class” ARCHIVE = “CalculatorClasses.jar, swing.jar” WIDTH = 100 HEIGHT = 150   using Swing; but on Java1.1 download Swing patch from network(swing.jar) ARCHIVE - list Java jar files,including classes and other resourceJava jar files Applet Tags in HTML(3)

23 13-23 Programming in Java 多数 Applet 的执行需使用不止一个类 Java 支持一个下载类文件的方法,可以把所需要的类文件、 图象声音文件、文本文件和二进制数据文件打包成一个单 一文件( Java Archive 文件和 JAR 文件) 用 JDK 提供的 jar 工具制作 JAR 文件 jar [ctxuvfmOM] [jar 文件名 ] [mainfest 文件名 ] [-C 目录 ] 文件名 … 例如: jar cf ShapeClasses.jar *.class 用一个存在的清单文件 mymainifest 将 foo/ 目录下的所有 文件归档到一个名为 classes.jar 的归档文件中 jar cvfm classes.jar mymainfest –C foo/. JAR file return

24 13-24 Programming in Java Applet can get parameters from html file Tag PARAM –HTML 文件 –APPLET 文件 String getParameter(“Param_Name”) HTML Transfer Parameters to Applet from HTML

25 13-25 Programming in Java Java Code public class Demo extends Applets { public void paint(Graphics g){ String fontName=getParameter(“font”); String sizeName=getParameter(“size”); int fontSize=Integer.parseInt(sizeName); Font fnt=new Font(fontName,Font.BOLD,fontSize); g.setFont(fnt); g.drawString(“Demo”,40,50); } Example

26 13-26 Programming in Java Get the HTML runtime environment –getAppletContext() [java.applet.Applet] Get an Applet –public Applet getApplet(String name); Get all Applets on the same HTML –public Enumeration getApplets() Interaction Between Applets in HTML

27 13-27 Programming in Java Example(1) import java.applet.*; import java.awt.*; import java.awt.event; import java.util.Enumeration; import javax.swing; public class GetApplets extends JApplet implements ActionListener { private TextArea textarea; private String newline; public void init() { container contentPane= getContentPane(); Button b=newButton(“Click to call getApplets()”); b.addActionListener(this); contentPane.add(“North”,b);

28 13-28 Programming in Java Example(2) textArea = new TextArea(5,40); textarea.setEditable(false); contentPane.add(“center”, textArea); newLine=System.getProperty(“line.separator”);} public void actionPerformed(ActionEvent e) { printApplets(); } public void printApplets() { Enumeration e=getAppletContext().getApplets(); textArea.append(“Result:” + newLine); while (e.hasMoreElements()) {Applet applet=(Applet)e.nextElement(); textArea.append(“-”+applet.getClass().getName()+ newLine);} textArea.append(“-”+newLine); }

29 13-29 Programming in Java Get Applet URL from browser –URL getCodeBase() Get HTML URL which embedded Applet from browser – URL getDocumentBase() Get PARAM value by name inHTML –String getParameter(String name) Get Applet receiving parameters –String[] getParameterInfo() Get this Applet information,such as applet designer,version –String[] getParameterInfo() Show a new web page in the browser –Use method ShowDocument() of Class AppletContext –ShowDocument() have two parameters, first is URL, second is where display new page. Interaction Between HTML and Applet

30 13-30 Programming in Java import java.applet.*; import java.awt.*; import java.net.*; public class Bookmark extends Applet { private List links; public boolean action(Event evt, Object arg) { if(evt.target==links) { try{ AppletContext context=getAppletContext(); URL u = new URL((String)arg); context.showDocument(u,"right");} catch(Exception e){showStatus("Erro"+e); }} else{return super.action(evt,arg);} return true; } Example(1)

31 13-31 Programming in Java public void init() { setLayout(new BorderLayout()); links=new List(5,false); int i=1; String s; while ((s=getParameter("link_"+i))!=null){ links.addItem(s); i++; } add("Center",links); } Example(2)

32 13-32 Programming in Java One Simple Bookmark Example(3)

33 13-33 Programming in Java How are Java Applets Used


Download ppt "13-1 Programming in Java Java Applet Lecture13. 13-2 Programming in Java 1. Introduction to Applet 2. Running of Applet 3. Applet tags in HTML 4. Environment."

Similar presentations


Ads by Google