Abstract Windowing Toolkit Support for Graphical User Interface

Abstract Windowing Toolkit Support for Graphical User Interface (Event-driven programming) cs 884(Prasad) java 12 AWT 1

• 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 ~ Active. XControls cs 884(Prasad) java 12 AWT 2

AWT classes • Component: – Basic: Button, Choice, Checkbox, List, Canvas, Label, Scrollbar, etc – Container: Panel, Scroll. Pane, Window (Dialog, File. Dialog, Frame), etc • A container instance can hold component instances. • Layout. Manager: Flow. Layout, Grid. Layout, Border. Layout, Card. Layout, etc • Automatically manage the relative positioning of the component instances within a container instance. cs 884(Prasad) java 12 AWT 3

Structure Example import java. awt. *; public class Demo. Awt extends Frame { Label l = new Label(“Demo”, Label. CENTER); Button b = new Button(“No Op”); Checkbox c = new Checkbox(“Windows. NT”); List li = new List(); … constructor definition. . . public static void main (String[] argv) { new Demo. Awt(); } } cs 884(Prasad) java 12 AWT 4

{. . . Demo. Awt() { resize(200, 300); set. Layout(new Flow. Layout()); add(l); add(b); add(new Text. Field(“TEXT”) ); add(c); add(li); li. add. Item(“Netcape”); li. add. Item(“SUN”); show(); . . . } cs 884(Prasad) java 12 AWT 5

{. . . Demo. Awt() { resize(250, 150); set. Layout(new Border. Layout()); // default add(“North”, l); add(“South”, b); add(“Center”, new Text. Field(“T”)); add(“East”, c); add(“West”, li); li. add. Item(“Netcape”); li. add. Item(“SUN”); show(); . . . } cs 884(Prasad) java 12 AWT 6

Applet Version import java. awt. *; import java. applet. *; public class Demo. Awt 2 extends Applet { …init-code. . . public void start() { show(); } public void stop() { hide(); } } cs 884(Prasad) java 12 AWT 7

public void init() { List li = new List(); li. add. Item(“Netcape”); li. add. Item(“SUN”); add(new Label(“Demo”)); add(new Button(“No Op”)); add(new Checkbox(“Windows. NT”)); add(li); } /* <applet code=Demo. Awt 2 width=250 height=300> </applet> */ cs 884(Prasad) java 12 AWT 8

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. cs 884(Prasad) java 12 AWT 9

Java 1. 0 Event Model • 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. cs 884(Prasad) java 12 AWT 10

Problems • Code Organization – Proliferation of sub-classes. – Complex switch in the top-level handle. Event(). • 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. ) cs 884(Prasad) java 12 AWT 11

Event model for Java 1. 1 and Beans • Delegation-based Model – Event object propagated from source to listener by invoking an event-specific method on a listener. – The listener implements an appropriate Event. Listener interface, and registers itself with the source cs 884(Prasad) event java 12 AWT listener 12

• OO-Events – java. awt. events. * – class java. util. Event. Object – interface java. util. Event. Listener • “Design Pattern” – – – event type EVevent interface EVListener source. add. EVListener (target) source. set. EVListener (target) target implements EVListener class EVAdapter Source can safely call any method in the interface on all (corresponding) listener targets. cs 884(Prasad) java 12 AWT 13

import java. applet. *; import java. awt. event. *; public class Scribble 11 extends Applet implements Mouse. Listener, Mouse. Motion. Listener { int old. X, old. Y; Graphics g; …init(). . . public void mouse. Pressed(Mouse. Event e) { old. X = e. get. X(); old. Y = e. get. Y(); } public void mouse. Released(Mouse. Event e){} public void mouse. Clicked(Mouse. Event e){} public void mouse. Entered(Mouse. Event e){} public void mouse. Exited(Mouse. Event e){} public void mouse. Moved(Mouse. Event e){} public void mouse. Dragged(Mouse. Event e) { g. draw. Line( old. X, old. Y, e. get. X(), get. Y()); }} cs 884(Prasad) java 12 AWT 14

public void init() { g = get. Graphics(); add. Mouse. Listener(this); add. Mouse. Motion. Listener(this); Button bg. B = new Button("Change Color"); bg. B. add. Action. Listener(new Action. Listener() { public void action. Performed(Action. Event e) { set. Background( new. Color()); repaint(); } } ); add(bg. B); Button clear. B = new Button("Clear"); clear. B. add. Action. Listener(new Action. Listener() { public void action. Performed(Action. Event e) { Color c = g. get. Color(); g. set. Color(get. Background()); g. fill. Rect(0, 0, bounds(). width, bounds(). height); g. set. Color(c); } } ; add(clear. B); } cs 884(Prasad) java 12 AWT 15

import java. applet. *; import java. awt. event. *; public class Scribble 22 extends Applet { int old. X, old. Y; Graphics g; public void init() { g = get. Graphics(); add. Mouse. Listener( new Mouse. Adapter() { public void mouse. Pressed(Mouse. Event e) { old. X = e. get. X(); old. Y = e. get. Y(); } } ); add. Mouse. Motion. Listener( new Mouse. Motion. Adapter(){ public void mouse. Dragged(Mouse. Event e) { g. draw. Line( old. X, old. Y, e. get. X(), e. get. Y()); }} ); cs 884(Prasad) java 12 AWT 16

Button bg. B = new Button("Change Color"); bg. B. add. Action. Listener(new Action. Listener() { public void action. Performed(Action. Event e) { set. Background( new. Color()); repaint(); } } ); add(bg. B); Button clear. B = new Button("Clear"); clear. B. add. Action. Listener(new Action. Listener() { public void action. Performed(Action. Event e) { Color c = g. get. Color(); g. set. Color(get. Background()); g. fill. Rect(0, 0, bounds(). width, bounds(). height); g. set. Color(c); } } ; add(clear. B); } cs 884(Prasad) java 12 AWT 17


cs 884(Prasad) java 12 AWT 19

cs 884(Prasad) java 12 AWT 20

cs 884(Prasad) java 12 AWT 21

cs 884(Prasad) java 12 AWT 22

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. cs 884(Prasad) java 12 AWT 23

Low-level Event Handling • One can subclass a component, to process events, by overriding the following methods: – protected void process. Event(AWTEvent); – protected void process. EV? Event(EV? Event); These are analogous to Java 1. 0 handle. Event() and specific event-handlers respectively. • It is necessary to enable delivery of events using: – protected void enable. Events(long events. To. Enable); Note: one cannot mix 1. 0 and 1. 1 event models. cs 884(Prasad) java 12 AWT 24

import java. applet. *; import java. awt. event. *; public class Scribble 33 extends Applet { int old. X, old. Y; Graphics g; public void init() { add( new Label("Scribbler: Press 'c' to clear. ") ); g = get. Graphics(); enable. Events(AWTEvent. MOUSE_EVENT_MASK | AWTEvent. MOUSE_MOTION_EVENT_MASK | AWTEvent. KEY_EVENT_MASK); request. Focus(); } cs 884(Prasad) java 12 AWT 25

public void process. Mouse. Event(Mouse. Event e) { if (e. get. ID() == Mouse. Event. MOUSE_PRESSED) { old. X = e. get. X(); old. Y = e. get. Y(); } else super. process. Mouse. Event(e); } public void process. Mouse. Motion. Event(Mouse. Event e) { if (e. get. ID() == Mouse. Event. MOUSE_DRAGGED) { int x = e. get. X(); int y = e. get. Y(); g. draw. Line(old. X, old. Y, x, y); old. X = x; old. Y = y; } else super. process. Mouse. Motion. Event(e); } cs 884(Prasad) java 12 AWT 26

public void process. Key. Event(Key. Event e) { if ( (e. get. ID() == Key. Event. KEY_TYPED) && (e. get. Key. Char() == 'c') ) { Color temp = g. get. Color(); g. set. Color(get. Background()); g. fill. Rect(0, 0, get. Size(). width, get. Size(). height); g. set. Color(temp); } else super. process. Key. Event(e); } } cs 884(Prasad) java 12 AWT 27
- Slides: 27