Cs884(Prasad)java12AWT1 Abstract Windowing Toolkit Support for Graphical User Interface (Event-driven programming)

Slides:



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

Java GUI building with the AWT. AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Managing Input Events in Swing Week 5 Workshop Lyn Bartram.
Inner Classes in Java CMSC 432 Shon Vick. 2 Conceptual Structure package p 1 class A 1 class A 2... class A n... package n class A 1 class A 2... class.
Fall 2007CS 225 Graphical User Interfaces Event Handling Appendix C.
Jan Event Handling -1.1 Yangjun Chen Dept. Business Computing University of Winnipeg.
June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 1 Lecture 9 Object Oriented Programming in Java Advanced Topics Abstract Windowing.
Mouse Events and Keyboard Events
For IST410 Students only Events-1 Event Handling.
Graphics Programming with Inheritance Template pattern (Chap 6, se SceneEditor)
CS3157 Java UI Recitation. Material Covered: Overview of AWT components, Event Handling, creating applets, and sample UI. Not covered in recitation: Drawing,
Graphical User Interfaces (GUI) Abstract Windows Toolkit (AWT): java.awt GUI elements: Primitive Button, Label, Checkbox, Scrollbar, etc. Container Panel,
GUI. Swing Class Hierarchy Swing Components Swing Conatiners  JFrame – top-level window to store components.
Java GUI building with the AWT. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in most Java textbooks Adequate for many.
GUI Event Handling Nithya Raman. What is an Event? GUI components communicate with the rest of the applications through events. The source of an event.
9/21/99www.cs.vt.edu/wwtut/1 Introduction To Abstract Windowing Toolkit (AWT) Part 3 – AWT Marc Abrams Virginia Tech CS Dept courses.cs.vt.edu/wwwtut/
Mouse Events. Handling Mouse Events Java provides two listener interfaces to handle mouse events: MouseListener;  MouseListener;  MouseMotionListener.
Graphical User Interface CSI 1101 N. El Kadri. Plan - agenda Graphical components Model-View-Controller Observer/Observable.
Java GUI CSCE 190 – Java Instructor: Joel Gompert Mon, July 26, 2004.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
Applets Session 8. Java Simplified / Session 8 / 2 of 31 Review The Abstract Windowing Toolkit (AWT) is a set of classes that allow us to create a graphical.
Java GUI’s are event driven, meaning they generate events when the user interacts with the program. Typical events are moving the mouse, clicking a mouse.
SD2071 Games Programming Abstraction, inheritance and interfaces Exceptions Two dimensional arrays Java collections framework Files Aaron Kans.
 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.
Copyright © 2002, Systems and Computer Engineering, Carleton University b-Gui2.ppt * Object-Oriented Software Development Part 18-b Building.
COMP 321 Week 2. Outline Event-Driven Programming Events, Event Sources, Event Listeners Button and Timer Events Mouse Events, Adapters.
Lec 16 Adding Mouse and KeyEvent handlers to an Applet Class.
EE2E1. JAVA Programming Lecture 6 Event handling and building user interfaces with Swing.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
The Abstract Window Toolkit (AWT) supports Graphical User Interface (GUI) programming. AWT features include: a rich set of user interface components; a.
Universidad Nacional de Colombia Facultad de Ingeniería Departamento de Sistemas ertificación en AVA.
Object Oriented Programming.  Interface  Event Handling.
Event Handling. The signals that a program receives from the operating system as a result of the actions are called events. A window based program is.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
Index Event Handling Events Event Source Event Listener Event Classes Action Event Class Adjustment event Class Event Source Event Listener Interface Using.
GUI DYNAMICS Lecture 11 CS2110 – Fall GUI Statics and GUI Dynamics  Statics: what’s drawn on the screen  Components buttons, labels, lists, sliders,
Csc Basic Graphical User Interface (GUI) Components.
CS Lecture 04 Mice Lynda Thomas
Graphical User Interfaces (GUI). PART ONE About GUI’s.
1 Event Handling – Lecture 4 Prepared by: Ahmad Ramin Rahimee Assistant Professor ICTI.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 10.
What Is an Event? Events – Objects that describe what happened Event sources – The generator of an event Event handlers – A method that receives an event.
CSI 3125, Preliminaries, page 1 Event Handling. CSI 3125, Preliminaries, page 2 Event Handling An Event Change in the state of an object is known as event.
Mouse Events GUI. Types of Events  Below, are some of the many kinds of events, swing components generate. Act causing EventListener Type User clicks.
1 IM103 week 8 (C&K ch17, p412) Advanced graphic programming Learning objectives By the end of this chapter you should be able to:  create dialogue windows.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
TENTH LECTURE Event and listener. Events and Listeners An event can be defined as a type of signal to the program that something has happened. The event.
1 A Quick Java Swing Tutorial. 2 Introduction Swing – A set of GUI classes –Part of the Java's standard library –Much better than the previous library:
AWT Vs SWING. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in most Java textbooks Adequate for many applications Uses.
Dept. of CSIE, National University of Tainan 10/21/2012 Responding to User Input.
CSC 205 Programming II Lecture 5 AWT - I.
Events and Event Handling
Aum Amriteshwaryai Namah
INFSY 547: WEB-Based Technologies
Computer Science 209 Graphics and GUIs.
A Quick Java Swing Tutorial
Programming in Java Event Handling
Ellen Walker Hiram College
GUI Event Handling Nithya Raman.
GUI Programming III: Events
Printing in Java Printing in Java 1.1 is implemented through the new PrintJob class and PrintGraphics interface When a PrintJob object is created, the.
A Quick Java Swing Tutorial
Events, Event Handlers, and Threads
Programming Graphical User Interface (GUI)
GUI building with the AWT
Presentation transcript:

cs884(Prasad)java12AWT1 Abstract Windowing Toolkit Support for Graphical User Interface (Event-driven programming)

cs884(Prasad)java12AWT2 Batch Programs  Interactive Programs  Graphical User Interfaces AWT –Classes for creating GUIs; organized as inheritance hierarchy. –Define the structure (geometry) and the (default) behavior of the components (“look and feel”) –Java-Components ~ X-Widgets ~ ActiveX- Controls

cs884(Prasad)java12AWT3 AWT classes Component: –Basic: Button, Choice, Checkbox, List, Canvas, Label, Scrollbar, etc –Container: Panel, ScrollPane, Window (Dialog, FileDialog, Frame), etc A container instance can hold component instances. LayoutManager: FlowLayout, GridLayout, BorderLayout, CardLayout, etc Automatically manage the relative positioning of the component instances within a container instance.

cs884(Prasad)java12AWT4 Structure Example import java.awt.*; public class DemoAwt extends Frame { Label l = new Label(“Demo”, Label.CENTER); Button b = new Button(“No Op”); Checkbox c = new Checkbox(“WindowsNT”); List li = new List(); … constructor definition... public static void main (String[] argv) { new DemoAwt(); }

cs884(Prasad)java12AWT5 {... DemoAwt() { resize(200,300); setLayout(new FlowLayout()); add(l); add(b); add(new TextField(“TEXT”) ); add(c); add(li); li.addItem(“Netcape”); li.addItem(“SUN”); show();...}

cs884(Prasad)java12AWT6 {... DemoAwt() { resize(250,150); setLayout(new BorderLayout()); // default add(“North”,l); add(“South”,b); add(“Center”, new TextField(“T”)); add(“East”,c); add(“West”,li); li.addItem(“Netcape”); li.addItem(“SUN”); show();...}

cs884(Prasad)java12AWT7 Applet Version import java.awt.*; import java.applet.*; public class DemoAwt2 extends Applet { … init-code... public void start() { show(); } public void stop() { hide(); }

cs884(Prasad)java12AWT8 public void init() { List li = new List(); li.addItem(“Netcape”); li.addItem(“SUN”); add(new Label(“Demo”)); add(new Button(“No Op”)); add(new Checkbox(“WindowsNT”)); add(li); } /* */

cs884(Prasad)java12AWT9 Adding Behavior : Event Model An event, such as mouse click, mouse motion, keyboard input, etc, associated with a component, can trigger a specific (event handler) method. Event instance fields id, target, x, y, when, key, modifier, etc. An event model specifies the protocol used to process/handle events.

cs884(Prasad)java12AWT10 Role of inheritance hierarchy –To associate an event-handler with a component class, it must be sub-classed, to override the default handler. Role of containment hierarchy – If an event-handler associated with the target returns true, then the event has been processed. Otherwise, the event is propagated to its container, for further processing. Java 1.0 Event Model

cs884(Prasad)java12AWT11 Problems Code Organization –Proliferation of sub-classes. –Complex switch in the top-level handleEvent (). No clear separation between application code and the GUI. Efficiency –No filtering of events. (Events delivered to a component even when an event is not handled.)

cs884(Prasad)java12AWT12 Delegation-based Model –Event object propagated from source to listener by invoking an event-specific method on a listener. –The listener implements an appropriate EventListener interface, and registers itself with the source. Event model for Java 1.1 and Beans sourcelistener event

cs884(Prasad)java12AWT13 OO-Events – java.awt.events.* – class java.util.EventObject – interface java.util.EventListener “Design Pattern” –event type EVevent –interface EVListener –source. addEVListener (target) –source. setEVListener (target) –target implements EVListener –class EVAdapter Source can safely call any method in the interface on all (corresponding) listener targets.

cs884(Prasad)java12AWT14 import java.applet.*; import java.awt.*; import java.awt.event.*; public class Scribble11 extends Applet implements MouseListener, MouseMotionListener { int oldX, oldY; Graphics g; …init()... public void mousePressed(MouseEvent e) { oldX = e.getX(); oldY = e.getY(); } public void mouseReleased(MouseEvent e){} public void mouseClicked(MouseEvent e){} public void mouseEntered(MouseEvent e){} public void mouseExited(MouseEvent e){} public void mouseMoved(MouseEvent e){} public void mouseDragged(MouseEvent e) { g.drawLine( oldX, oldY, e.getX(), getY()); }}

cs884(Prasad)java12AWT15 public void init(){ g = getGraphics(); addMouseListener(this); addMouseMotionListener(this); Button bgB = new Button("Change Color"); bgB.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { setBackground( newColor()); repaint(); } } ); add(bgB); Button clearB = new Button("Clear"); clearB.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Color c = g.getColor(); g.setColor(getBackground()); g.fillRect(0, 0, bounds().width, bounds().height); g.setColor(c);} } ; add(clearB); }

cs884(Prasad)java12AWT16 import java.applet.*; import java.awt.*; import java.awt.event.*; public class Scribble22 extends Applet { int oldX, oldY; Graphics g; public void init() { g = getGraphics(); addMouseListener( new MouseAdapter() { public void mousePressed(MouseEvent e) { oldX = e.getX(); oldY = e.getY(); } } ); addMouseMotionListener( new MouseMotionAdapter (){ public void mouseDragged(MouseEvent e) { g.drawLine( oldX, oldY, e.getX(), e.getY()); } } );

cs884(Prasad)java12AWT17 Button bgB = new Button("Change Color"); bgB.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { setBackground( newColor()); repaint(); } } ); add(bgB); Button clearB = new Button("Clear"); clearB.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Color c = g.getColor(); g.setColor(getBackground()); g.fillRect(0, 0, bounds().width, bounds().height); g.setColor(c);} } ; add(clearB); }

cs884(Prasad)java12AWT19

cs884(Prasad)java12AWT20

cs884(Prasad)java12AWT21

cs884(Prasad)java12AWT22

cs884(Prasad)java12AWT23 Advantages Flexible source-listener association dynamic. 1-1, 1-n, n-1, n-m source-listener combinations. Efficient Event-filtering: Deliver only to registered listeners. Separation of Application and GUI code Enable (tool builders) run-time discovery of events that a component generates/observes.

cs884(Prasad)java12AWT24 Low-level Event Handling One can subclass a component, to process events, by overriding the following methods: –protected void processEvent(AWTEvent); –protected void processEV?Event(EV?Event); These are analogous to Java 1.0 handleEvent() and specific event-handlers respectively. It is necessary to enable delivery of events using: –protected void enableEvents(long eventsToEnable ); Note: one cannot mix 1.0 and 1.1 event models.

cs884(Prasad)java12AWT25 import java.applet.*; import java.awt.*; import java.awt.event.*; public class Scribble33 extends Applet { int oldX, oldY; Graphics g; public void init() { add( new Label("Scribbler: Press 'c' to clear.") ); g = getGraphics(); enableEvents(AWTEvent.MOUSE_EVENT_MASK | AWTEvent.MOUSE_MOTION_EVENT_MASK | AWTEvent.KEY_EVENT_MASK); requestFocus(); }

cs884(Prasad)java12AWT26 public void processMouseEvent(MouseEvent e) { if (e.getID() == MouseEvent.MOUSE_PRESSED) { oldX = e.getX(); oldY = e.getY(); } else super.processMouseEvent(e); } public void processMouseMotionEvent(MouseEvent e) { if (e.getID() == MouseEvent.MOUSE_DRAGGED) { int x = e.getX(); int y = e.getY(); g.drawLine(oldX,oldY,x,y); oldX = x; oldY = y; } else super.processMouseMotionEvent(e); }

cs884(Prasad)java12AWT27 public void processKeyEvent(KeyEvent e) { if ( (e.getID() == KeyEvent.KEY_TYPED) && (e.getKeyChar() == 'c') ) { Color temp = g.getColor(); g.setColor(getBackground()); g.fillRect(0, 0, getSize().width, getSize().height); g.setColor(temp); } else super.processKeyEvent(e); }