MCA-401: ADVANCED JAVA PROGRAMMING 1DEPTT. OF COMP. SC & APPLICATIONS PREPARED BY : NAVEEN NAGPAL (SENIOR ASSISTANT PROFESSOR)

Slides:



Advertisements
Similar presentations
1 Graphical User Interface (GUI) Applications Abstract Windowing Toolkit (AWT) Events Handling Applets.
Advertisements

Introduction to Java 2 Programming
Java GUI building with the AWT. AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Objectives The objectives of this chapter are: To discuss the classes present in the java.awt package To understand the inheritance hierarchy of the AWT.
AWT Components. 2 Using AWT Components 4 Component –Canvas –Scrollbar –Button –Checkbox –Label –List –Choice –TextComponent –TextArea –TextField 4 Component.
Graphic User Interfaces Layout Managers Event Handling.
CMSC 341 Building Java GUIs. 09/26/2007 CMSC 341 GUI 2 Why Java GUI Development? Course is about Data Structures, not GUIs. We are giving you the opportunity.
Corresponds with Chapter 12
Applets The objectives of this chapter are: To describe applets and their purpose To discuss embedding applets in HTML pages.
Graphics Programming With Applets Feb 23, Applets There are three different types of executable java code. –Standalone application, which has main()
Inheritance Review CSC 171 FALL 2004 LECTURE 19. READING Read Horstmann, Chapter 11 Look at Chapter 12 – Will not be on MT or Final – One lab Begin Reading.
Advanced Java Class GUI – part 1. Intro to GUI GUI = Graphical User Interface -- “Gooey” Just because it’s “gooey” does not mean you may write messy code.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Io package as Java’s basic I/O system continue’d.
Java Programming Chapter 10 Graphical User Interfaces.
20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 16: Java Applets & AWT Fundamentals of Web Programming.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
Java Software Solutions Lewis and Loftus Chapter 10 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphical User Interfaces --
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
CIS 068 Welcome to CIS 083 ! Introduction to GUIs: JAVA Swing.
JAPPLET.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
CSE 219 Computer Science III Graphical User Interface.
Java GUI building with the AWT. AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Graphical User Interface CSI 1101 N. El Kadri. Plan - agenda Graphical components Model-View-Controller Observer/Observable.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
Abstract Window Toolkit (AWT) The Abstract Window Toolkit (AWT) supports Graphical User Interface (GUI) programming. AWT features include:  A rich set.
 2002 Prentice Hall, Inc. All rights reserved Introduction Graphical User Interface (GUI) –Gives program distinctive “look” and “feel” –Provides.
– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.
1 Outline 1 Introduction 2 Overview of Swing Components 3 JLabel 4 Event Handling 5 TextFields 6 How Event Handling Works 7 JButton 8 JCheckBox and JRadioButton.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
1 Unit 5 GUI Aum Amriteshwaryai Namah. 2 Overview Shall learn how to reuse the graphics classes provided by Java for constructing Graphical User Interface.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
Timer class and inner classes. Processing timer events Timer is part of javax.swing helps manage activity over time Use it to set up a timer to generate.
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.
The Abstract Window Toolkit (AWT) supports Graphical User Interface (GUI) programming. AWT features include: a rich set of user interface components; a.
SEEM3460 Tutorial GUI in Java. Some Basic GUI Terms Component (Control in some languages) the basic GUI unit something visible something that user can.
Introduction to GUI in 1 Graphical User Interface 2 Nouf Almunyif.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
Introduction to Java Chapter 9 - Graphical User Interfaces and Applets1 Chapter 9 Graphical User Interfaces and Applets.
Csc Basic Graphical User Interface (GUI) Components.
Creating a Window. A basic window in Java is represented by an object of the class Window in the package java.awt.
CSI 3125, Preliminaries, page 1 AWT. CSI 3125, Preliminaries, page 2 AWT Java AWT (Abstract Windowing Toolkit) is an API to develop GUI or window-based.
Graphical User Interfaces (GUI). PART ONE About GUI’s.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
1 Lecture 8: User Interface Components with Swing.
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.
Introduction to GUI in 1 Graphical User Interface 3 Nouf Almunyif.
Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts.
AWT Vs SWING. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in most Java textbooks Adequate for many applications Uses.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 Event-Driven Programming and Basic GUI Objects.
Aum Amriteshwaryai Namah
A First Look at GUI Applications
Course Outcomes of Advanced Java Programming AJP (17625, C603)
Abstract Window ToolKit (AWT)
Ellen Walker Hiram College
Chap 7. Building Java Graphical User Interfaces
Chapter 13: Advanced GUIs and Graphics
Graphical User Interfaces -- Introduction
AWT Components and Containers
GUI building with the AWT
AWT Components Java AWT components are platform-dependent i.e. components are displayed according to the view of operating system. AWT is heavyweight i.e.
Objectives The objectives of this chapter are:
Advanced GUIs and Graphics
Graphical User Interface
Java Chapter 1 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

MCA-401: ADVANCED JAVA PROGRAMMING 1DEPTT. OF COMP. SC & APPLICATIONS PREPARED BY : NAVEEN NAGPAL (SENIOR ASSISTANT PROFESSOR)

SYLLABUS Unit-I Conti.. DEPTT. OF COMP. SC & APPLICATIONS2 Review of Applets, Class, Event Handling, AWT Programming

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 & APPLICATIONS3

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 & APPLICATIONS4

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 & APPLICATIONS5

AWT awt stands for abstract window toolkit. The Abstract Window Toolkit (AWT) is java’s original platform- dependent 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 & APPLICATIONS6

Panel Button Checkbox Choice Label List Component ContainerFrameWindow TextAreaTextFieldTextComponent AWT Class Hierarchy DEPTT. OF COMP. SC & APPLICATIONS7

Classes used for awt DEPTT. OF COMP. SC & APPLICATIONS8

Classes used for awt conti… DEPTT. OF COMP. SC & APPLICATIONS9

Classes used for awt conti… DEPTT. OF COMP. SC & APPLICATIONS10

Classes used for awt conti… DEPTT. OF COMP. SC & APPLICATIONS11

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 & APPLICATIONS12

PIC OF CLASS HIERARCHY IN AWT DEPTT. OF COMP. SC & APPLICATIONS13

Component is the superclass of most of the displayable classes defined within the AWT.. MenuComponent 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 setBounds setSize setLocation setFont setEnabled setVisible setForeground-- colour setBackground-- colour Component DEPTT. OF COMP. SC & APPLICATIONS14

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 getComponent getMaximumSize getMinimumSize getPreferredSize remove removeAll Container DEPTT. OF COMP. SC & APPLICATIONS15

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 aFrame = new Frame( “ Hello World ” ); aFrame.setSize(100,100); aFrame.setLocation(10,10); aFrame.setVisible(true); Windows and Frames DEPTT. OF COMP. SC & APPLICATIONS16

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 aPanel = new Panel(); aPanel.add(new Button("Ok")); aPanel.add(new Button("Cancel")); Frame aFrame = new Frame("Button Test"); aFrame.setSize(100,100); aFrame.setLocation(10,10); aFrame.add(aPanel); Panels DEPTT. OF COMP. SC & APPLICATIONS17

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 ActionListener Interface. Panel aPanel = new Panel(); Button okButton = new Button("Ok"); Button cancelButton = new Button("Cancel"); aPanel.add(okButton)); aPanel.add(cancelButton)); okButton.addActionListener(controller2); cancelButton.addActionListener(controller1); Buttons DEPTT. OF COMP. SC & APPLICATIONS18

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 aLabel = new Label("Enter password:"); aLabel.setAlignment(Label.RIGHT); aPanel.add(aLabel); Labels DEPTT. OF COMP. SC & APPLICATIONS19

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 aList = new List(); aList.add("Calgary"); aList.add("Edmonton"); aList.add("Regina"); aList.add("Vancouver"); aList.setMultipleMode(true); List DEPTT. OF COMP. SC & APPLICATIONS20

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 CheckBoxGroup, it will behave like a radio button. Checkbox creamCheckbox = new CheckBox("Cream"); Checkbox sugarCheckbox = new CheckBox("Sugar"); [ … ] if (creamCheckbox.getState()) { coffee.addCream(); } Checkbox DEPTT. OF COMP. SC & APPLICATIONS21

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 aChoice = new Choice(); aChoice.add("Calgary"); aChoice.add("Edmonton"); aChoice.add("Alert Bay"); [ … ] String selectedDestination= aChoice.getSelectedItem(); Choice DEPTT. OF COMP. SC & APPLICATIONS22

This class displays a single line of optionally editable text. This class inherits several methods from TextComponent. This is one of the most commonly used Components in the AWT TextField TextField = new TextField(); TextField passwordTextField = new TextField(); passwordTextField.setEchoChar("*"); […] String user = TextField.getText(); String userpassword = passwordTextField.getText(); TextField DEPTT. OF COMP. SC & APPLICATIONS23

This class displays multiple lines of optionally editable text. This class inherits several methods from TextComponent. TextArea also provides the methods: appendText(), insertText() and replaceText() // 5 rows, 80 columns TextArea fullAddressTextArea = new TextArea(5, 80); [ … ] String userFullAddress= fullAddressTextArea.getText(); TextArea DEPTT. OF COMP. SC & APPLICATIONS24

Since the Component class defines the setSize() and setLocation() 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 setSize and setLocation methods. Layout Managers DEPTT. OF COMP. SC & APPLICATIONS25

There are several different LayoutManagers, each of which sizes and positions its Components based on an algorithm: FlowLayout BorderLayout GridLayout For Windows and Frames, the default LayoutManager is BorderLayout. For Panels, the default LayoutManager is FlowLayout. Layout Managers (cont) DEPTT. OF COMP. SC & APPLICATIONS26

The algorithm used by the FlowLayout 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 aPanel = new Panel(); aPanel.add(new Button("Ok")); aPanel.add(new Button("Add")); aPanel.add(new Button("Delete")); aPanel.add(new Button("Cancel")); Flow Layout DEPTT. OF COMP. SC & APPLICATIONS27

The BorderLayout Manager breaks the Container up into 5 regions (North, South, East, West, and Center). When Components are added, their region is also specified: Frame aFrame = new Frame(); aFrame.add("North", new Button("Ok")); aFrame.add("South", new Button("Add")); aFrame.add("East", new Button("Delete")); aFrame.add("West", new Button("Cancel")); aFrame.add("Center", new Button("Recalculate")); Border Layout DEPTT. OF COMP. SC & APPLICATIONS28

The regions of the BorderLayout are defined as follows: Center North South WestEast Border Layout (cont) DEPTT. OF COMP. SC & APPLICATIONS29

The GridLayout 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 LayoutManager. Panel aPanel = new Panel(); GridLayout theLayout = new GridLayout(2,2); aPanel.setLayout(theLayout); aPanel.add(new Button("Ok")); aPanel.add(new Button("Add")); aPanel.add(new Button("Delete")); aPanel.add(new Button("Cancel")); Grid Layout DEPTT. OF COMP. SC & APPLICATIONS30

LayoutManagers have proved to be difficult and frustrating to deal with. The LayoutManager can be removed from a Container by invoking its setLayout method with a null parameter. Panel aPanel = new Panel(); aPanel.setLayout(null); What if I don’t want a LayoutManager? DEPTT. OF COMP. SC & APPLICATIONS31

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 getGraphics(). Common methods used in Graphics include: drawLine drawOval drawPolygon drawPolyLine drawRect drawRoundRect drawString draw3DRect fill3DRect fillArc • fillOval • fillPolygon • fillRect • fillRoundRect • setColor • setFont • setPaintMode • drawImage Graphics DEPTT. OF COMP. SC & APPLICATIONS32

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 33 DEPTT. OF COMP. SC & APPLICATIONS

EXAMPLE //WAP to make human face. import java.awt.*; import java.applet.*; /* public class Face extends Applet { public void paint(Graphics g) { g.drawOval(40,40,120,150); g.drawOval(57,75,30,20); g.drawOval(110,75,30,20); g.fillOval(68,81,10,10); g.fillOval(121,81,10,10); g.drawOval(85,100,30,30); g.fillArc(60,125,80,40,180,180); g.drawOval(25,92,15,30); g.drawOval(160,92,15,30); } DEPTT. OF COMP. SC & APPLICATIONS34

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 & APPLICATIONS36

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 addTypeListener(TypeListener el) Here,Type is the name of the event and el is a reference to the event listener DEPTT. OF COMP. SC & APPLICATIONS37

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 removeTypeListener(TypeListener 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 removeKeyListener( ) DEPTT. OF COMP. SC & APPLICATIONS38

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 & APPLICATIONS39

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 EventObject, which is in java.util. package. It is the superclass for all events. Its one constructor is shown here: EventObject(Objectsrc) Here, src is the object that generates this event. EventObject contains two methods:getSource( )and toString( ). The getSource( ) method returns the source of the event. Its general form is as : Object getSource( ) As expected, toString( ) returns the string equivalent of the event. The class AWTEvent, defined within the java.awt package, is a subclass of EventObject. It is the superclass (either directly or indirectly) of all AWT-based events DEPTT. OF COMP. SC & APPLICATIONS40

Delegation Event Model conti.. The classAWTEvent, defined within the java.awt package, is a subclass of EventObject.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 & APPLICATIONS41

Delegation Event Model conti.. DEPTT. OF COMP. SC & APPLICATIONS42