Introduction to Computing Using Java

Slides:



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

Java Applets:. How Applets differ from application?: They do not use main method but init(), start() and paint() methods of the applet class They can.
Java 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.
Programming and Problem Solving With Java Copyright 1999, James M. Slack Applets What is an Applet? Applet Parameters Graphics in Applets Other Applet.
1 Frameworks. 2 Framework Set of cooperating classes/interfaces –Structure essential mechanisms of a problem domain –Programmer can extend framework classes,
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.
1 Introduction to Applets Overview l What is an Applet? l Steps for creating an applet l What is HTML? l Basic HTML tags l Drawing Simple Graphical shapes.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 16 Applets.
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.
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.
Applets, Graphical User Interfaces, and Threads / Chapter 9 1 Applets, Graphical User Interfaces, and Threads.
Java Applets. Road Map Introduction to Java Applets Review applets that ship with JDK Make our own simple applets –Introduce inheritance –Introduce the.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 14 Applets, Images,
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.
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 CS 3331 Sections 3.3 & 4.7 of [Jia03].
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.
Applets.
JAPPLET.
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.
1 Week 1 l HTML l Applets Applets and HTML. 2 Overview l Applets: Java programs designed to run from a document on the Internet l HTML: Hypertext Markup.
Chapter 13. Applets and HTML HTML Applets Computer Programming with JAVA.
CS 3131 Introduction to Programming in Java Rich Maclin Computer Science Department.
Chapter 13Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 13 l HTML l Applets Applets and HTML.
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,
Java Applets. 2 Introduction to Java Applet Programs Applications are ___________________ programs –executed with Java interpreter Applet is a small program.
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, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Applets and Multimedia.
CSE S. Tanimoto Java Introduction 1 Java A Programming Language for Web-based Computing with Graphics.
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 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.
Your name CSCI/CMPE 3326 Object-Oriented Programming in Java Dongchul Kim Department of Computer Science University of Texas – Pan American 1.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.
Applets, Images, and Audio Chapter 14 CSCI CSCI 1302 – Applets, Images, and Audio2 Outline Introduction The Applet Class –The init Method –The start.
Applets Java code is compiled into byte code instead of machine language –Languages like C, C++, Pascal and others are compiled into machine language so.
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.
CSI 3125, Preliminaries, page 1 Applet. CSI 3125, Preliminaries, page 2 Applet An applet is a Java program that runs in a Web browser. An applet can be.
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 Applets Programming. Introduction Java programs are divided into two main categories, applets and applications. An application is an ordinary Java program.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Applications Active Web Documents Active Web Documents.
30 Java Applets.
Distributed Computing, M. L. Liu
Distributed Computing, M. L. Liu
Applets.
UNIT-5.
Java Applets.
Introduction to Applet, Application and JDK
Java applets 1/3/2019.
Applet in Java.
Java Programming COMP-417 Applet
11.1 Applets & graphics.
Handout-14 Applets and Graphics
JAVA APPLET PREPARED BY Mr. Jahanzaib Ahmed
Enabling Application Delivery Via the Web
G6DICP - Lecture 27 Java Applets.
APPLET PROGRAMMING.
Presentation transcript:

Introduction to Computing Using Java Java Applet 2005-2010 12a Michael Fung, CS&E, The Chinese University of HK

Michael Fung, CS&E, The Chinese University of HK Overview Executing our Java program everywhere Java Applet (Application-let [small/mini]) Applet in Action Transformation Mix and Match with HTML 2005-2010 12a Michael Fung, CS&E, The Chinese University of HK

Michael Fung, CS&E, The Chinese University of HK What’s an Applet? A Java program. Operates within a browser e.g. I.E., Netscape, … Appears in a web page. Runs on the viewer’s computer. Loaded from the web. 2005-2010 12a Michael Fung, CS&E, The Chinese University of HK

JVM Everywhere Java Program [Welcome.java] Compile Java Compiler [javac] Compile Java Byte Code [Welcome.class] native code Translate Java Virtual Machine (JVM) [java] and BROWSERS 2005-2010 12a Michael Fung, CS&E, The Chinese University of HK

Applet Around the World Web site [http://mike.earth/] Applet (Java Byte Code) Lake.class Browser I.E. at my home PC [M-II-2GHz] Browser Netscape at Bill’s office PC [Fiii-5GHz] Internet Embedded JVM Embedded JVM 2005-2010 12a Michael Fung, CS&E, The Chinese University of HK

Olympic Rings Example Again import java.awt.*; // import Java packages class Rings extends Frame { public Rings ( ) { // constructor setTitle ("Olympic Rings"); // the window title bar } public void paint (Graphics g) { // a method for painting g.setColor(Color.red); g.drawOval( 80,80,30,30); // the rings g.setColor(Color.blue); g.drawOval(105,80,30,30); g.setColor(Color.green); g.drawOval(130,80,30,30); g.setColor(Color.yellow); g.drawOval(155,80,30,30); g.setColor(Color.black); g.drawOval(180,80,30,30); g.drawString("Olympic Rings", 110, 140); // the text public static void main (String [ ] args) { // main method Rings f = new Rings (); f.setSize (300, 200); // window dimensions f.setVisible (true); // show the window 2005-2010 12a Michael Fung, CS&E, The Chinese University of HK

Olympic Rings Applet Example Embedded in browser. There’s no window frame and title bar. No Close mechanism. Fit seamlessly in the web page. Started automatically! 2005-2010 12a Michael Fung, CS&E, The Chinese University of HK

Olympic Rings Example Again import java.awt.*; // import AWT package import java.applet.*; // import Applet package // inherits from Applet, a kind of container, c.f. Frame public class Rings extends Applet { // public class // constructor Rings() and main() replaced by init() public void paint (Graphics g) { // method defined in class Applet g.setColor(Color.red); g.drawOval( 80,80,30,30); // the rings g.setColor(Color.blue); g.drawOval(105,80,30,30); g.setColor(Color.green); g.drawOval(130,80,30,30); g.setColor(Color.yellow); g.drawOval(155,80,30,30); g.setColor(Color.black); g.drawOval(180,80,30,30); g.drawString("Olympic Rings", 110, 140); // the text } public void init() { // init(): instance method // setTitle() removed setSize(300, 200); setVisible(true); 2005-2010 12a Michael Fung, CS&E, The Chinese University of HK

Michael Fung, CS&E, The Chinese University of HK The Beds for Applets Applets are compiled using javac as usual. Applets cannot be run using java. Why? Applets should be run with a HTML file using the tool appletviewer in the JDK using any web browser supporting Java Applet (i.e. with JVM embedded). 2005-2010 12a Michael Fung, CS&E, The Chinese University of HK

Michael Fung, CS&E, The Chinese University of HK 7-Minute Break 2005-2010 12a Michael Fung, CS&E, The Chinese University of HK

Actions in Applet Internet Web site [http://mike.earth/Index.html] <HTML>… <APPLET CODE=Rings.class …>... </HTML> Browser I.E. at my home PC [M-II-2GHz] Internet User visits a web page. Inside the HTML file, there is a tag linking to an applet. Embedded JVM 2005-2010 12a Michael Fung, CS&E, The Chinese University of HK

Actions in Applet Internet Web site [http://mike.earth/Rings.class] (Java Byte Code) Rings.class Browser I.E. at my home PC [M-II-2GHz] Internet Assuming the user’s browser supports Java Applet, the JBC is downloaded. Embedded JVM 2005-2010 12a Michael Fung, CS&E, The Chinese University of HK

Actions in Applet On (re)loading the web page Browser I.E. at my home PC [M-II-2GHz] On (re)loading the web page the browser reserves a space; the JVM creates a new object from the class Rings; the JVM calls the instance method init() of the new object (instead of static main(), it’d be ignored if any) to initialize the applet. Embedded JVM Applet (Java Byte Code) Rings.class 2005-2010 12a Michael Fung, CS&E, The Chinese University of HK

Actions in Applet On scrolling the web page Browser I.E. at my home PC [M-II-2GHz] On scrolling the web page the applet may become invisible; the applet may appear again; the JVM calls the instance methods stop() and start() respectively in hope of halting/restarting the activities of the applet. Embedded JVM Applet (Java Byte Code) Rings.class 2005-2010 12a Michael Fung, CS&E, The Chinese University of HK

Actions in Applet On surfing to another page Browser I.E. at my home PC [M-II-2GHz] On surfing to another page the JVM calls the instance method destroy() of the object in order to perform some finalizations (clear house actions); kills the applet object; the browser reclaims the space; the browser moves on to another page. Embedded JVM Applet (Java Byte Code) Rings.class 2005-2010 12a Michael Fung, CS&E, The Chinese University of HK

7-Steps in Transformation There are some precautions and advice in transforming a Java Application into an Applet: Using the GUI for user interactions e.g. System.out.println(“Hello”); g.drawString(“Hello”, 0, 0); 2005-2010 12a Michael Fung, CS&E, The Chinese University of HK

7-Steps in Transformation Avoid program termination Remove all occurrences of, say, System.exit(); Catch all Exceptions to avoid abnormal termination. Let the browser do the job and make use of our destroy() to do what we need before termination. 2005-2010 12a Michael Fung, CS&E, The Chinese University of HK

7-Steps in Transformation Beware of layout within the applet An applet object acts part of the role of a Frame object. An applet is a Container. The default layout is FlowLayout. We may override it by calling setLayout(). We may add children to an applet. 2005-2010 12a Michael Fung, CS&E, The Chinese University of HK

7-Steps in Transformation Import the package java.applet.* and extends Applet instead of Frame in the main window. As a matter of taste, you may want the applet to appear thoroughly within the browser. Alternatively, you may call up another Frame object in order to pop up another window. In either case, you have to extend Applet in the main class/ window. 2005-2010 12a Michael Fung, CS&E, The Chinese University of HK

7-Steps in Transformation Replace (combine) the class’s constructor method and the static main() method by the instance method public void init() It initializes the applet object on kicking-off. In fact, we may still keep the main() method so that our program becomes dual executable. 2005-2010 12a Michael Fung, CS&E, The Chinese University of HK

7-Steps in Transformation Make sure our class is public Let it be accessible to other packages Simply add the key word public before the class declaration. e.g. public class Rings { ... } 2005-2010 12a Michael Fung, CS&E, The Chinese University of HK

7-Steps in Transformation Create a HTML file to refer to the applet e.g. Rings.html: <HTML> <APPLET CODE=Rings.class> </APPLET> </HTML> Keep the applet JBC with the HTML file. Feed the HTML file to any Applet-enabled viewers. 2005-2010 12a Michael Fung, CS&E, The Chinese University of HK

Michael Fung, CS&E, The Chinese University of HK 7-Minute Break 2005-2010 12a Michael Fung, CS&E, The Chinese University of HK

Michael Fung, CS&E, The Chinese University of HK Write it from Scratch There’s not much difference as compared with programming in Java GUI. Let’s see some examples: 7-Steps Transformation LazyConversion Java Image Viewer 2005-2010 12a Michael Fung, CS&E, The Chinese University of HK

Michael Fung, CS&E, The Chinese University of HK Wet our Feet with HTML An applet is a small application embedded in a web page. A HTML file is responsible for calling up and accommodating the applet. We may place both the HTML and the JBC files in the same directory in our local machines or in the Internet. For the latter case, mind the URL’s and the file access permissions. 2005-2010 12a Michael Fung, CS&E, The Chinese University of HK

Michael Fung, CS&E, The Chinese University of HK Sample HTML Rings.html: <HTML> <APPLET code=Rings.class width=300 height=200> </APPLET> </HTML> Tag <APPLET> indicates that an applet is to be inserted here. 2005-2010 12a Michael Fung, CS&E, The Chinese University of HK

Michael Fung, CS&E, The Chinese University of HK Sample HTML Rings.html: <HTML> <APPLET code=Rings.class width=300 height=200> </APPLET> </HTML> Tag <APPLET> accepts several parameters, for example, code, width, height, ... 2005-2010 12a Michael Fung, CS&E, The Chinese University of HK

Michael Fung, CS&E, The Chinese University of HK Sample HTML Rings.html: <HTML> <APPLET code=Rings.class width=300 height=200> </APPLET> </HTML> code tells where the applet is located and its class name. 2005-2010 12a Michael Fung, CS&E, The Chinese University of HK

Michael Fung, CS&E, The Chinese University of HK Sample HTML Rings.html: <HTML> <APPLET code=Rings.class width=300 height=50%> </APPLET> </HTML> width and height specifies how much space is to be reserved by the browser to accommodate the applet. 2005-2010 12a Michael Fung, CS&E, The Chinese University of HK

Michael Fung, CS&E, The Chinese University of HK Sample HTML Rings.html: <HTML> <APPLET code=Rings.class width=300 height=200> </APPLET> </HTML> Pity that this width and height do not correspond very well to the size of an applet. 2005-2010 12a Michael Fung, CS&E, The Chinese University of HK

Michael Fung, CS&E, The Chinese University of HK Do Them Together We may add the HTML source to the project and test our applet within the integrated environment. If we got several class files (JBC), sound files, image files, etc., we’d better JAR them into a single archive. 2005-2010 12a Michael Fung, CS&E, The Chinese University of HK

Michael Fung, CS&E, The Chinese University of HK What’s Next? Parameter Passing from HTML Limitations Security Issues Compare with Javascript Image and Sound with Applet 2005-2010 12a Michael Fung, CS&E, The Chinese University of HK

Michael Fung, CS&E, The Chinese University of HK End Note Readings and References Appendix G 2005-2010 12a Michael Fung, CS&E, The Chinese University of HK