1 CSC 551: Web Programming Spring 2004 Combining Java & JavaScript  integrating Java with JavaScript  calling Java routines from JavaScript  controlling.

Slides:



Advertisements
Similar presentations
Java Script Session1 INTRODUCTION.
Advertisements

1 CSC 551: Web Programming Spring 2004 client-side programming with JavaScript  scripts vs. programs  JavaScript vs. JScript vs. VBScript  common tasks.
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.
Java Applet & JavaScript SNU OOPSLA Lab. October 2005.
1 Frameworks. 2 Framework Set of cooperating classes/interfaces –Structure essential mechanisms of a problem domain –Programmer can extend framework classes,
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.
1 A Balanced Introduction to Computer Science, 2/E David Reed, Creighton University ©2008 Pearson Prentice Hall ISBN Chapter 7 Event-Driven.
COMP 519: Web Programming Fall 2006 Acknowledgment: The slides are by Dr. David Reed.Dr. David Reed Java Overview  Design goals & features  platform.
Applets, Graphical User Interfaces, and Threads / Chapter 9 1 Applets, Graphical User Interfaces, and Threads.
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.
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.
Programming Task: Task 1 Controlled Assessment Practice.
1 CSC 551: Web Programming Spring 2004 Java Overview  Design goals & features  platform independence, portable, secure, simple, object-oriented, … 
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.
Java Overview  Design goals & features  platform independence, portable, secure, simple, object-oriented, …  Programming models  applications vs. applets.
Java Beans.
CS413: Java Programming language Applications Applets
20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 16: Java Applets & AWT Fundamentals of Web Programming.
CSC 551: Web Programming Fall 2001 Combining Java & JavaScript  integrating Java with JavaScript  calling Java routines from JavaScript  controlling.
Java Programming, 3e Concepts and Techniques Chapter 2 - Part 2 Creating a Java Application and Applet.
Slides © Marty Hall, book © Sun Microsystems Press 1 Including Files & Applets in JSP Documents Core Servlets & JSP book:
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
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.
COMP 519: Web Programming Autumn 2014 Combining Java & JavaScript  integrating Java with JavaScript  calling Java routines from JavaScript  controlling.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
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 34 Java Technology for Active Web Documents methods used to provide continuous Web updates to browser – Server push – Active documents.
CS 3131 Introduction to Programming in Java Rich Maclin Computer Science Department.
Active Server Pages  In this chapter, you will learn:  How browsers and servers interacted on the Internet when the Internet first became popular 
Computers and Scientific Thinking David Reed, Creighton University Functions and Libraries 1.
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.
Object Oriented Programming Lecture 5: Refactoring by Inheritance and Delegation - A simple Design Pattern for animation applets, A Generic Function Plotter.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Lec.10 + (Chapter 8 & 9) GUI Java Applet Jiang (Jen) ZHENG July 6 th, 2005.
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.
CSC 205 – Java Programming II Applet. Types of Java Programs Applets Applications Console applications Graphics applications Applications are stand-alone.
Program that runs in appletviewer (test utility for applets) Web browser (IE, Communicator) Executes when HTML (Hypertext Markup Language) document containing.
MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -1 Lecture 3 - Graphical User Interfaces r GUI toolkits in Java API r JFrame r GUI components.
LECTURE 18 Java and Web Pages. Java Savvy Browser A browser capable of handling java applets: - Netscape Navigator - Microsoft Internet Explorer - Sun.
CSE S. Tanimoto Java Introduction 1 Java A Programming Language for Web-based Computing with Graphics.
Applets Yong Choi School of Business CSU, Bakersfield.
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.
Jan 2001C.Watters1 World Wide Web and E-Commerce Client Side Processing.
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.
Java Overview  Design goals & features  platform independence, portable, secure, simple, object-oriented, …  Programming models  applications vs. applets.
Creating a Java Application and Applet
JavaScript 101 Introduction to Programming. Topics What is programming? The common elements found in most programming languages Introduction to JavaScript.
CHAPTER Agenda Applets Servelets Browsers HelloWorld.
COMP 519: Web Programming Autumn 2010 Combining Java & JavaScript  integrating Java with JavaScript  calling Java routines from JavaScript  controlling.
1 CSC160 Chapter 1: Introduction to JavaScript Chapter 2: Placing JavaScript in an HTML File.
Adding Interactivity Comp 140 Fall Web 2.0 Major change in internet usage –From mostly static pages Text Graphics Simple links –To new paradigm.
1 Applets Programming. Introduction Java programs are divided into two main categories, applets and applications. An application is an ordinary Java program.
Web Programming Java Script-Introduction. What is Javascript? JavaScript is a scripting language using for the Web. JavaScript is a programming language.
1 CSC 551: Web Programming Spring 2004 Java Applets  Java vs. C++  instance vs. class variables, primitive vs. reference types, inheritance  graphical.
1 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used as a type to declare an object reference.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Java Applet.
UNIT-5.
CSC 551: Web Programming Spring 2004
CSC 551: Web Programming Spring 2004
CSC 551: Web Programming Spring 2004
11.1 Applets & graphics.
JAVA APPLET PREPARED BY Mr. Jahanzaib Ahmed
APPLET PROGRAMMING.
Presentation transcript:

1 CSC 551: Web Programming Spring 2004 Combining Java & JavaScript  integrating Java with JavaScript  calling Java routines from JavaScript  controlling an applet from JavaScript  accessing JavaScript & HTML elements from an applet  related topics  Java archives (JARs), JavaBeans

2 recall: JavaScript is very good for simple tasks, GUI layout  flexible data typing, primitive object types fine for quick development  integration with HTML makes layout & control of GUI elements easy  not much library support, only primitive data structuring capabilities  not well-suited to multi-file projects, OO approach JavaScript vs. Java recall: Java is better at complex tasks, especially graphics  full-featured, more robust, extensive libraries of classes/routines  can support large projects, interacting objects  GUI layout is difficult, integration with HTML not obvious IDEAL: make use of the the strengths of each language  include applets in a page when needed (e.g., graphics)  allow communication between applet & JavaScript code

3 Calling Java routines from JavaScript Java+JavaScript Demo document.write(java.lang.Math.random()); Netscape Communicator allows direct calls to Java routines  specify full package name of routine, then call as in Java useful for more esoteric routines that are not supported directly in JavaScript  this feature is NOT supported by Internet Explorer view page in browser

4 Calling applet methods more commonly, want to include an applet in a page, control via HTML events & JavaScript consider MontePI example from last week  want to draw dots inside a square (with an inscribed circle)  could build GUI interface into applet, but required tricky layout manager  instead, leave graphics up to the applet, controlled via JavaScript to call a Java applet method from JavaScript document.appletName.methodCall(…)

5 MontePI revisited import java.awt.*; import java.applet.*; import java.util.Random; public class Monte6 extends Applet { private static Random randy; private int SIZE; private Image offScreenImage; private Graphics offScreenGraphics; private int randomInRange(int low, int high) {…} private double distance(int x1, int y1, int x2, int y2) {…} public void init() { randy = new Random(); Dimension dim = getSize(); SIZE = dim.width; drawCircle(); } public void drawCircle() { // DRAWS CIRCLE ON BOTH getGraphics() AND // offScreenGraphics } public void drawDots(int numPoints) { // DRAWS numPoints RANDOM DOTS ON BOTH getGraphics() // AND offScreenGraphics } public void paint(Graphics g) { g.drawImage(offScreenImage, 0, 0, null); } init creates the random number generator & gets applet size drawDots draws the dots on the screen and to the off-screen buffer paint redraws the screen using the buffer

6 MontePI example (cont.) Monte Carlo Darts Page <applet code="Monte6.class" name="MonteApplet" height=300 width=300> You must use a Java-enabled browser to view this applet. <input type="button" value="Generate points" onClick="document.MonteApplet.drawDots(1000);"> here, HTML button controls the applet (via JavaScript) view page in browser

7 Example (cont.) Monte Carlo Darts Page <applet code="Monte6.class" name="MonteApplet" height=300 width=300> You must use a Java-enabled browser to view this applet. points better interface: allow user to specify number of dots in text box each click adds new dots, have separate button to clear view page in browser

8 Dividing control where the control lies affects the efficiency/usability of an applet  want the applet to be as self-contained as possible, take advantage of speed advantage, more advanced features  but if GUI controls are in HTML, then JavaScript needs overall control consider adding counters for number of dots inside & outside circle  have the applet keep track of the dots in instance variables 1.call method to draw all dots, then JavaScript accesses counts & display  fast, but only see counts when done 2.could return more control to the page, applet draws one dot at a time  repetition is handled by JavaScript, can update boxes after each dot  slower, but more flexible (and can see counts change in Netscape) 3.could have applet update the HTML text boxes itself  tricky, ties the applet to the page

9 JavaScript in control import java.awt.*; import java.applet.*; import java.util.Random; public class Monte7 extends Applet {... public int numInside, numOutside; public void drawCircle() { numInside = 0; numOutside = 0;... } public void drawDots(int numDots) {... for (int i = 0; i < numPoints; i++) { int x = randomInRange(0, SIZE); int y = randomInRange(0, SIZE); if (distance(x, y, SIZE/2, SIZE/2) < SIZE/2) { offScreenGraphics.setColor(Color.white); g.setColor(Color.white); numInside++; } else { offScreenGraphics.setColor(Color.black); g.setColor(Color.black); numOutside++; }... }... } have applet keep track of number inside & out instance variables numInside and numOutside are initialized in drawCircle, updated in drawDots since public, these instance variables can be accessed in the page

10 Example (cont.) Monte Carlo Darts Page function drawAll() { var numDots = parseFloat(document.MonteForm.numPoints.value); document.MonteApplet.drawDots(numDots); document.MonteForm.numIn.value = document.MonteApplet.numInside; document.MonteForm.numOut.value = document.MonteApplet.numOutside; } function clearAll() { document.MonteApplet.drawCircle(); document.MonteForm.numIn.value = 0; document.MonteForm.numOut.value = 0; } You must use a Java-enabled browser to view this applet. points points inside points outside Note: can utilize HTML table to achieve desired layout of elements view page in browser

11 Example (cont.) Monte Carlo Darts Page function drawAll() { var numDots = parseFloat(document.MonteForm.numPoints.value); for (var i = 0; i < numDots; i++) { document.MonteApplet.drawDots(1); document.MonteForm.numIn.value = document.MonteApplet.numInside; document.MonteForm.numOut.value = document.MonteApplet.numOutside; } } function clearAll() { document.MonteApplet.drawCircle(); document.MonteForm.numIn.value = 0; document.MonteForm.numOut.value = 0; } You must use a Java-enabled browser to view this applet. points points inside points outside Alternatively: could draw each dot individually, display counts after each dot view page in browser

12 Accessing HTML/JavaScript from the applet it is possible for the applet to access elements in the page  requires the JSObject class from the netscape.javascript package import netscape.javascript.JSObject;  use getWindow and getMember methods to access components JSObject jsWin = JSObject.getWindow(this); // GETS WINDOW JSObject jsDoc = (JSObject) jsWin.getMember("document"); // GETS DOCUMENT JSObject MonteForm = (JSObject) jsDoc.getMember("MonteForm"); // GETS FORM numInside = (JSObject) MonteForm.getMember("numIn"); // GETS TEXT BOX  use getMember and setMember methods to access component attributes int num = Integer.parseInt( (String)numInside.getMember("value") ); numInside.setMember("value", ""+(num+1));

13 Java in control import java.awt.*; import java.applet.*; import java.util.Random; import netscape.javascript.JSObject; // need plugin.jar in classpath public class Monte8 extends Applet {... private JSObject numDots, numInside, numOutside; public void init() {... try { JSObject jsWin = JSObject.getWindow(this); JSObject jsDoc = (JSObject) jsWin.getMember("document"); JSObject MonteForm = (JSObject) jsDoc.getMember("MonteForm"); numDots = (JSObject) MonteForm.getMember("numDots"); numInside = (JSObject) MonteForm.getMember("numIn"); numOutside = (JSObject) MonteForm.getMember("numOut"); } catch (netscape.javascript.JSException jse) { } drawCircle(); } public void drawDot() {... int inCount = 0; outCount = 0; if (distance(x, y, SIZE/2, SIZE/2) < SIZE/2) {... inCount++; } else {... outCount++; } int newIn = Integer.parseInt((String)numInside.getMember("value"))+ inCount; numInside.setMember("value", ""+newIn); int newOut = Integer.parseInt((String)numOutside.getMember("value"))+ outCount; numOutside.setMember("value", ""+newOut); } public void drawCircle() { numInside.setMember("value", "0"); numOutside.setMember("value", "0");... }

14 Example (cont.) Monte Carlo Darts Page <applet code="Monte9.class" name="MonteApplet" height=300 width=300 mayscript> You must use a Java-enabled browser to view this applet. <input type="button" value="Generate" onClick="document.MonteApplet.drawDots();"> points points inside points outside <input type="button" value="Clear the screen" onClick="document.MonteApplet.drawCircle();"> view page in browser MAYSCRIPT attribute must be specified in the APPLET tag to allow access to HTML & JavaScript in the page

15 Non-graphical example import java.applet.Applet; import java.awt.*; import java.net.*; // This appears in Core Web Programming from // Prentice Hall Publishers, and may be freely used // or adapted Marty Hall, public class GetHost extends Applet { private String host; public void init() { setBackground(Color.white); try { host = InetAddress.getLocalHost().toString(); } catch(UnknownHostException uhe) { host = "Unknown Host"; } public String getHost() { return(host); } uses the Java InetAddress class to get the client's host name returns via getHost method

16 Example (cont.) WonderWidget function showResume() // Results: sets location of page based on user's host name { if ((document.gethost.getHost()).indexOf("widgets-r-us.com") != -1) { location = "ResumeLoyal.html"; } else { location = "ResumeReal.html"; } } WonderWidget Description: Name: Wonder Widget Serial Number: 1544X Cost: $7.95 (plus shipping and handling) Designer: J. Random Hacker applet provides access to getHost method here, the link is conditional based on the host name view page in browser

17 Related topics JavaBeans  reusable components (e.g., buttons, menus) that can be packaged and reused  requires special tools for compiling and packaging (e.g., BDK)  downloaded with an applet using the ARCHIVES attribute JAR files  for applets that are comprised of multiple classes, can bundle all necessary files into a Java Archive (JAR) file  uses the popular ZIP file format  download using ARCHIVES attribute, automatically unzipped by browser