COMP201 Java Programming Part II: GUI Programming Topic 11: Applets Chapter 10.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming Lecture 10 Applets.
Advertisements

Learn about Java applets. Know the differences between Java applets and applications. Designing and using Java applets Running Java applets. Security.
1 Chapter 17 Applets and Multimedia. 2 Motivations F When browsing the Web – you frequently see the graphical user interface –animation developed using.
1 Frameworks. 2 Framework Set of cooperating classes/interfaces –Structure essential mechanisms of a problem domain –Programmer can extend framework classes,
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 18 Applets and Multimedia.
Slides prepared by Rose Williams, Binghamton University Mouse Events + Chapter 17: Applets.
Applets. The Applet Class public class MyApplet extends java.applet.Applet {... /** The no-arg constructor is called by the browser when the Web page.
Review of Java Applets Vijayan Sugumaran Decision and Information Sciences Oakland University.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 16 Applets.
18-Jun-15 Applets. 2 An applet is a program that is typically embedded in a Web page and can be run from a browser You need special HTML in the Web page.
1 Applets. 2 Motivations When browsing the Web, you frequently see the graphical user interface and animation developed using Java. These programs are.
1 Applets Chapter 1 To understand:  why applets are used to extend the capabilities of Web pages  how an applet is executed and know about the restrictions.
COMP201 Java Programming Part II: GUI Programming Topic 11: Applets Chapter 10.
Java Applets. Lecture Objectives Learn about Java applets. Know the differences between Java applets and applications. Designing and using Java applets.
Applets. An applet is a Panel that allows interaction with a Java program A applet is typically embedded in a Web page and can be run from a browser You.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L06 (Chapter 16) Applets.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 14 Applets, Images,
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 16 Applets and Multimedia.
26-Jun-15 Applets. 2 An applet is a Panel that allows interaction with a Java program A applet is typically embedded in a Web page and can be run from.
28-Jun-15 Applets. 2 An applet is a program that is typically embedded in a Web page and can be run from a browser You need special HTML in the Web page.
Applets. What is an Applet?  According to Sun “An applet is a small program that is intended not to be run on its own, but rather to be embedded inside.
COMP201 Java Programming Part II: GUI Programming Topic 11: Applets Chapter 10.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L05 (Chapter 16) Applets.
Applets  The Applet Class  The HTML Tag F Passing Parameters to Applets.
Applets Java API.
Chapter 12 Applets and Advanced Graphics  The Applet Class  The HTML Tag F Passing Parameters to Applets F Conversions Between Applications and Applets.
19-Sep-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming Design Topic : Applets Maj Joel Young
JAVA APPLETS By: Ms. Humaira Siddiqui. Java and the Internet Java is considered to be a language well suited to be used in the internet. In contrast with.
Applets Chapter 17.  Java’s big splash onto the scene came in the mid 90’s. The people at Sun Microsystems had managed to work java programs into Web.
JAPPLET.
Applets Life Cycle Drawing and Event Handling Methods for UI Components Applet Capabilities Example.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
1 Introduction to Applets. 2 Applications Programs that execute using the java interpreter Executes in command windows.
Chapter 17: Applets, Images, and Sound. Objectives Learn about applets Write an HTML document to host an applet Use the init() method Work with JApplet.
Chapter 8 Introduction to HTML and Applets Fundamentals of Java.
Chapter 12 Applets and Advanced GUI  The Applet Class  The HTML Tag F Passing Parameters to Applets F Conversions Between Applications and Applets F.
Chapter 10: Applets and Advanced Graphics The Applet Class The Applet Class The HTML Tag The HTML Tag Passing Parameters to Applets Passing Parameters.
CSC 205 – Java Programming II Applet. Types of Java Programs Applets Applications Console applications Graphics applications Applications are stand-alone.
Pravin Yannawar, DOCS, NMU Jalgaon Basic Java : Applets 2 Objectives of This Session Identify the need for Applets Distinguish between Applets and Applications.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 16 Applets and Multimedia.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved GUI: Applets and Multimedia.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Applets and Multimedia.
Chapter 6 Applets and HTML  Overview  HTML tags for Applets  Applet Life Cycle  Applet Class  JAR files.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 16 Applets and Multimedia.
Applets and Frames. Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 2 Applets Usually.
Chapter 10: Applets and Advanced Graphics The Applet Class The Applet Class The HTML Tag The HTML Tag Passing Parameters to Applets Passing Parameters.
Chapter 14 Applets and Advanced GUI  The Applet Class  The HTML Tag F Passing Parameters to Applets F Conversions Between Applications and Applets F.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
Creating Applets. What is an applet? What is an applet? A Java program that runs in a web browser. A Java program that runs in a web browser. An applet.
1 Applets are small applications that are accessed on an Internet server, transported over the internet, automatically installed and run as a part of web.
1 Applets. 2 Design of Applets 3 Sun wrote Java to be executable within a hosting application browser The applications are applets. An applet is downloaded.
1 Applications & Applets Standalone applications & Java applets Peter Mozelius DSV/UCSC.
Chapter 131 Applets and HTML Chapter Objectives learn how to write applets learn to write a simple HTML document learn how to embed an applet in.
Applets, Images, and Audio Chapter 14 CSCI CSCI 1302 – Applets, Images, and Audio2 Outline Introduction The Applet Class –The init Method –The start.
Chapter 12: Accessing the Web URL (Uniform Resource Locator) class Applet methods –for audio clips –for images –context interface.
1 Applets. 2 What is an applet? Applet: a Java program that can be inserted into a web page and run by loading that page in a browser brings web pages.
Introduction to Applets Chapter 21. Applets An applet is a Java application that is intended to be invoked and executed through a Web browser. Click Here.
CHAPTER Agenda Applets Servelets Browsers HelloWorld.
Graphical User Interface (GUI)
1 Applets Programming. Introduction Java programs are divided into two main categories, applets and applications. An application is an ordinary Java program.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 17 : Applets King Fahd University of Petroleum & Minerals College of Computer Science.
Chapter 14 Applets. Applets: Introduction Java programs are divided into two main categories, applets and applications An application is an ordinary Java.
Java Applets.
Chapter 17 Applets and Multimedia
30 Java Applets.
UNIT-5.
Introduction to Computing Using Java
Java applets 1/3/2019.
Applet in Java.
JAVA APPLET PREPARED BY Mr. Jahanzaib Ahmed
Presentation transcript:

COMP201 Java Programming Part II: GUI Programming Topic 11: Applets Chapter 10

COMP201 Topic 11 / Slide 2 Introduction l Applets are java programs that run within browsers l Example: n Jmol applet – n NASA 3D real-time satellite tracker – Applet Client BrowserServer Applet User

COMP201 Topic 11 / Slide 3 An Example l Creation n Converting applications to applets l Transportation n Jar files: Move applets from servers to browsers quickly l Operation n Applet life cycle n Security restrictions n Getting resources from home n Communicating with browser Applet Client BrowserServer Applet User

COMP201 Topic 11 / Slide 4 An Example An applet is a Java class which extends java.applet.Applet If Swing components are used, the applet must extend from javax.swing.JApplet public class NotHelloWorldApplet extends JApplet { public void init() { JLabel label = new JLabel("Not a Hello, World applet", SwingConstants.CENTER); add(label); } } //NotHelloWorldApplet.java

COMP201 Topic 11 / Slide 5 l Compile and run Compile: javac NotHelloWorldApplet.java n Run: –Create a HTML file that tells the browser which file to load and how to size the applet This is an example. This text shown if browser doesn’t do Java. –View the HTML file with a browser or the command appletviewer n Note: –Textpad: Cntrl+3 – creates a simple html file and show it with appletviewer An Example

COMP201 Topic 11 / Slide 6 An Example l More notes n To view applet, one needs Java 2 enabled browser (recent version of IE and Netscape, e.g. IE 6, Netscape 6, Netscape 7. Netscape 4 is not Java 2 enabled) n Class files are cached by browser. –Restart browser after each modification –Alternatively, one can clear the cache from Java console, which can be accessed from Netscape or control panel on Windows

COMP201 Topic 11 / Slide 7 An Example

COMP201 Topic 11 / Slide 8 class NotHelloWorldFrame extends JFrame { public NotHelloWorldFrame() { setTitle("NotHelloWorld"); setSize(300, 200); JLabel label = new JLabel("Not a Hello, World application", SwingConstants.CENTER); add(label); }} public class NotHelloWorldApplication { public static void main(String[] args) { NotHelloWorldFrame frame = new NotHelloWorldFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }} Compare with appl.: NotHelloWorld.java

COMP201 Topic 11 / Slide 9 l Applets are created, run, and destroyed by web browser n Don’t set size for an applet: determined by HTML file. n Don’t set title for an applet: applets cannot have title bars. –Can have menus. No need to explicitly construct an applet. Construction code placed inside the init method. There is no main method. n An applet cannot be closed. It terminates automatically when the browser exit No need to call method setVisible(true). An applet is displayed automatically. An Example

COMP201 Topic 11 / Slide 10 Outline l An example l Creation n Converting applications to applets l Transportation n Jar files: Move applets from servers to browsers quickly l Operation n Applet life cycle n Security restrictions n Getting resources from home n Communicating with browser

COMP201 Topic 11 / Slide 11 Creating Applets from Applications l Non-IO applications for now n Pop up window for application n Embed top-level frame of application inside browser

COMP201 Topic 11 / Slide 12 Popup Applets from Applications l Popping up a window for application. n Assume: Separate class for creating and showing a top-level frame. (If this class also does some other things, move the other things to other classes.) class NotHelloWorldFrame extends JFrame {…} public class NotHelloWorldApplication { public static void main(String[] args) { JFrame frame = new NotHelloWorldFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_ CLOSE); frame.setVisible(true); }

COMP201 Topic 11 / Slide 13 l Steps of conversion: n Delete the class for creating and showing the top-level frame Add an applet class whose init method contains the same instructions as main method of deleted class. n Remove code for closing window public class NHWApplet extends JApplet { public void init() { JFrame frame = new NotHelloWorldFrame(); frame.setVisible(true); } } //NHWApplet.java n The popup window coming with a warning message for security reasons, (which can be avoided for signed applets). Creating popup Applets from Applications

COMP201 Topic 11 / Slide 14 l Steps of conversion n Delete the class for creating and showing the top-level frame n Convert the top-level frame class into an applet –JFrame class => JApplet class; must be public –Remove setSize: set in HTML file –Remove setTitle: Applet cannot have title bar –Replace constructor with init. Creating Embedded Applets from Applications

COMP201 Topic 11 / Slide 15 public class PopupCalculatorApplet extends JApplet { public void init() { // create a frame with a calculator panel final JFrame frame = new JFrame(); frame.setTitle("Calculator"); frame.setSize(200, 200); frame.add(new CalculatorPanel()); // add a button that pops up or hides the calculator JButton calcButton = new JButton("Calculator"); add(calcButton); calcButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { frame.setVisible(!frame.isVisible());} }); }} Example: Embedded Calculator Applet

COMP201 Topic 11 / Slide 16 A Calculator Click this button to pop up a calculator. <applet align="middle" code="PopupCalculatorApplet.class" width="100" height="20"> HTML of the Embedded Calculator Applet

COMP201 Topic 11 / Slide 17 import java.awt.*; import javax.swing.*; public class CalculatorApplet extends JApplet { public void init() { CalculatorPanel panel = new CalculatorPanel(); add(panel); }} //CalculatorApplet.java A Calculator Here is a calculator, just in case you can't find yours. //Calculator.html Example: Popup Calculator Applet

COMP201 Topic 11 / Slide 18 Outline l An example l Creation n Converting applications to applets l Transportation n Jar files: Move applets from servers to browsers quickly l Operation n Applet life cycle n Security restrictions n Getting resources from home n Communicating with browser

COMP201 Topic 11 / Slide 19 Transportation of Applets via JAR Files l PopupCalculatorApplet involve three classes n CalculatorFrame.class, CalculatorPanel.class PopupCalculatorApplet.class l HTML file contains name of the applet class n l Class loader First fetches PopupCalculatorApplet.class In the process, it notices that some other classes are also needed. It then makes net connections to get them. n Many connections might be needed in general, especially when there are associated resources such as images and sounds.

COMP201 Topic 11 / Slide 20 JAR Files l Java supports an improved method for loading class files. It allows you to package all the needed class files into as single JAR file which could be downloaded with a single HTTP request to the server. l A JAR file is simply a zip file that contains classes, other files that a program may need and a manifest file, which describes special features of the archive. l The manifest file is called MANIFEST.MF and is located in a special META-INF subdirectory of the JAR file. l A manifest file can have many entries which are groouped into section. The first section is called the main section, which applies to the whole JAR file. Subsequent entries specify properties of named entities such as individual files, packages, or URLs.

COMP201 Topic 11 / Slide 21 JAR File l An example of manifest file: Manifest-Version:1.0 Lines describing this archive Name:Woozle.class Lines describing this file Name: com/myCompany/mypkg/ Lines describing this package l To make an new JAR file with a manifest run jar cfm MyArchive.jar mainclass.mf *class l To add items to the manifest, place the additions into a text file and use a command jar umf MyJarFile.jar manifest-additions.mf

COMP201 Topic 11 / Slide 22 Jar Files l Creating jar files n jar cvf PopupCalculatorAppletClasses.jar *class l In general: jar cvf myJarFile.jar *.class *.gif pack all files ending with.class or.gif

COMP201 Topic 11 / Slide 23 Jar Files Refer to JAR files in the APPLET tag l JAR file is downloaded via one net connection. l Class loader tries to find necessary files in JAR file before attempting to get them from the net.

COMP201 Topic 11 / Slide 24 Diversion/Self-Running Jar File l Just as with applets, when you ship an application, you should also package the class files and other resources required by the program in a Jar file. l You can even set it up to be Self-Running (executable) jar file. In that case you need to indicate the main class in the manifest file. 1. Create mainclass.mf with one line : Main-Class: MyApplet 2. Create the JAR file with the manifest file plus the update from the mainclass.mf 1.jar cvfm MyJarFile.jar mainclass.mf *class 3. Or, one can update the manifest files of an existing jar file jar umf mainclass.mf MyJarFile.jar l Run: n java -jar MyJarFile.jar n Or click on JAR file icon

COMP201 Topic 11 / Slide 25 Outline l An example l Creation n Converting applications to applets l Transportation n Jar files: Move applets from servers to browsers quickly l Operation n Applet life cycle n Security restrictions n Getting resources from home n Communicating with browser

COMP201 Topic 11 / Slide 26 Applet Life Cycle l An application starts from main and runs until exit l Applets are controlled by browser through 4 methods n init() –Called when loaded by browser n start() –Called right after init and when user return to page n stop() –Called when user moves off page n destroy() –Called when browser shuts down. l Overwrite the methods to control applet behavior non-existenton page off page init( ) destroy( ) stop( ) destroy( )start( )

COMP201 Topic 11 / Slide 27 l public void init() n One-time initialization when first loaded n Good place to process parameters and add interface components. l public void start() n Called whenever user returns to the page containing the applet after having gone off to other pages. n Can be called multiple times. n Good place to resume animation or game Applet Life Cycle

COMP201 Topic 11 / Slide 28 l public void stop() n Called when user moves off page (to other pages) n Good place to stop time-consuming activities such as animation and audio playing. l public void destroy() n Called when browser shuts down. n Good place to reclaim non-memory-dependent resources such as graphics contexts. n Normally, no need to worry. l Example: sound (Stop Playing when going out of page) Compare with the one of the two other versions. Applet Life Cycle

COMP201 Topic 11 / Slide 29 Outline l An example l Creation n Converting applications to applets l Transportation n Jar files: Move applets from servers to browsers quickly l Operation n Applet life cycle n Security restrictions n Getting resources from home n Communicating with browser

COMP201 Topic 11 / Slide 30 Security Restrictions l Applets are downloaded from the net and executed by a browser’s JVM immediately. l User never gets a chance to confirm or to stop an applet from running. l Consequently, applets are restricted in what they can do. The applet security manager is responsible for enforcing access rules and throws an SecurityException when an access rule is violated.

COMP201 Topic 11 / Slide 31 Security Restriction l By default, an applet is restricted to run “inside the sandbox”. Strictest security restrictions. l Signed applets can have more access privileges. l For now, we consider only applets playing in the sandbox.

l Access rights for Applets and Java Applications (JA) BR: applets running inside a browser with default applet security model AV: applets running insider Applet viewer BR Read local file N Write local file N Get file info. N Delete file N Run another program N Read theuser.name property N Connect to network port on home server Y Connect to network port on other server N Load Java library N Call exit N Create a pop-up window warning AVJA YY YY YY NY YY YY YY YY YY YY YY

COMP201 Topic 11 / Slide 33 Outline l An example l Creation n Converting applications to applets l Transportation n Jar files: Move applets from servers to browsers quickly l Operation n Applet life cycle n Security restrictions n Getting resources from home n Communicating with browser Applet Client BrowserServer Applet User

COMP201 Topic 11 / Slide 34 Resources for Applets Classes in both applets and applications often use associated data file, such as image, sound file, Text file, binary file. In Java those associated files are called resources. 1. One can provide information to applets in HTML file 2. Application/Applets can access resources at home server:

COMP201 Topic 11 / Slide Passing Info to Applets via HTML File l In HTML file, use PARAM, NAME, VALUE tags …. In applet, use the getParameter method of the Applet class getParameter("title"); //returns "Diameters of the Planets “ String vString = getParameter(“values”); //returns “9” if (vString == null ) {do something} // precaution else int v=Integer.parseInt(vString); //must parse to get numbers Chart.java, Chart.html

COMP201 Topic 11 / Slide Accessing Resources inside a class The class loader knows how to search for class files from the class path, or in an archive, or on a web server. You can call the getResource(filename) to get files that are not class files. x.class gives one an object of the Class class that contain information of x. Class is a special class and has method getResource. C.f. Object class x.class.getResource( resourceName ) returns URL of resource

COMP201 Topic 11 / Slide 37 Applets can handle images in GIF, PNG or JPEG format using: getImage(url), getImage(url, name) For example, to load a image file “about.gif”: URL url = AboutPanel.class.getRessource(“about.gif”); ImageIcon = new ImageIcon(url); Means: “locate the about.gif at the same place where you find AboutPanel.class” Accessing Images at Home Server

COMP201 Topic 11 / Slide Accessing Resources at Home Server Inside a subclass of Applet getDocumentBase returns URL of the html file that calls the applet getCodeBase returns URL of the applet itself Example: Image cat = getImage(getCodeBase(), “image/cat.gif”); AudioClip meow = getAudioClip(getCodeBase(), “audio/meow.au”); or: play(getcodeBase(), “audio/meow.au”); The getImage and getAudioClip/play methods automatically search the JAR files of the applet first and load them immediately. Otherwise, the browser requests it from the web server.

COMP201 Topic 11 / Slide 39 l Applets can handle audio files in AU, AIFF, WAV, or MIDI format. Audio Clips (java.applet.Applet) Load: AudioClip getAudioClip(url), AudioClip getAudioClip(url, name) Then use play method of AudioClip to play and the stop method to stop Play without first loading: void play(url), void play(url, name) //SoundApplet.java Accessing Audio Files at Home Server

COMP201 Topic 11 / Slide 40 Accessing Text Files at Home Server Find the URL of text file and the create an InputStream using the openStream method of URL class InputStream in = url.openStream(); Or create an InputStream directly using the getResourceAsStream method of the Class class. InputStream in = x.class.getResoruceAsStream( fileName); The InputStream can be nested with other streams in the normal way (see Topic 4)

COMP201 Topic 11 / Slide 41 Accessing Text Files at Home Server For example: URL url = AboutPanel.class.getRessource(“about.txt”); InputStream stream = url.openStream(); or InputStream stream = AboutPanel.class.getRessourceAsStream(“about.txt”); Scanner in = Scanner.create(stream); While (in.hasNext()) textArea.append(in.nextLine() + “\n”); //resourceTest.java, resourceTest.html

COMP201 Topic 11 / Slide 42 Outline l An example l Creation n Converting applications to applets l Transportation n Jar files: Move applets from servers to browsers quickly l Operation n Applet life cycle n Security restrictions n Getting resources from home n Communicating with browser Applet Client BrowserServer Applet User

COMP201 Topic 11 / Slide 43 Communication with Browser To establish a communication channel between an applet and the ambient browser, use the getAppletContext method of the Applet class. The method returns an object of the AppletContext, which is an interface. An applet has access to two areas of the ambient browsers: The status line and the web page display area. Both use methods of the AppletContext To display a string in the status line: showStatus(“Loading data … Please wait” ) To show a different web page : showDocument(URL url ) showDocument(URL url, String target ) Ex: URL u = new URL(“ getAppletContext().showDocument(u); ShowPageApplet.java

COMP201 Topic 11 / Slide 44 Java Web Start l A technology for simplifying deployment of Java applications n Gives you the power to launch full-featured applications with a single click from your Web browser. n You can now download and launch applications, such as a complete spreadsheet program or an Internet chat client, without going through complicated installation procedures.