abstract windowing toolkit

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

Upload: quin-king

Post on 03-Jan-2016

76 views

Category:

Documents


10 download

DESCRIPTION

Abstract Windowing Toolkit. Support for Graphical User Interface (Event-driven programming). Batch Programs -> Interactive Programs => Graphical User Interfaces AWT Classes for creating GUIs; organized as inheritance hierarchy. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Abstract Windowing Toolkit

cs884(Prasad) java12AWT 1

Abstract Windowing Toolkit

Support for Graphical User Interface

(Event-driven programming)

Page 2: Abstract Windowing Toolkit

cs884(Prasad) java12AWT 2

• 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

Page 3: Abstract Windowing Toolkit

cs884(Prasad) java12AWT 3

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.

Page 4: Abstract Windowing Toolkit

cs884(Prasad) java12AWT 4

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(); }

}

Page 5: Abstract Windowing Toolkit

cs884(Prasad) java12AWT 5

{ ...

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();

...}

Page 6: Abstract Windowing Toolkit

cs884(Prasad) java12AWT 6

{ ...

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();

...}

Page 7: Abstract Windowing Toolkit

cs884(Prasad) java12AWT 7

Applet Version

import java.awt.*;

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

Page 8: Abstract Windowing Toolkit

cs884(Prasad) java12AWT 8

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);

}

/* <applet code=DemoAwt2 width=250 height=300> </applet>

*/

Page 9: Abstract Windowing Toolkit

cs884(Prasad) java12AWT 9

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.

Page 10: Abstract Windowing Toolkit

cs884(Prasad) java12AWT 10

• 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

Page 11: Abstract Windowing Toolkit

cs884(Prasad) java12AWT 11

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.)

Page 12: Abstract Windowing Toolkit

cs884(Prasad) java12AWT 12

• 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

source listenerevent

Page 13: Abstract Windowing Toolkit

cs884(Prasad) java12AWT 13

• 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.

Page 14: Abstract Windowing Toolkit

cs884(Prasad) java12AWT 14

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());

}}

Page 15: Abstract Windowing Toolkit

cs884(Prasad) java12AWT 15

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);}

Page 16: Abstract Windowing Toolkit

cs884(Prasad) java12AWT 16

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()); } } );

Page 17: Abstract Windowing Toolkit

cs884(Prasad) java12AWT 17

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);}

Page 18: Abstract Windowing Toolkit

Event ClassListener interface/Source method/ (opt)Adapter class

Listener Methods

ActionEvent ActionListener/addActionListener()

actionPerformed()

AdjustmentEvent AdjustmentListener/addActionListener()

adjustmentValueChanged()

ContainerEvent containerListener/addContainerListener()/ContainerAdapter

componentAdded()componentRemoved()

FocusEvent FocusListener/addFocusListener()/FocusAdapter

focusGained()focusLost()

ItemEvent ItemListener/addItemListener()

itemStateChanged()

Page 19: Abstract Windowing Toolkit

cs884(Prasad) java12AWT 19

ComponentEvent

ComponentListener/addComponentListener()/ComponentAdapter

componentHidden()componentMoved()componentResized()componentShown()

MouseEvent

MouseListener/addMouseListener()/MouseAdapter

MouseMotionListener/addMouseMotionListener()/MouseMotionAdapter

mouseClicked()mouseEntered()mouseExited()mousePressed()mouseReleased()

mouseDragged()mouseMoved()

Page 20: Abstract Windowing Toolkit

cs884(Prasad) java12AWT 20

KeyEvent

KeyListener/ addKeyListener/ KeyAdapter

keyPressed()keyReleased()keyTyped()

TextEvent TextListener/addTextListener()

textValueChanged()

WindowEvent WindowListener/ addWindowListener()

windowActivated()windowClosed()windowClosing()windowDeactivated()windowDeiconified()windowIconified()windowOpened()

Page 21: Abstract Windowing Toolkit

cs884(Prasad) java12AWT 21

Component Events Generated

Button ActionEvent

Checkbox ItemEvent

CheckboxMenuItem ItemEvent

Component ComponentEvent FocusEvent KeyEvent MouseEvent

Container ContainerEvent

Page 22: Abstract Windowing Toolkit

cs884(Prasad) java12AWT 22

List ActionEvent ItemEvent

MenuItem ActionEvent

ScrollBar AdjustmentEvent

TextComponent TextEvent

TextField ActionEvent

Window WindowEvent

Page 23: Abstract Windowing Toolkit

cs884(Prasad) java12AWT 23

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.

Page 24: Abstract Windowing Toolkit

cs884(Prasad) java12AWT 24

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.

Page 25: Abstract Windowing Toolkit

cs884(Prasad) java12AWT 25

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();

}

Page 26: Abstract Windowing Toolkit

cs884(Prasad) java12AWT 26

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);

}

Page 27: Abstract Windowing Toolkit

cs884(Prasad) java12AWT 27

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);

}

}