Chapter 9 © 2001 by Addison Wesley Longman, Inc. 1 Chapter 9 Sebesta: Programming the World Wide Web.

Slides:



Advertisements
Similar presentations
Java GUI building with the AWT. AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
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.
Made with love, by Zachary Langley Applets The Graphics Presentation.
1 More on Applets Overview l Changing Colors l Changing Fonts & Styles l Applet Life-Cycle l Input using Dialog Window l Input using HTML parameters l.
Graphics You draw on a Graphics object The Graphics object cannot directly be created by your code, instead one is generated when the method paintComponent.
LAB SESSION 7 Graphical user interface Applet fundamentals Methods in applets Execution of an applet Graphics class.
Graphic User Interfaces Layout Managers Event Handling.
GUI and Swing, part 2 The illustrated edition. Scroll bars As we have previously seen, a JTextArea has a fixed size, but the amount of text that can be.
Programming and Problem Solving With Java Copyright 1999, James M. Slack Applets What is an Applet? Applet Parameters Graphics in Applets Other Applet.
COS 381 Day 12. Agenda Assignment 2 Not corrected Viewed the results and it looks like many of you failed Assignment 3 Posted Due March 3 Problem 2, 3.
1 Frameworks. 2 Framework Set of cooperating classes/interfaces –Structure essential mechanisms of a problem domain –Programmer can extend framework classes,
COS 381 Day 10. Agenda Assignment 2 Posted Program a web-based Version of Soduku using JavaScript and DOM to learn how to play.
©2004 Brooks/Cole Applets Graphics & GUIs. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Graphical Programs Most applications these days are.
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Topics  Applets  Classes used for graphics Graphics Point Dimension.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 16 Applets.
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Java Applets What is an Applet? How do you create.
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.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 14 Applets, Images,
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Java Graphics Applets.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Applets and Graphics.
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.
Chapter 3 - Introduction to Java Applets Outline 3.1Introduction 3.2Thinking About Objects 3.4A Simple Java Applet: Drawing a String 3.5Two More Simple.
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.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Program Design With Methods And Graphics / Chapter 4 1 Abstract Window Toolkit.
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
Applets Java API.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Java Applets What is an Applet? How do you create.
20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 16: Java Applets & AWT Fundamentals of Web Programming.
Chapter 5 Java Script And Forms JavaScript, Third Edition.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
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.
Chapter 7 © 2003 by Addison-Wesley, Inc Introduction - Applets are relatively small Java programs whose execution is triggered by a browser - The.
Java applet basics, loading & displaying images After this section, you should be able to : –Use the applet tag and applet parameters –Describe what a.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 20.1 Test-Driving the Screen Saver Application.
JAPPLET.
Section 17.1 Add an audio file using HTML Create a form using HTML Add text boxes using HTML Add radio buttons and check boxes using HTML Add a pull-down.
Applets Applet is java program that can be embedded into HTML pages. Java applets runs on the java enabled web browsers such as mozilla and internet explorer.
Chapter 7 © 2005 by Addison Wesley Longman, Inc Introduction - Applets are relatively small Java programs whose execution is triggered by a browser.
Field Trip #19 Animations with Java By Keith Lynn.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
CSC 205 – Java Programming II Applet. Types of Java Programs Applets Applications Console applications Graphics applications Applications are stand-alone.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010.
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.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
Csc Basic Graphical User Interface (GUI) Components.
Creating Graphics in Java CSE301 University of Sunderland Harry R Erwin, PhD.
Classes Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Copyright © Curt Hill Applets A different type of program.
Creating a Java Application and Applet
CHAPTER Agenda Applets Servelets Browsers HelloWorld.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Introduction to Java Applets Will not cover Section 3.7 Thinking About Objects: Identifying.
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.
CSC 205 Programming II Lecture 5 AWT - I.
A First Look at GUI Applications
Lecture 09 Applets.
Ellen Walker Hiram College
Chapter 13: Advanced GUIs and Graphics
Java – Applet Class.
11.1 Applets & graphics.
Advanced GUIs and Graphics
Presentation transcript:

Chapter 9 © 2001 by Addison Wesley Longman, Inc. 1 Chapter 9 Sebesta: Programming the World Wide Web

Chapter 9 © 2001 by Addison Wesley Longman, Inc Introduction - 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 - The ‘standard’ operations of applets are provided by the parent class, Applet public class class_name extends Applet { … } - The Applet class descends directly from Panel, which descends directly from Component - The use of applets has been restrained by the lack of support for Swing - Specifically, the JVM that comes with NN4 does not support Swing, so NN4 users cannot execute applets that use it - Use of applets is still widespread, and there is heavy use in intranets, where all browsers can be required to support the latest JVM

Chapter 9 © 2001 by Addison Wesley Longman, Inc Introduction (continued) - Applets are an alternative to CGI and embedded scripts - Comparisons: - 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, even with just AWT (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, in the form of servlets, than a client-side tool

Chapter 9 © 2001 by Addison Wesley Longman, Inc Primary Applet Activities - Browser actions: a. Download and instantiate the applet class b. Call the applet’s init method c. Call the applet’s start method - This starts 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, the browser calls the applet’s destroy method - Every applet must override at least one of the three methods, init, start, or paint ( paint is inherited from Component )

Chapter 9 © 2001 by Addison Wesley Longman, Inc The paint Method - Always called by the browser (not the applet itself) - Takes one parameter, of class Graphics, which is defined in java.awt - The parameter object is created by the browser - The protocol is: public void paint(Graphics grafObj) { … } - The simplest use of paint is to display text, using the drawString method - Three parameters: a String literal, the x coordinate of the left end of the string, and the y coordinate of the base of the string (the coordinates are given in pixels)

Chapter 9 © 2001 by Addison Wesley Longman, Inc The paint Method (continued) /* welcomeMessage.java An applet to illustrate the display of a string */ import java.applet.Applet; import java.awt.*; public class welcomeMessage extends Applet { public void paint(Graphics grafObj) { grafObj.drawString( "Welcome to my home page!", 50, 50); } - Font Control - The Font class, defined in java.awt.Font, has three variables that specify the font name, style, and size of the font used by drawString The size parameter is in points - The styles are PLAIN, BOLD, and ITALIC - To change the font, create a Font object with the desired parameters and set it with the setFont method of Graphics, which takes a Font parameter

Chapter 9 © 2001 by Addison Wesley Longman, Inc The paint Method (continued) /* welcomeMessage_2.java An applet to illustrate the display of a string in specific font, font style, and font size */ import java.applet.Applet; import java.awt.*; public class welcomeMessage_2 extends Applet { Font myFont = new Font("TimesRoman", Font.ITALIC, 24); public void paint(Graphics grafObj) { grafObj.setFont(myFont); grafObj.drawString( "Welcome to my home page!", 50, 50); } 9.4 The Tag - Used to specify an applet in an HTML document - Creates a space in the document display for applet output (like does)

Chapter 9 © 2001 by Addison Wesley Longman, Inc The Tag (continued) <object codetype = "application/java" code = " applet_class_file " width = " applet display width " height = " applet display height "> - The display width and height are in pixels - The applet_class_file is the compiled version <!-- ch9_2.html To test the applet, welcomeMessage_2 --> Test welcomeMessage_2 <object codetype = "application/java" code = "welcomeMessage_2.class" width = "500" height = "100">

Chapter 9 © 2001 by Addison Wesley Longman, Inc The Tag (continued) 9.5 Applet Parameters - Applets can be sent parameters through HTML, using the tag and its two attributes, name and value - Parameter values are strings - e.g., - The applet gets the parameter values with getParameter, which takes a string parameter, which is the name of the parameter String myFruit = getParameter("fruit");

Chapter 9 © 2001 by Addison Wesley Longman, Inc Applet Parameters (continued) - If no parameter with the given name has been specified in the HTML document, getParameter returns null - By checking the return value against null, a default value can be set - If the parameter value is not really a string (although parameters are all sent as strings), the value returned from getParameter must be converted, as in String pString = getParameter("size"); if (pString == null) mySize = 24; else mySize = Integer.parseInt(pString); - The best place to put the code to get parameter values is in init - Parameters are stored in instance variables

Chapter 9 © 2001 by Addison Wesley Longman, Inc Applet Parameters (continued) <object codetype = "application/java" code = "welcomeMessage_3.class" width = "500" height = "100"> <param name = "size" value = "35"> 9.6 Simple Graphics (AWT) - Coordinate system: (0, 0) is at the upper left corner - The methods that draw graphic figures are called through the Graphics object (the parameter to paint ) - Lines are drawn with drawLine(x1, y1, x2, y2) - Draws a line from (x1, y1) to (x2, y2)

Chapter 9 © 2001 by Addison Wesley Longman, Inc Simple Graphics (AWT) (continued) - Rectangles are drawn with drawRect and fillRect - Both take four parameters, the coordinates of the upper left corner of the rectangle and the width and height of the rectangle (width and height are in pixels) - Rectangles with rounded corners can be drawn with drawRoundRect and fillRoundRect - These two take two more parameters, which specify the numbers of horizontal pixels and vertical pixels in the rounding /* rectangles.java */ import java.applet.Applet; import java.awt.*; public class rectangles extends Applet { public void paint(Graphics grafObj) { grafObj.drawRect(10, 10, 80, 60); grafObj.fillRect(120, 10, 60, 80); grafObj.drawRoundRect(10, 120, 80, 60, 20, 30); grafObj.fillRoundRect(120, 120, 60, 80, 40, 40); }

Chapter 9 © 2001 by Addison Wesley Longman, Inc Simple Graphics (AWT) (continued) - 3D rectangles can be created with a 5th parameter, true (not pushed) or false (pushed) - Polygons are drawn with drawPolygon, which takes three parameters, two arrays of coordinates of edge endpoints, and the number of edges public void paint(Graphics grafObj) { int xCoordinates [] = {30, 50, 64, 64, 50, 30, 16, 16, 30}; int yCoordinates [] = {10, 10, 24, 44, 58, 58, 44, 24, 10}; grafObj.drawPolygon(xCoordinates, yCoordinates, 9); }

Chapter 9 © 2001 by Addison Wesley Longman, Inc Simple Graphics (AWT) (continued) - drawPolygon can also take a single parameter, which is a Polygon object, whose constructor takes the same three parameters as drawPolygon - Ovals are like rectangles (same parameters) 9.7 Color - The Color class has predefined objects for common colors Color.white, Color.black, Color.gray, Color.red, Color.green, Color.blue, Color.yellow, Color.magenta, Color.cyan, Color.pink, Color.orange

Chapter 9 © 2001 by Addison Wesley Longman, Inc Color (continued) - An object for any color can be created with the Color constructor, as in Color myColor = new Color(x, y, z); - The color of the Graphics object can be set with setColor, as in grafObj.setColor(Color.cyan); - The foreground and background colors of the applet display are set with methods from Panel 9.8 Interactive Applets - Applet extends Panel, which extends Component - The Component class includes GUI components as subclasses - Panel objects are used to enclose GUI components - The Applet class is itself a GUI component

Chapter 9 © 2001 by Addison Wesley Longman, Inc Interactive Applets (continued) - Labels - Label objects are static strings Label labl1 = new Label("Click this button"); - Plain buttons Button myButton = new Button( "Click here for fun"); - Checkboxes Checkbox box1 = new Checkbox("Beer"); Checkbox box2 = new Checkbox("Pretzels"); - Radio buttons – just checkboxes in a CheckboxGroup pop = new CheckboxGroup(); Checkbox box1 = new Checkbox("Coke", pop, true); Checkbox box2 = new Checkbox("Pepsi", pop, false);

Chapter 9 © 2001 by Addison Wesley Longman, Inc Interactive Applets (continued) - Text boxes TextField myName = new TextField(30); - A Panel object is needed to contain components Panel myPanel = new Panel(); myPanel.setBackground(Color.yellow); myPanel.setForeground(Color.blue); myPanel.add(box1); - Layout Managers - Default is FlowLayout – just like a browser - GridLayout is like HTML document panels Panel buttonPanel = new Panel(); buttonPanel.setLayout( new GridLayout(3, 2, 10, 10)); - Three rows of two components each, with 10 pixels between the components --> SHOW Pizza.java

Chapter 9 © 2001 by Addison Wesley Longman, Inc Interactive Applets (continued)

Chapter 9 © 2001 by Addison Wesley Longman, Inc Interactive Applets (continued) - The Java Event Model - Related to the JavaScript event model - Event handlers are called event listeners - Connection of an event to a listener is through event listener registration - Done with a method of the class that implements the listener interface - The panel object that holds the components can be the event listener for those components - Event generators send messages (call methods) to registered event listeners when events occur - Event handling methods must conform to a standard protocol, which comes from an interface - We only consider the “semantic” events (there are also “low-level” events)

Chapter 9 © 2001 by Addison Wesley Longman, Inc Interactive Applets (continued) - Semantic Events ActionEvent click a button, select from a menu or list, or type the enter button in a text field AdjustmentEvent adjust a scroll bar ItemEvent select a checkbox or list item TextEvent change the contents of a text field or text area - For these four events, we have the following interfaces and handler methods: InterfaceHandler method ActionListeneractionPerformed AdjustmentListeneradjustmentValueChanged ItemListeneritemStateChanged TextListenertextValueChanged - The methods to register the listener is the interface name with “ add ” prepended - e.g., button1.addActionListener(myPanel);

Chapter 9 © 2001 by Addison Wesley Longman, Inc Interactive Applets (continued) - Event handlers get an event object as a parameter, through which information about the event can be gotten with methods, such as getState - e.g., button1.getState() returns true if the button is on, false otherwise - When an event handler has just a few lines, it can be implemented as an instance of an anonymous nested class - Example: a button that sets a font button.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { text.setFont(newFont); } }); --> SHOW RadioB.java

Chapter 9 © 2001 by Addison Wesley Longman, Inc Concurrency in Java - Our only interest in concurrency here is to illustrate how threads can be used to create animation in an applet - A thread of control is a sequence of program points reached as execution flows through the program - A nonconcurrent program has a single thread of control; a concurrent program has more than one - Java supports lightweight concurrency through its threads - 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 - The Thread class - Two essential methods, run and start - run is the concurrent method - start tells the run method to begin execution

Chapter 9 © 2001 by Addison Wesley Longman, Inc Concurrency in Java (continued) - All Java programs run in threads - For applications, when execution is to begin, a thread is created for main and its start method is called - For applets, when the browser finds one, it creates a thread and calls the applet --> SHOW Names.java, output, delayer, and output - Thread States - New - created, but start hasn’t been called - Runnable or ready - ready to run, but is not currently running - In the ready queue - Running - actually has the processor - Blocked - was running, but is not now, because it was interrupted (i/o, end of time slot, gave up its time slot, etc.) - Dead - either its stop was called or its run method completed its execution

Chapter 9 © 2001 by Addison Wesley Longman, Inc Concurrency in Java (continued) - Thread methods - yield is a request from the running thread to give up the processor; a static method - sleep( time ) - blocks the thread for at least as many milliseconds as the parameter specifies; also a static method - sleep can throw InterruptedException, which must be caught - stop - now deprecated, because of safety problems - Now we override it and just set the thread reference to null (destroys the thread) - An example - an animated digital clock - An applet must implement Runnable, its start and stop methods, and the repaint method of Graphics - repaint is called after the applet display has changed

Chapter 9 © 2001 by Addison Wesley Longman, Inc Concurrency in Java (continued) - Our applet is named Clock - Its start method creates a new Thread object, sending this to the constructor. This sets the new Thread object’s target to the Clock object, which forces the thread to get its run method from the Clock object - After creating the Thread object, start is called to start its execution - The run method sets a variable to the currently executing thread, and then loops as long as the thread is clockThread - The loop gets the new time with a new Date object and repaints the display every second