MCA401 ADVANCED JAVA PROGRAMMING PREPARED BY NAVEEN NAGPAL
MCA-401: ADVANCED JAVA PROGRAMMING PREPARED BY : NAVEEN NAGPAL (SENIOR ASSISTANT PROFESSOR) DEPTT. OF COMP. SC & APPLICATIONS 1
SYLLABUS Unit-I Conti. . Review of Applets, Class, Event Handling, AWT Programming DEPTT. OF COMP. SC & APPLICATIONS 2
Java Applet • A Java applet is a small application which is written in Java and delivered to users in the form of bytecode. The user launches the Java applet from a web page, and the applet is then executed within a Java Virtual Machine (JVM) in a process separate from the web browser itself. DEPTT. OF COMP. SC & APPLICATIONS 3
Advantage/Disadvanttage of Applet • It works at client side hence response time less. • Secured • It can be executed by differenct browsers supporting java running under different plateforms, including Linux, Windows, Mac Os etc Disadvantages of Applet: A plugin is required to run the java applet. DEPTT. OF COMP. SC & APPLICATIONS 4
Applet life Cycle • init : It is the initialization phase in which the java applet is initialized. • start : it is invoked after the init() method or browser is maximized. It is used to start the Applet • stop : is used to stop the Applet. It is invoked when Applet is stop or browser is minimized. • destroy : is used to destroy the Applet. It is invoked only once. • paint: it is not part of life cycle. It is used to display the contents on applet DEPTT. OF COMP. SC & APPLICATIONS 5
AWT • awt stands for abstract window toolkit. The Abstract Window Toolkit (AWT) is java’s original platformdependent windows, graphics , and user interface defined tool. • The AWT classes are contained in the java. awt package. It is one of Java’s largest packages. DEPTT. OF COMP. SC & APPLICATIONS 6
AWT Class Hierarchy Component Container Window Button Panel Frame List Checkbox Choice Label Text. Component Text. Field Text. Area DEPTT. OF COMP. SC & APPLICATIONS 7
Classes used for awt DEPTT. OF COMP. SC & APPLICATIONS 8
Classes used for awt conti… DEPTT. OF COMP. SC & APPLICATIONS 9
Classes used for awt conti… DEPTT. OF COMP. SC & APPLICATIONS 10
Classes used for awt conti… DEPTT. OF COMP. SC & APPLICATIONS 11
Window Fundamentals • The AWT defines windows according to a class hierarchy that adds functionality and specificity with each level. The two most common windows are those derived from Panel, which is used by applets, and those derived from Frame, which creates a standard window. Much of the functionality of these windows is derived from their parent classes. DEPTT. OF COMP. SC & APPLICATIONS 12
PIC OF CLASS HIERARCHY IN AWT DEPTT. OF COMP. SC & APPLICATIONS 13
Component is the superclass of most of the displayable classes defined within the AWT. . Menu. Component is another class which is similar to Component except it is the superclass for all GUI items which can be displayed within a drop-down menu. The Component class defines data and methods which are relevant to all Components set. Bounds set. Size set. Location set. Font set. Enabled set. Visible set. Foreground set. Background -- colour DEPTT. OF COMP. SC & APPLICATIONS 14
Container is a subclass of Component. (ie. All containers are themselves, Components) Containers contain components For a component to be placed on the screen, it must be placed within a Container The Container class defined all the data and methods necessary for managing groups of Components add get. Component get. Maximum. Size get. Minimum. Size get. Preferred. Size remove. All DEPTT. OF COMP. SC & APPLICATIONS 15
Windows and Frames The Window class defines a top-level Window with no Borders or Menu bar. Usually used for application splash screens • Frame defines a top-level Window with Borders and a Menu Bar • Frames are more commonly used than Windows Once defined, a Frame is a Container which can contain Components Frame a. Frame = new Frame(� Hello World� ); a. Frame. set. Size(100, 100); a. Frame. set. Location(10, 10); a. Frame. set. Visible(true); DEPTT. OF COMP. SC & APPLICATIONS 16
Panels When writing a GUI application, the GUI portion can become quite complex. To manage the complexity, GUIs are broken down into groups of components. Each group generally provides a unit of functionality. A Panel is a rectangular Container whose sole purpose is to hold and manage components within a GUI. Panel a. Panel = new Panel(); a. Panel. add(new Button("Ok")); a. Panel. add(new Button("Cancel")); Frame a. Frame = new Frame("Button Test"); a. Frame. set. Size(100, 100); a. Frame. set. Location(10, 10); a. Frame. add(a. Panel); DEPTT. OF COMP. SC & APPLICATIONS 17
Buttons This class represents a push-button which displays some specified text. When a button is pressed, it notifies its Listeners. (More about Listeners in the next chapter). To be a Listener for a button, an object must implement the Action. Listener Interface. Panel a. Panel = new Panel(); Button ok. Button = new Button("Ok"); Button cancel. Button = new Button("Cancel"); a. Panel. add(ok. Button)); a. Panel. add(cancel. Button)); ok. Button. add. Action. Listener(controller 2); cancel. Button. add. Action. Listener(controller 1); DEPTT. OF COMP. SC & APPLICATIONS 18
Labels This class is a Component which displays a single line of text. Labels are read-only. That is, the user cannot click on a label to edit the text it displays. Text can be aligned within the label Label a. Label = new Label("Enter password: "); a. Label. set. Alignment(Label. RIGHT); a. Panel. add(a. Label); DEPTT. OF COMP. SC & APPLICATIONS 19
List This class is a Component which displays a list of Strings. The list is scrollable, if necessary. Sometimes called Listbox in other languages. Lists can be set up to allow single or multiple selections. The list will return an array indicating which Strings are selected List a. List = new List(); a. List. add("Calgary"); a. List. add("Edmonton"); a. List. add("Regina"); a. List. add("Vancouver"); a. List. set. Multiple. Mode(true); DEPTT. OF COMP. SC & APPLICATIONS 20
Checkbox This class represents a GUI checkbox with a textual label. The Checkbox maintains a boolean state indicating whether it is checked or not. If a Checkbox is added to a Check. Box. Group, it will behave like a radio button. Checkbox cream. Checkbox = new Check. Box("Cream"); Checkbox sugar. Checkbox = new Check. Box("Sugar"); [� ] if (cream. Checkbox. get. State()) { coffee. add. Cream(); } DEPTT. OF COMP. SC & APPLICATIONS 21
Choice This class represents a dropdown list of Strings. Similar to a list in terms of functionality, but displayed differently. Only one item from the list can be selected at one time and the currently selected element is displayed. Choice a. Choice = new Choice(); a. Choice. add("Calgary"); a. Choice. add("Edmonton"); a. Choice. add("Alert Bay"); [� ] String selected. Destination= a. Choice. get. Selected. Item(); DEPTT. OF COMP. SC & APPLICATIONS 22
Text. Field This class displays a single line of optionally editable text. This class inherits several methods from Text. Component. This is one of the most commonly used Components in the AWT Text. Field email. Text. Field = new Text. Field(); Text. Field password. Text. Field = new Text. Field(); password. Text. Field. set. Echo. Char("*"); […] String user. Email = email. Text. Field. get. Text(); String userpassword = password. Text. Field. get. Text(); DEPTT. OF COMP. SC & APPLICATIONS 23
Text. Area This class displays multiple lines of optionally editable text. This class inherits several methods from Text. Component. Text. Area also provides the methods: append. Text(), insert. Text() and replace. Text() // 5 rows, 80 columns Text. Area full. Address. Text. Area = new Text. Area(5, 80); [� ] String user. Full. Address= full. Address. Text. Area. get. Text(); DEPTT. OF COMP. SC & APPLICATIONS 24
Layout Managers Since the Component class defines the set. Size() and set. Location() methods, all Components can be sized and positioned with those methods. Problem: the parameters provided to those methods are defined in terms of pixels. Pixel sizes may be different (depending on the platform) so the use of those methods tends to produce GUIs which will not display properly on all platforms. Solution: Layout Managers. Layout managers are assigned to Containers. When a Component is added to a Container, its Layout Manager is consulted in order to determine the size and placement of the Component. NOTE: If you use a Layout Manager, you can no longer change the size and location of a Component through the set. Size and set. Location methods. DEPTT. OF COMP. SC & APPLICATIONS 25
Layout Managers (cont) There are several different Layout. Managers, each of which sizes and positions its Components based on an algorithm: Flow. Layout Border. Layout Grid. Layout For Windows and Frames, the default Layout. Manager is Border. Layout. For Panels, the default Layout. Manager is Flow. Layout. DEPTT. OF COMP. SC & APPLICATIONS 26
Flow Layout The algorithm used by the Flow. Layout is to lay out Components like words on a page: Left to right, top to bottom. It fits as many Components into a given row before moving to the next row. Panel a. Panel = a. Panel. add(new new Panel(); Button("Ok")); Button("Add")); Button("Delete")); Button("Cancel")); DEPTT. OF COMP. SC & APPLICATIONS 27
Border Layout The Border. Layout Manager breaks the Container up into 5 regions (North, South, East, West, and Center). When Components are added, their region is also specified: Frame a. Frame = new Frame(); a. Frame. add("North", new Button("Ok")); a. Frame. add("South", new Button("Add")); a. Frame. add("East", new Button("Delete")); a. Frame. add("West", new Button("Cancel")); a. Frame. add("Center", new Button("Recalculate")); DEPTT. OF COMP. SC & APPLICATIONS 28
Border Layout (cont) The regions of the Border. Layout are defined as follows: North West Center East South DEPTT. OF COMP. SC & APPLICATIONS 29
Grid Layout The Grid. Layout class divides the region into a grid of equally sized rows and columns. Components are added left-to-right, top-to-bottom. The number of rows and columns is specified in the constructor for the Layout. Manager. Panel a. Panel = new Panel(); Grid. Layout the. Layout = new Grid. Layout(2, 2); a. Panel. set. Layout(the. Layout); a. Panel. add(new Button("Ok")); Button("Add")); Button("Delete")); Button("Cancel")); DEPTT. OF COMP. SC & APPLICATIONS 30
What if I don’t want a Layout. Manager? Layout. Managers have proved to be difficult and frustrating to deal with. The Layout. Manager can be removed from a Container by invoking its set. Layout method with a null parameter. Panel a. Panel = new Panel(); a. Panel. set. Layout(null); DEPTT. OF COMP. SC & APPLICATIONS 31
Graphics It is possible to draw lines and various shapes within a Panel under the AWT. Each Component contains a Graphics object which defines a Graphics Context which can be obtained by a call to get. Graphics(). Common methods used in Graphics include: draw. Line draw. Oval draw. Polygon draw. Poly. Line draw. Rect draw. Round. Rect draw. String draw 3 DRect fill. Arc � fill. Oval � fill. Polygon � fill. Rect � fill. Round. Rect � set. Color � set. Font � set. Paint. Mode � draw. Image DEPTT. OF COMP. SC & APPLICATIONS 32
Graphics in Applet • The Graphics class is the abstract base class for all graphics contexts that allow an application to draw onto components that are realized on various devices, as well as onto off-screen images. Example to draw Human Face DEPTT. OF COMP. SC & APPLICATIONS 33
EXAMPLE //WAP to make human face. import java. awt. *; import java. applet. *; /*<applet code="Face. class" width=200 height=200> </applet> public class Face extends Applet { public void paint(Graphics g) { g. draw. Oval(40, 120, 150); g. draw. Oval(57, 75, 30, 20); g. draw. Oval(110, 75, 30, 20); g. fill. Oval(68, 81, 10); g. fill. Oval(121, 81, 10); g. draw. Oval(85, 100, 30); g. fill. Arc(60, 125, 80, 40, 180); g. draw. Oval(25, 92, 15, 30); g. draw. Oval(160, 92, 15, 30); } } DEPTT. OF COMP. SC & APPLICATIONS 34
HUMAN FACE APPLET(OUTPUT) DEPTT. OF COMP. SC & APPLICATIONS 35
Delegation Event Model • The modern approach to handling events is based on the delegation event model which defines standard and consistent mechanisms to generate and process events. Its concept is quite simple: a source generates an event and sends it to one or more listeners DEPTT. OF COMP. SC & APPLICATIONS 36
Delegation Event Model conti. . • Event : In the delegation model, an event is an object that describes a state change in a source. It can be generated as a consequence of a person interacting with the elements in a graphical user interface. Some of the activities that cause events to be generated are pressing a button, entering a character via the keyboard, selecting an item in a list, and clicking the mouse or it can generated with the help of timer. • Event Sources : A source is an object that generates an event. This occurs when the internal state of that object changes in some way. Sources may generate more than one type of event. A source must register listeners in order for the listeners to receive notifications about a specific type of event. Each type of event has its own registration method. the general form is public void add. Type. Listener(Type. Listener el) Here, Type is the name of the event and el is a reference to the event listener DEPTT. OF COMP. SC & APPLICATIONS 37
Delegation Event Model conti. . • A source must also provide a method that allows a listener to unregister an interest in a specific type of event. The general form of such a method is this: public void remove. Type. Listener(Type. Listener el) Here, Type is the name of the event and el is a reference to the event listener. For example, to remove a keyboard listener, you would call remove. Key. Listener( ) DEPTT. OF COMP. SC & APPLICATIONS 38
Delegation Event Model conti. . • Event Listeners : A listener is an object that is notified when an event occurs. It has two major requirements. First, it must have been registered with one or more sources to receive notifications about specific types of events. Second, it must implement methods to receive and process these notifications. The methods that receive and process events are defined in a set of interfaces found in java. awt. event package DEPTT. OF COMP. SC & APPLICATIONS 39
Delegation Event Model conti. . • Event Classes : The classes that represent events are at the core of Java’s event handling mechanism. Thus, we begin our study of event handling with a tour of the event classes. As you will see, they provide a consistent, easy-to-use means of encapsulating events. At the root of the Java event class hierarchy is Event. Object, which is in java. util. package. It is the superclass for all events. Its one constructor is shown here: Event. Object(Objectsrc) Here, src is the object that generates this event. Event. Object contains two methods: get. Source( )and to. String( ). The get. Source( ) method returns the source of the event. Its general form is as : Object get. Source( ) As expected, to. String( ) returns the string equivalent of the event. The class AWTEvent, defined within the java. awt package, is a subclass of Event. Object. It is the superclass (either directly or indirectly) of all AWT-based events DEPTT. OF COMP. SC & APPLICATIONS 40
Delegation Event Model conti. . • The class. AWTEvent, defined within the java. awt package, is a subclass of Event. Object. It is the superclass (either directly or indirectly) of all AWT-based events. • Some important class that are used in model are as : DEPTT. OF COMP. SC & APPLICATIONS 41
Delegation Event Model conti. . DEPTT. OF COMP. SC & APPLICATIONS 42
- Slides: 42