1 / 28

Programming for WWW (ICE 1338)

Programming for WWW (ICE 1338). Lecture #7 July 14, 2004 In-Young Ko iko .AT. i cu . ac.kr Information and Communications University (ICU). Announcements. Midterm Exam: 10:00AM – 11:30AM, Friday July 16 th Your grades of homework#1 are posted on the class Web page.

cwen
Download Presentation

Programming for WWW (ICE 1338)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming for WWW(ICE 1338) Lecture #7July 14, 2004In-Young Koiko .AT. icu.ac.krInformation and Communications University (ICU)

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

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

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

  5. Java Applets • Applets are relatively small Java programs whose execution is triggered by a browser • The purpose of an applet is to provide processingcapability and interactivity for HTML documents through widgets • 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 • Applets are an alternative to CGI and embeddedclient-side scripts AW lecture notes Programming for WWW (Lecture#7) In-Young Ko, Information Communications University

  6. Applets vs. JavaScript (CGI) • 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 • Java is faster than JavaScript • Java graphics are powerful, but JavaScript has none • JavaScript does not require the additional download from the server that is required for applets • Java may become more of a server-side tool, inthe form of servlets, than a client-side tool AW lecture notes Programming for WWW (Lecture#7) In-Young Ko, Information Communications University

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

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

  9. Applet Parameters • Applets can be sent parameters through HTML, using the <param> tag and its two attributes, name and value • Parameter values are strings- e.g., <param name = "fruit" value = "apple"> • The applet gets the parameter values with getParameter, which takes the name of the parameter • e.g., String myFruit = getParameter("fruit"); • 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: String pString = getParameter("size"); if (pString == null) mySize = 24; elsemySize = Integer.parseInt(pString); • The best place to put the code to get parameters is in init AW lecture notes Programming for WWW (Lecture#7) In-Young Ko, Information Communications University

  10. The JApplet Class • 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) • External frames can be created from an applet Programming for WWW (Lecture#7) In-Young Ko, Information Communications University

  11. Paint Method in Applet • 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 • The protocol of the paint method is: public void paint(Graphics grafObj) { … } • The Graphics object is created by the browser • Methods in Graphics: drawImage, drawLine, drawOval, drawPolygon, drawRect, drawString, fillOval, fillPolygon, fillRect, … AW lecture notes Programming for WWW (Lecture#7) In-Young Ko, Information Communications University

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

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

  14. 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 } }); 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(); Programming for WWW (Lecture#7) In-Young Ko, Information Communications University

  15. Button Pressed Event A JButton Event Listeners Event Handling in Java • 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 JButton helloButton = new JButton(“Hello”); helloButton.addActionListener(new AbstractAction() { public void actionPerformed(ActionEvent e) { System.out.println(“Hello World!”); } } Programming for WWW (Lecture#7) In-Young Ko, Information Communications University

  16. 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) • 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 handling methods must conform to a standard protocol, which comes from an interface Programming for WWW (Lecture#7) In-Young Ko, Information Communications University

  17. Event Classes and Handler Methods • Semantic Event Classes • 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 • 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: Interface Handler method ------------------- ------------------------- ActionListener actionPerformed ItemListener itemStateChanged • The methods to register the listener is the interface name with “add” prepended e.g., button1.addActionListener(this); Programming for WWW (Lecture#7) In-Young Ko, Information Communications University

  18. Main Program Control Printing “A” Printing “B” Forking two concurrent execution threads Java 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 () { public void run() { while (true) { try {System.out.print("A");sleep(0,1); } catch (Exception e) { } } }).start(); (new Thread () { public void run() { while (true) { try {System.out.print("B");sleep(0,1); } catch (Exception e) { } } }).start(); } Programming for WWW (Lecture#7) In-Young Ko, Information Communications University

  19. Java Threads • 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 • 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 • There are two ways to implement threads, as a subclass of Thread and by implementing theinterface Runnable • Two essential methods: • run is the concurrent method • start tells the run method to begin execution Programming for WWW (Lecture#7) In-Young Ko, Information Communications University

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

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

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

  23. Plug-ins • Code modules that are inserted into the browser • Adds new capabilities to the Web browser • e.g., http://wp.netscape.com/plugins/?cp=brictrpr3 Programming for WWW (Lecture#7) In-Young Ko, Information Communications University

  24. 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 • 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 • 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 Programming for WWW (Lecture#7) In-Young Ko, Information Communications University

  25. Java Plug-in Tags Internet Explorer <OBJECTclassid="clsid:CAFEEFAC-0014-0002-0000-ABCDEFFEDCBA" width=“600" height=“50" codebase="http://java.sun.com/products/plugin/autodl/ jinstall-1_4_2-windows-i586.cab"> <PARAM name="code" value=“AppletTest.class"> <PARAM name="codebase" value=“http://bigbear.icu.ac.kr/~iko/classes/ice1338/"> <PARAM name="type" value="application/x-java-applet;jpi-version=1.4.2"> <PARAM name=“message" value=“Information and Communications University”> </OBJECT> Netscape <EMBEDtype="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“> <NOEMBED>No Java 2 SDK, Standard Edition v 1.4.2 support for APPLET!! </NOEMBED> </EMBED> Static Versioning Programming for WWW (Lecture#7) In-Young Ko, Information Communications University

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

  27. Java Plug-in Tags (cont.) Combined Form <OBJECTclassid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93" width=“600" height=“50" codebase="http://java.sun.com/products/plugin/autodl/ jinstall-1_4_2-windows-i586.cab#Version=1,4,2,0"> <PARAM name="code" value=“AppletTest.class"> <PARAM name="codebase" value="http://bigbear.icu.ac.kr/~iko/classes/ice1338/"> <PARAM name="type" value="application/x-java-applet;jpi-version=1.4.2"> <PARAM name=“message" value=“Information and Communications University”> <COMMENT> <EMBEDtype="application/x-java-applet;jpi-version=1.4.2“ width="200"   height="200“ pluginspage=http://java.sun.com/j2se/1.4.2/download.html code=“AppletTest.class“ codebase="http://bigbear.icu.ac.kr/~iko/classes/ice1338/ “ message=“Information and Communications University"> <NOEMBED>No Java 2 SDK, Standard Edition v 1.4.2 support for APPLET!! </NOEMBED> </EMBED> </COMMENT> </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.” Programming for WWW (Lecture#7) In-Young Ko, Information Communications University

  28. Other Plug-ins • Macromedia Flash Player <object classid=“clsid:D27CDB6E-AE6D-11cf-96B8-444553540000” codebase=“http://download.macromedia.com/pub/ shockwave/cabs/flash/swflash.cab#version=6,0,29,0” width="832" height="240"> <param name="movie" value="images/flash/intropic.swf"> <param name="quality" value="high"> <embed src="images/flash/intropic.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash“ width="832" height="240"> </embed> </object> Programming for WWW (Lecture#7) In-Young Ko, Information Communications University

More Related