Programming for WWW (ICE 1338) Lecture #7 Lecture #7 July 14, 2004 In-Young Ko iko.AT. icu.ac.kr Information and Communications University (ICU) iko.AT.

Slides:



Advertisements
Similar presentations
1 Applets Programming Enabling Application Delivery Via the Web.
Advertisements

2D Graphics Drawing Things. Graphics In your GUI, you might want to draw graphics E.g. draw lines, circles, shapes, draw strings etc The Graphics class.
LAB SESSION 7 Graphical user interface Applet fundamentals Methods in applets Execution of an applet Graphics class.
Graphic User Interfaces Layout Managers Event Handling.
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,
Graphics Programming With Applets Feb 23, Applets There are three different types of executable java code. –Standalone application, which has main()
COS 381 Day 10. Agenda Assignment 2 Posted Program a web-based Version of Soduku using JavaScript and DOM to learn how to play.
Event Handling Events and Listeners Timers and Animation.
Lecture 24 Applets. Introduction to Applets Applets should NOT have main method but rather init, stop, paint etc They should be run through javac compiler.
COMP 14 Introduction to Programming Miguel A. Otaduy June 8, 2004.
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.
Chapter 14 Applets. 2 Knowledge Goals Understand the differing roles of applications and applets Understand how a browser operates Understand the role.
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.
Active X Microsoft’s Answer to Dynamic Content Reference: Using Active X by Brian Farrar QUE
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.
Lecture 241 CS110 Lecture 25 Tuesday, May 4, 2004 Announcements –final exam Thursday, May 20, 8:00 AM McCormack, Floor 01, Room 0608 (easier than last.
Applets  The Applet Class  The HTML Tag F Passing Parameters to Applets.
Applets Java API.
20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 16: Java Applets & AWT Fundamentals of Web Programming.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
Chapter 7 © 2003 by Addison-Wesley, Inc Introduction - Applets are relatively small Java programs whose execution is triggered by a browser - The.
Java Programming: From Problem Analysis to Program Design, Second Edition1  Learn about basic GUI components.  Explore how the GUI components JFrame,
Applets and Frames CS 21a: Introduction to Computing I First Semester,
MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up.
GUI programming Graphical user interface-based programming.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
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.
GUIs in Java Swing, Events CS2110, SW Development Methods Readings: MSD, Chapter 12 Lab Exercise.
Chapter 9 © 2001 by Addison Wesley Longman, Inc. 1 Chapter 9 Sebesta: Programming the World Wide Web.
Chapter 7 © 2005 by Addison Wesley Longman, Inc Introduction - Applets are relatively small Java programs whose execution is triggered by a browser.
– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Java Applets 1. What is an applet? An applet is a small Java program that is typically embedded in a Web page and can be run using the applet viewer or.
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.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
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.
1 MSCS 237 Overview of web technologies (A specific type of distributed systems)
Swing Differences between Swing and AWT Naming Conventions All Swing components begin with a capital J -- JPanel, JButton, JScrollBar, JApplet, etc..
Concurrent Programming and Threads Threads Blocking a User Interface.
Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010.
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.
Java Programming Applets. Topics Write an HTML document to host an applet Understand simple applets Use Labels with simple AWT applets Write a simple.
Java Applet. Introductions Applet is java program that can be embedded into HTML pages Java applets runs on the java enables web browsers such as mozila.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
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 GUI programming Graphical user interface-based programming Chapter G1 (pages )
Chapter 8 (Horstmann’s Book) Frameworks Hwajung Lee.
Chapter 8 Frameworks. Frameworks Framework is a set of cooperating classes and interface types that structures the essential mechanisms of a particular.
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.
CHAPTER Agenda Applets Servelets Browsers HelloWorld.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Applets. 9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L12: Applets Slide 2 Applets Usually.
1 Applets Programming. Introduction Java programs are divided into two main categories, applets and applications. An application is an ordinary Java program.
Applet: An applet is a java program that is transmitted over the network from the server to client & executed within clients browser. Applets are used.
A First Look at GUI Applications
Lecture 09 Applets.
Ellen Walker Hiram College
Distributed Computing, M. L. Liu
Distributed Computing, M. L. Liu
Applets.
UNIT-5.
Constructors, GUI’s(Using Swing) and ActionListner
JAVA APPLET PREPARED BY Mr. Jahanzaib Ahmed
Enabling Application Delivery Via the Web
Presentation transcript:

Programming for WWW (ICE 1338) Lecture #7 Lecture #7 July 14, 2004 In-Young Ko iko.AT. icu.ac.kr Information and Communications University (ICU) iko.AT. icu.ac.kr

July 14, Programming for WWW (Lecture#7) In-Young Ko, Information Communications University Announcements Midterm Exam: Midterm Exam: 10:00AM – 11:30AM, Friday July 16 th 10:00AM – 11:30AM, Friday July 16 th Your grades of homework#1 are posted on the class Web page Your grades of homework#1 are posted on the class Web page

July 14, Programming for WWW (Lecture#7) In-Young Ko, Information Communications University Review of the Previous Lecture Use of JavaScript Use of JavaScript Document Object Model HTML Document Object Model HTML Data types Data types Operators Operators Pattern matching Pattern matching Event handling Event handling

July 14, Programming for WWW (Lecture#7) In-Young Ko, Information Communications University Contents of Today’s Lecture Java Applets Java Applets Applet GUI structure Applet GUI structure Event handling in Applet GUIs Event handling in Applet GUIs Concurrency in Applets Concurrency in Applets Plug-in programs Plug-in programs

July 14, Programming for WWW (Lecture#7) In-Young Ko, Information Communications University Java Applets Applets are relatively small Java programs whose execution is triggered by a browser Applets are relatively small Java programs whose execution is triggered by a browser The purpose of an applet is to provide processing capability and interactivity for HTML documents through widgets The purpose of an applet is to provide processing capability and interactivity for HTML documents through widgets The ‘standard’ operations of applets are provided by the parent class, JApplet The ‘standard’ operations of applets are provided by the parent class, JApplet public class class_name extends JApplet { … } Use of applets is still widespread, and there is heavy use in intranets Use of applets is still widespread, and there is heavy use in intranets Applets are an alternative to CGI and embedded client-side scripts Applets are an alternative to CGI and embedded client-side scripts AW lecture notes

July 14, Programming for WWW (Lecture#7) In-Young Ko, Information Communications University Applets vs. JavaScript (CGI) CGI is faster than applets and JavaScript, but it is run on the server CGI is faster than applets and JavaScript, but it is run on the server JavaScript is easier to learn and use than Java, but less expressive JavaScript is easier to learn and use than Java, but less expressive Java is faster than JavaScript Java is faster than JavaScript Java graphics are powerful, but JavaScript has none Java graphics are powerful, but JavaScript has none JavaScript does not require the additional download from the server that is required for applets JavaScript does not require the additional download from the server that is required for applets Java may become more of a server-side tool, in the form of servlets, than a client-side tool Java may become more of a server-side tool, in the form of servlets, than a client-side tool AW lecture notes

July 14, Programming for WWW (Lecture#7) In-Young Ko, Information Communications University Browser Actions for Running Applets 1.Download and instantiate the applet class 2.Call the applet’s init method 3.Call the applet’s start method – This starts the execution of the applet 4.When the user takes a link from the document that has the applet, the browser calls the applet’s stop method 5.When the browser is stopped by the user, the browser calls the applet’s destroy method AW lecture notes

July 14, Programming for WWW (Lecture#7) In-Young Ko, Information Communications University An Example A Scrolling Banner public class AppletTest extends JApplet { private String msg; private String msg; private boolean needToStop = false; private boolean needToStop = false; public void init() { public void init() { msg = getParameter("message"); msg = getParameter("message"); setFont(new Font("Arial", Font.BOLD, 24)); setFont(new Font("Arial", Font.BOLD, 24)); } public void start() { public void start() { repaint(); repaint(); } public void paint(Graphics g) { public void paint(Graphics g) { g.setColor(Color.blue); g.setColor(Color.blue); int x = getWidth(), y = 20; int x = getWidth(), y = 20; while (!needToStop && x > 20) { while (!needToStop && x > 20) { try { Thread.sleep(10); } catch(Exception e) { } try { Thread.sleep(10); } catch(Exception e) { } g.clearRect(0, 0, getWidth(), getHeight()); g.clearRect(0, 0, getWidth(), getHeight()); g.drawString(msg, x--, y); g.drawString(msg, x--, y); } } public void stop() { public void stop() { needToStop = true; needToStop = true; }} <applet code="AppletTest.class“ width=600 height=50> <param name=“message” value="Information and Communications University"> HTML Document Applet Code

July 14, Programming for WWW (Lecture#7) In-Young Ko, Information Communications University Applet Parameters Applets can be sent parameters through HTML, using the tag and its two attributes, name and value Applets can be sent parameters through HTML, using the tag and its two attributes, name and value Parameter values are strings - e.g., Parameter values are strings - e.g., The applet gets the parameter values with getParameter, which takes the name of the parameter The applet gets the parameter values with getParameter, which takes the name of the parameter e.g., String myFruit = getParameter("fruit"); e.g., String myFruit = getParameter("fruit"); If no parameter with the given name has been specified in the HTML document, getParameter returns null If no parameter with the given name has been specified in the HTML document, getParameter returns null If the parameter value is not really a string, the value returned from getParameter must be converted like: If the parameter value is not really a string, the value returned from getParameter must be converted like: String pString = getParameter("size"); if (pString == null) mySize = 24; else mySize = Integer.parseInt(pString); The best place to put the code to get parameters is in init The best place to put the code to get parameters is in init AW lecture notes

July 14, Programming for WWW (Lecture#7) In-Young Ko, Information Communications University The JApplet Class An applet is a panel that can be embedded in a Web browser window An applet is a panel that can be embedded in a Web browser window An applet panel can contain other GUI components (e.g., buttons, menus, …), and customized drawings (using the ‘paint’ method) An applet panel can contain other GUI components (e.g., buttons, menus, …), and customized drawings (using the ‘paint’ method) External frames can be created from an applet External frames can be created from an applet

July 14, Programming for WWW (Lecture#7) In-Young Ko, Information Communications University Paint Method in Applet Always called by the browser (not the applet itself) when it displays/refreshes its window Always called by the browser (not the applet itself) when it displays/refreshes its window Takes one parameter, an object of class Graphics, which is defined in java.awt Takes one parameter, an object of class Graphics, which is defined in java.awt The protocol of the paint method is: The protocol of the paint method is: public void paint(Graphics grafObj) { … } The Graphics object is created by the browser The Graphics object is created by the browser Methods in Graphics: drawImage, drawLine, drawOval, drawPolygon, drawRect, drawString, fillOval, fillPolygon, fillRect, … Methods in Graphics: drawImage, drawLine, drawOval, drawPolygon, drawRect, drawString, fillOval, fillPolygon, fillRect, … AW lecture notes

July 14, Programming for WWW (Lecture#7) In-Young Ko, Information Communications University Applet GUI Structure

July 14, Programming for WWW (Lecture#7) In-Young Ko, Information Communications University Java GUI Component Layers Layered Pane

July 14, Programming for WWW (Lecture#7) In-Young Ko, Information Communications University Java GUI Program Example JLabel queryLabel = new JLabel("Query: "); JTextField queryField = new JTextField(20); JButton searchButton = new JButton("Search"); searchButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // search action // search action} }); }); JPanel mainPanel = new JPanel(); mainPanel.add(queryLabel); mainPanel.add(queryField); mainPanel.add(searchButton); JFrame mainFrame = new JFrame("Search Input"); mainFrame.getContentPane().add(mainPanel); mainFrame.pack(); mainFrame.show();

July 14, Programming for WWW (Lecture#7) In-Young Ko, Information Communications University Event Handling in Java An event is created by an external action such as a user interaction through a GUI An event is created by an external action such as a user interaction through a GUI The event handler (event listener) is a segment of code that is called in response to an event The event handler (event listener) is a segment of code that is called in response to an event JButton helloButton = new JButton(“Hello”); JButton helloButton = new JButton(“Hello”); helloButton.addActionListener(new AbstractAction() { helloButton.addActionListener(new AbstractAction() { public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) { System.out.println(“Hello World!”); System.out.println(“Hello World!”); } } A JButton Event Listeners Button Pressed Event

July 14, Programming for WWW (Lecture#7) In-Young Ko, Information Communications University Event Listener Registration Connection of an event to a listener is established through event listener registration Connection of an event to a listener is established through event listener registration Done with a method of the class that implements the listener interface (e.g., ActionListener) Done with a method of the class that implements the listener interface (e.g., ActionListener) The panel object that holds the components can be the event listener for those components The panel object that holds the components can be the event listener for those components Event generators send messages (call methods, e.g., actionPerformed) to registered event listeners when events occur Event generators send messages (call methods, e.g., actionPerformed) to registered event listeners when events occur Event handling methods must conform to a standard protocol, which comes from an interface Event handling methods must conform to a standard protocol, which comes from an interface

July 14, Programming for WWW (Lecture#7) In-Young Ko, Information Communications University Event Classes and Handler Methods Semantic Event Classes Semantic Event Classes ActionEvent - click a button, select from a menu or list, or type the enter button in a text field ActionEvent - click a button, select from a menu or list, or type the enter button in a text field ItemEvent - select a checkbox or list item ItemEvent - select a checkbox or list item TextEvent - change the contents of a text field or text area TextEvent - change the contents of a text field or text area For the two most commonly used events, ActionEvent and ItemEvent, there are the following interfaces and handler methods: For the two most commonly used events, ActionEvent and ItemEvent, there are the following interfaces and handler methods: InterfaceHandler method ActionListeneractionPerformed ItemListeneritemStateChanged The methods to register the listener is the interface name with “add” prepended The methods to register the listener is the interface name with “add” prepended e.g., button1.addActionListener(this);

July 14, Programming for WWW (Lecture#7) In-Young Ko, Information Communications University Concurrency in Java “A program is said to be concurrent if it contains more than one active execution context” [M. Scott] void concurrentPrint() { (new Thread () { (new Thread () { public void run() { public void run() { while (true) { while (true) { try { System.out.print("A"); sleep(0,1); try { System.out.print("A"); sleep(0,1); } catch (Exception e) { } } catch (Exception e) { } } }).start(); }).start(); (new Thread () { (new Thread () { public void run() { public void run() { while (true) { while (true) { try { System.out.print("B"); sleep(0,1); try { System.out.print("B"); sleep(0,1); } catch (Exception e) { } } catch (Exception e) { } } }).start(); }).start();} Java Forking two concurrent execution threads Main Program Control Printing “A” Printing “B”

July 14, Programming for WWW (Lecture#7) In-Young Ko, Information Communications University Java Threads A thread of control is a sequence of program points reached as execution flows through the program A thread of control is a sequence of program points reached as execution flows through the program Java threads are lightweight tasks – all threads run in the same address space Java threads are lightweight tasks – all threads run in the same address space c.f., Ada tasks are heavyweight threads (processes) that run in their own address spaces c.f., Ada tasks are heavyweight threads (processes) that run in their own address spaces The concurrent program units in Java are methods named run, whose code can be in concurrent execution with other run methods and with main The concurrent program units in Java are methods named run, whose code can be in concurrent execution with other run methods and with main There are two ways to implement threads, as a subclass of Thread and by implementing the interface Runnable There are two ways to implement threads, as a subclass of Thread and by implementing the interface Runnable Two essential methods: Two essential methods: run is the concurrent method run is the concurrent method start tells the run method to begin execution start tells the run method to begin execution

July 14, Programming for WWW (Lecture#7) In-Young Ko, Information Communications University Thread Methods class MyThread extends Thread { public void run() { public void run() { … // Task body // Task body … }}… Thread aTask = new MyThread(); aTask.start(); … aTask.setPriority (Thread.MAX_PRIORITY); … aTask.yield(); … aTask.sleep(2000); … aTask.join(); … Concurrent method definition Concurrent method definition Tells the run method to begin execution Tells the run method to begin execution Sets the scheduling priority Sets the scheduling priority Gives up the processor to other threads Gives up the processor to other threads Blocks the thread for some milliseconds Blocks the thread for some milliseconds Waits for the thread to complete Waits for the thread to complete

July 14, Programming for WWW (Lecture#7) In-Young Ko, Information Communications University An Applet with A Thread public class AppletTestThread extends JApplet implements Runnable { private String msg; private String msg; private boolean needToStop = false; private boolean needToStop = false; private int x, y; private int x, y; public void init() { public void init() { msg = getParameter("message"); msg = getParameter("message"); setFont(new Font("Arial", Font.BOLD, 24)); setFont(new Font("Arial", Font.BOLD, 24)); x = getWidth(); y = 20; x = getWidth(); y = 20; } public void start() { public void start() { Thread appletThread = new Thread(this); Thread appletThread = new Thread(this); appletThread.start(); appletThread.start(); } public void run() { public void run() { while (!needToStop && x-- > 20) { while (!needToStop && x-- > 20) { try { Thread.sleep(10); } catch(Exception e) { } try { Thread.sleep(10); } catch(Exception e) { } repaint(); repaint(); } } public void paint(Graphics g) { public void paint(Graphics g) { g.setColor(Color.blue); g.setColor(Color.blue); g.clearRect(0, 0, getWidth(), getHeight()); g.clearRect(0, 0, getWidth(), getHeight()); g.drawString(msg, x, y); g.drawString(msg, x, y); } … } …}

July 14, Programming for WWW (Lecture#7) In-Young Ko, Information Communications University Applet & Swing References How to make Applets: html How to make Applets: html html html The Swing Tutorial: The Swing Tutorial: Painting in AWT and Swing: Painting in AWT and Swing: Understanding Containers: Understanding Containers:

July 14, Programming for WWW (Lecture#7) In-Young Ko, Information Communications University Plug-ins Code modules that are inserted into the browser Code modules that are inserted into the browser Adds new capabilities to the Web browser Adds new capabilities to the Web browser e.g., e.g.,

July 14, Programming for WWW (Lecture#7) In-Young Ko, Information Communications University Java Plug-in Extends the functionality of a web browser, allowing applets to be run under Sun's Java 2 runtime environment (JRE) rather than the Java runtime environment that comes with the web browser Extends the functionality of a web browser, allowing applets to be run under Sun's Java 2 runtime environment (JRE) rather than the Java runtime environment that comes with the web browser Java Plug-in is part of Sun's JRE and is installed with it when the JRE is installed on a computer or can be automatically downloaded Java Plug-in is part of Sun's JRE and is installed with it when the JRE is installed on a computer or can be automatically downloaded Works with both Netscape and Internet Explorer Works with both Netscape and Internet Explorer Makes it more suitable for widespread use on consumer client machines that typically are not as powerful as client platforms Makes it more suitable for widespread use on consumer client machines that typically are not as powerful as client platforms

July 14, Programming for WWW (Lecture#7) In-Young Ko, Information Communications University Java Plug-in Tags Internet Explorer <OBJECT classid="clsid:CAFEEFAC ABCDEFFEDCBA" width=“600" height=“50" width=“600" height=“50" codebase=" codebase=" jinstall-1_4_2-windows-i586.cab"> jinstall-1_4_2-windows-i586.cab"> </OBJECT>Netscape <EMBED type="application/x-java-applet;jpi-version=1.4.2“ width="200" height="200“ pluginspage=" codebase=" “ code=“AppletTest.class“ message=“Information and Communications University“> No Java 2 SDK, Standard Edition v support for APPLET!! No Java 2 SDK, Standard Edition v support for APPLET!! </EMBED> Static Versioning

July 14, Programming for WWW (Lecture#7) In-Young Ko, Information Communications University Autodownload Files /guide/deployment/deployment- guide/autodl-files.html

July 14, Programming for WWW (Lecture#7) In-Young Ko, Information Communications University Java Plug-in Tags (cont.) Combined Form <OBJECT classid="clsid:8AD9C E-11D1-B3E F499D93" width=“600" height=“50" width=“600" height=“50" codebase=" codebase=" jinstall-1_4_2-windows-i586.cab#Version=1,4,2,0"> jinstall-1_4_2-windows-i586.cab#Version=1,4,2,0"> <EMBED type="application/x-java-applet;jpi-version=1.4.2“ width="200" height="200“ <EMBED type="application/x-java-applet;jpi-version=1.4.2“ width="200" height="200“ pluginspage= pluginspage= code=“AppletTest.class“ code=“AppletTest.class“ codebase=" “ codebase=" “ message=“Information and Communications University"> message=“Information and Communications University"> No Java 2 SDK, Standard Edition v support for APPLET!! No Java 2 SDK, Standard Edition v support for APPLET!! </OBJECT> Dynamic Versioning “If no version of Java is installed, or a version less than the major version of the family is installed, then this will cause automatic redirection to the latest.cab for the latest version in the family.”

July 14, Programming for WWW (Lecture#7) In-Young Ko, Information Communications University Other Plug-ins Macromedia Flash Player Macromedia Flash Player<object classid=“clsid:D27CDB6E-AE6D-11cf-96B ” classid=“clsid:D27CDB6E-AE6D-11cf-96B ” codebase=“ codebase=“ shockwave/cabs/flash/swflash.cab#version=6,0,29,0” shockwave/cabs/flash/swflash.cab#version=6,0,29,0” width="832" height="240"> width="832" height="240"> <embed src="images/flash/intropic.swf" quality="high" <embed src="images/flash/intropic.swf" quality="high" pluginspage=" pluginspage=" type="application/x-shockwave-flash“ type="application/x-shockwave-flash“ width="832" height="240"> width="832" height="240"> </object>