Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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

2 July 14, 2004 2 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

3 July 14, 2004 3 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

4 July 14, 2004 4 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

5 July 14, 2004 5 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

6 July 14, 2004 6 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

7 July 14, 2004 7 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

8 July 14, 2004 8 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

9 July 14, 2004 9 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

10 July 14, 2004 10 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

11 July 14, 2004 11 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

12 July 14, 2004 12 Programming for WWW (Lecture#7) In-Young Ko, Information Communications University Applet GUI Structure http://java.sun.com/products/jfc/tsc/articles/containers/index.html

13 July 14, 2004 13 Programming for WWW (Lecture#7) In-Young Ko, Information Communications University Java GUI Component Layers http://java.sun.com/docs/books/tutorial/uiswing/components/rootpane.html Layered Pane

14 July 14, 2004 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();

15 July 14, 2004 15 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

16 July 14, 2004 16 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

17 July 14, 2004 17 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);

18 July 14, 2004 18 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”

19 July 14, 2004 19 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

20 July 14, 2004 20 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

21 July 14, 2004 21 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); } … } …}

22 July 14, 2004 22 Programming for WWW (Lecture#7) In-Young Ko, Information Communications University Applet & Swing References How to make Applets: http://java.sun.com/docs/books/tutorial/uiswing/components/applet. html How to make Applets: http://java.sun.com/docs/books/tutorial/uiswing/components/applet. html http://java.sun.com/docs/books/tutorial/uiswing/components/applet. html http://java.sun.com/docs/books/tutorial/uiswing/components/applet. html The Swing Tutorial: http://java.sun.com/docs/books/tutorial/uiswing/ The Swing Tutorial: http://java.sun.com/docs/books/tutorial/uiswing/ http://java.sun.com/docs/books/tutorial/uiswing/ Painting in AWT and Swing: http://java.sun.com/products/jfc/tsc/articles/painting/ Painting in AWT and Swing: http://java.sun.com/products/jfc/tsc/articles/painting/ http://java.sun.com/products/jfc/tsc/articles/painting/ Understanding Containers: http://java.sun.com/products/jfc/tsc/articles/containers/index.html Understanding Containers: http://java.sun.com/products/jfc/tsc/articles/containers/index.html http://java.sun.com/products/jfc/tsc/articles/containers/index.html

23 July 14, 2004 23 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., http://wp.netscape.com/plugins/?cp=brictrpr3

24 July 14, 2004 24 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 http://java.sun.com/j2se/1.4.2/docs/guide/plugin/developer_guide/contents.html

25 July 14, 2004 25 Programming for WWW (Lecture#7) In-Young Ko, Information Communications University Java Plug-in Tags Internet Explorer <OBJECT classid="clsid:CAFEEFAC-0014-0002-0000-ABCDEFFEDCBA" width=“600" height=“50" width=“600" height=“50" codebase="http://java.sun.com/products/plugin/autodl/ codebase="http://java.sun.com/products/plugin/autodl/ 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="http://java.sun.com/j2se/1.4.2/download.html" codebase="http://bigbear.icu.ac.kr/~iko/classes/ice1338/ “ code=“AppletTest.class“ message=“Information and Communications University“> No Java 2 SDK, Standard Edition v 1.4.2 support for APPLET!! No Java 2 SDK, Standard Edition v 1.4.2 support for APPLET!! </EMBED> Static Versioning

26 July 14, 2004 26 Programming for WWW (Lecture#7) In-Young Ko, Information Communications University Autodownload Files http://java.sun.com/j2se/1.5.0/docs /guide/deployment/deployment- guide/autodl-files.html

27 July 14, 2004 27 Programming for WWW (Lecture#7) In-Young Ko, Information Communications University Java Plug-in Tags (cont.) Combined Form <OBJECT classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93" width=“600" height=“50" width=“600" height=“50" codebase="http://java.sun.com/products/plugin/autodl/ codebase="http://java.sun.com/products/plugin/autodl/ 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=http://java.sun.com/j2se/1.4.2/download.html pluginspage=http://java.sun.com/j2se/1.4.2/download.htmlhttp://java.sun.com/j2se/1.4.2/download.html code=“AppletTest.class“ code=“AppletTest.class“ codebase="http://bigbear.icu.ac.kr/~iko/classes/ice1338/ “ codebase="http://bigbear.icu.ac.kr/~iko/classes/ice1338/ “ message=“Information and Communications University"> message=“Information and Communications University"> No Java 2 SDK, Standard Edition v 1.4.2 support for APPLET!! No Java 2 SDK, Standard Edition v 1.4.2 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.”

28 July 14, 2004 28 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-96B8-444553540000” classid=“clsid:D27CDB6E-AE6D-11cf-96B8-444553540000” codebase=“http://download.macromedia.com/pub/ codebase=“http://download.macromedia.com/pub/ 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="http://www.macromedia.com/go/getflashplayer" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash“ type="application/x-shockwave-flash“ width="832" height="240"> width="832" height="240"> </object>


Download ppt "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."

Similar presentations


Ads by Google