Advanced Programming Rabie A. Ramadan Lecture 5.

Slides:



Advertisements
Similar presentations
Client Side Programming Using Java Applet Outcomes: You will be expected to know: – Java Applets and HTML file; –bytecode and platform independent programs;
Advertisements

1 Chapter 17 Applets and Multimedia. 2 Motivations F When browsing the Web – you frequently see the graphical user interface –animation developed using.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved What is Multimedia? Multimedia.
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.
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 Recitation 11. Applet Applets. An applet is a Java program that is started by a browser (e.g. netscape or internet explorer) when an html file has a.
Applet class The Applet class provides the essential framework that enables applets to be run by a web browser Applet do not have main method Applet depend.
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) 2007 Pearson Education, Inc. All rights reserved Chapter 16 Applets and Multimedia.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 16 Applets and Multimedia.
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.
Object Oriented Programming (OOP) LAB # 5 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L05 (Chapter 16) Applets.
Developing User Interfaces (DUI) Chris North cs3724: HCI.
Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet.
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.
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.
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 12 Applets and Advanced GUI  The Applet Class  The HTML Tag F Passing Parameters to Applets F Conversions Between Applications and Applets F.
Liang,Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 14 Applets, Images, and Audio 愿生如夏花之绚烂,死如秋叶之静美 ---- 泰戈尔.
Chapter 10: Applets and Advanced Graphics The Applet Class The Applet Class The HTML Tag The HTML Tag Passing Parameters to Applets Passing Parameters.
Applets. What is an applet? Why create applets instead of applications? – Applets are Java programs that can be embedded in an HTML document – In contrast,
1 CSC Computer Education (P) Ltd. DESIGNED BY K PRAKASH,
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.
1 Chapter 14 Applets, Images, and Audio. 2 Objectives F To explain how the Web browser controls and executes applets (§ 14.2). F To describe the init,
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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 16 Applets and Multimedia.
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.
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.
Applets, Images, and Audio Chapter 14 CSCI CSCI 1302 – Applets, Images, and Audio2 Outline Introduction The Applet Class –The init Method –The start.
Copyright © Curt Hill Applets A different type of program.
Creating a Java Application and Applet
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.
1 Java Applet Basics Chapter Eight. 2 Applets vs. Applications l Applications: Stand alone Java programs run by interpreter l Applets WWW browser embedded.
Jaeki Song ISQS6337 JAVA Lecture 10 Applets. Jaeki Song ISQS6337 JAVA Applet Applets run within the Web browser environment –Applets bring dynamic interaction.
1 Applets Programming. Introduction Java programs are divided into two main categories, applets and applications. An application is an ordinary Java program.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 14 Applets, Images,
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Chapter 17 Applets and Multimedia
Applets.
Chapter 12 Applets and Advanced Graphics
30 Java Applets.
Distributed Computing, M. L. Liu
Distributed Computing, M. L. Liu
UNIT-5.
Introduction to Computing Using Java
Java applets 1/3/2019.
Java Programming COMP-417 Applet
JAVA APPLET PREPARED BY Mr. Jahanzaib Ahmed
G6DICP - Lecture 27 Java Applets.
APPLET PROGRAMMING.
Presentation transcript:

Advanced Programming Rabie A. Ramadan Lecture 5

Item 13: Use objects to manage resources. 2

Q1: what might goes wrong with the following code: 3 class Investment { … }; Investment *createInvestment(); // factory function, caller must delete pointer // when finished using it void f() { Investment *pInv = createInvestment(); … delete pInv; } Looks OK, but what if premature return? OR, exception thrown? Even if correct at first, what happens as code is maintained?

Q2: how can I solve this problem ? 4 class Investment { … }; Investment *createInvestment(); // factory function, caller must delete pointer // when finished using it void f() { Investment *pInv = createInvestment(); … delete pInv; }

A solution.. Put the resource returned by createInvestment inside an object whose destructor will release the resource when control leaves f. std::auto_ptr is a smart pointer whose destructor automatically calls delete on what it points to void f() { std::auto_ptr pInv(createInvestment()); … // use pInv as before } call to factory function will delete pInv via auto_ptr’s destructor

Q3: what is the problem with auto pointers ? 6 There should never be more than one auto_ptr pointing to an object, or the object will be deleted more than once. Copying an auto_ptr sets it to NULL, so copying pointer has sole ownership. std::auto_ptr pInv1(createInvestment()); std::auto_ptr pInv2(pInv1); // pInv1 now NULL pInv1 = pInv2; // pInv2 now NULL

Q4: what are the other alternatives ? 7 Sometimes pointers must have “normal” copying behavior (e.g., in container classes) Use reference-counting smart pointer (RCSP). Keeps track of how many objects point to resource, deletes it when nobody points to it. Similar to garbage collection, but can’t break cycles. void f() { std::tr1::shared_ptr pInv1(createInvestment()); // count=1 std::tr1::shared_ptr pInv2(pInv1); // both now point to same object, count = 2 pInv1 = pInv2; // still point to same object, count = 2 } pointers destroyed at end, object count will be 0 so it will be destroyed.

Q5: why it is not recommended to use these pointers with arrays? 8 auto_ptr and tr1::shared_ptr use delete, not delete [] in their destructors Bad idea: std::auto_ptr aps(new std::string[10]); std::tr1::shared_ptr spi(new int[1024]); Boost library has classes for dealing with arrays. You can also create your own resource-managing class.

Item 14: Think carefully about copying behavior in resource-managing classes. 9

Q6: what is the main idea behind item 14? 10 Copying behavior in resource-managing classes Sometimes you need to use your own recourse manger class for pointers. Not all pointers kept in heap

Q7: what is the problem you should handle if you use your own resource manger class ? 11 Copying such pointer

Q8: what are the possible solution ? 12 Prohibit copying. Might not make sense to have a copy – why would you have a copy of a Lock? Reference-count the underlying resource. Hold the resource until the last object using it has been destroyed. tr1::shared_ptr does this. BUT, shared_ptr deletes resource when reference count is 0, may not be what you want. Can specify “deleter” as second parameter to shared_ptr constructor.

Q8 13 Copy the underlying resource. Only purpose of resource-managing class is to ensure resource gets deleted. Makes a deep copy. Transfer ownership of the underlying resource. Used by auto_ptr in Item #13.

Chapter 10 Applets 14

Q9: What is the main structure of an Applet’s class ? 15

16 public class MyApplet extends java.applet.Applet {... /** The no-arg constructor is called by the browser when the Web page containing this applet is initially loaded, or reloaded */ public MyApplet() {... } /** Called by the browser after the applet is loaded */ public void init() {... } /** Called by the browser after the init() method, or every time the Web page is visited */ public void start() {... } /** Called by the browser when the page containing this applet becomes inactive */ public void stop() {... } /** Called by the browser when the Web browser exits */ public void destroy() {... } /** Other methods if necessary... */ }

The Applet Class 17 When the applet is loaded, the Web browser creates an instance of the applet by invoking the applet’s no-arg constructor. The browser uses the init, start, stop, and destroy methods to control the applet. By default, these methods do nothing. To perform specific functions, they need to be modified in the user's applet so that the browser can call your code properly.

Q10: Draw a flowchart showing how the browser deals with applets ? 18

Q11: When the init() Method is called ? 19 Invoked when the applet is first loaded and again if the applet is reloaded. A subclass of Applet should override this method if the subclass has an initialization to perform. The functions usually implemented in this method include creating new threads, loading images, setting up user- interface components, and getting string parameter values from the tag in the HTML page.

Q12: When the start() Method is called ? 20 Invoked after the init() method is executed; also called whenever the applet becomes active again after a period of inactivity (for example, when the user returns to the page containing the applet after surfing other Web pages). A subclass of Applet overrides this method if it has any operation that needs to be performed whenever the Web page containing the applet is visited. An applet with animation, for example, might use the start method to resume animation.

Q13: When the stop() Method is called ? 21 The opposite of the start() method, which is called when the user moves back to the page containing the applet; the stop() method is invoked when the user moves off the page. A subclass of Applet overrides this method if it has any operation that needs to be performed each time the Web page containing the applet is no longer visible. When the user leaves the page, any threads the applet has started but not completed will continue to run. You should override the stop method to suspend the running threads so that the applet does not take up system resources when it is inactive.

Q14: When the destroy() Method is called ? 22 Invoked when the browser exits normally to inform the applet that it is no longer needed and that it should release any resources it has allocated. A subclass of Applet overrides this method if it has any operation that needs to be performed before it is destroyed. Usually, you won't need to override this method unless you wish to release specific resources, such as threads that the applet created.

Q15: The Applet class is an AWT class and is not designed to work with Swing components. To use Swing components in Java applets, it is necessary to create a Java applet that extends …………, which is a subclass of …………. 23

Q15: 24 The Applet class is an AWT class and is not designed to work with Swing components. To use Swing components in Java applets, it is necessary to create a Java applet that extends javax.swing.JApplet, which is a subclass of java.applet.Applet. JApplet inherits all the methods from the Applet class. In addition, it provides support for laying out Swing components.

Q16: Is there anything wrong with these two applets ? 25

Q16 26 Run Applet Viewer

Summary 27 Always extends the JApplet class, which is a subclass of Applet for Swing components. Override init(), start(), stop(), and destroy() if necessary. By default, these methods are empty. Add your own methods and data if necessary. Applets are always embedded in an HTML page.

Q17: what is the purpose of this html file? 28 <applet code = "DisplayMessage.class" width = 200 height = 50> alt="You must have a Java-enabled browser to view the applet"

The HTML Tag 29 <applet code=classfilename.class width=applet_viewing_width_in_pixels height=applet_viewing_height_in_pixels [archive= location of archivefile] [codebase=applet_url] [vspace=vertical_margin] [hspace=horizontal_margin] [align=applet_alignment] [alt=alternative_text] >

Q18: What are the similarities between Applets and Applications ? 30 Similarities Since JFrame and JApplet both are subclasses of the Container class, all the user interface components, layout managers and event-handling features are the same for both classes.

Q19: What are the differences between Applets and Applications ? 31 Differences Applications are invoked from the static main method by the Java interpreter, and applets are run by the Web browser. The Web browser creates an instance of the applet using the applet’s no-arg constructor and controls and executes the applet through the init, start, stop, and destroy methods. Applets have security restrictions Web browser creates graphical environment for applets, GUI applications are placed in a frame.

Q20: What are the Security Restrictions on Applets? 32 Applets are not allowed to read from, or write to, the file system of the computer viewing the applets. Applets are not allowed to run any programs on the browser’s computer. Applets are not allowed to establish connections between the user’s computer and another computer except with the server where the applets are stored.

Q21: 33 Due to security restrictions, applets cannot access local files. How can an applet load resource files for image and audio? Use URL class instead of absolute location

Q22: What is wrong with this code ? 34 ImageIcon imageIcon = new ImageIcon("c:\\book\\image\\us.gif"); jlbl.setIcon(imageIcon); This approach suffers a problem. The file location is fixed since it uses the absolute file path on Window. Thus, the program cannot run on other platforms and cannot run as applet. Example : Class metaObject = this.getClass(); URL url = metaObject.getResource("image/us.gif"); You can now create an ImageIcon using ImageIcon imageIcon = new ImageIcon(url);

Q23: How to run an audio clip in an applet ? 35 Class class = this.getClass(); URL url = class.getResource("beep.au"); AudioClip audioClip = Applet.newAudioClip(url);

Q24: What is JAR? 36 Java archive file can be used to group all the project files in a compressed file for deployment. The Java archive file format (JAR) is based on the popular ZIP file format. Pack200 tool

Q25: Why should I use JAR ? 37 This single file can be deployed on an end-user’s machine as an application. It also can be downloaded to a browser in a single HTTP transaction, rather than opening a new connection for each piece. This greatly simplifies application deployment and improves the speed with which an applet can be loaded onto a web page and begin functioning.

Creating JAR 38 jar -cf TicTacToe.jar TicTacToe.class TicTacToe$Cell.class The -c option is for creating a new archive file, The -f option specifies the archive file’s name.

Q26 : What is the function of Manifest File 39 The manifest is a special file that contains information about the files packaged in a JAR file. For instance, the manifest file in TicTacToe.jar contains the following information: Manifest-Version: 1.0 Name: TicTacToe.class Java-Bean: True Name: TioTacToe$Cell.class Java-Bean: True

Jar and Applete 40 To run TicTacToe as an applet, modify the tag in the HTML file to include an ARCHIVE attribute. The ARCHIVE attribute specifies the archive file in which the applet is contained. For example, the HTML file for running TicTacToe can be modified as shown below: <APPLET CODE = "TicTacToe.class" ARCHIVE = "TicTacToe.jar" WIDTH = 400 HEIGHT = 300 HSPACE = 0 VSPACE = 0 ALIGN = Middle >

Q27: Are we satisfying the following items : 41 Acquire the basic knowledge and essential concepts of Java programming including data types, loops, arrays, classes, inner and outer classes, the concept of Servlets, and Applets programming. Acquire advanced knowledge in C++ programming through learning different advanced skills.