Hoofdstuk 13 Object-georiënteerd ontwerp. Methode-aanroep: Type van parameters void paint(Graphics g) { g.drawRect( ); class Graphics { void drawRect(int.

Slides:



Advertisements
Similar presentations
1 Event Listeners Some Events and Their Associated Event Listeners Act that Results in the EventListener Type User clicks a button, presses Enter while.
Advertisements

1 Graphical User Interface (GUI) Applications Abstract Windowing Toolkit (AWT) Events Handling Applets.
Java GUI building with the AWT. AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Programming in Java; Instructor:John Punin Graphics and Graphical User Interfaces1 Programming in Java Graphics and Graphical User Interfaces.
Event Handling.
Applications of Java to Physics Teaching (Part II) S S Tong Department of Physics CUHK.
Computer Science 209 Graphics and GUIs. Working with Color The class java.awt.Color includes constants for typical color values and also supports the.
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.
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
Lecture 24 Applets. Introduction to Applets Applets should NOT have main method but rather init, stop, paint etc They should be run through javac compiler.
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,
EVENTS CSC 171 FALL 2004 LECTURE 16. “Traditional” Input In console applications, user input is under control of the program The program asks the user.
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/
Lesson 35: Review of the Java GUI. The JFrame, Container and JButton.
Mouse Events. Handling Mouse Events Java provides two listener interfaces to handle mouse events: MouseListener;  MouseListener;  MouseMotionListener.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
More Event Handling Adapters Anonymous Listeners Pop menus Validating User Input.
Cs884(Prasad)java12AWT1 Abstract Windowing Toolkit Support for Graphical User Interface (Event-driven programming)
CS 11 java track: lecture 4 This week: arrays interfaces listener classes inner classes GUI callbacks.
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.
Previous programs used a JLabel for OUTPUT. Another Swing component that can be used for both user input and output is the JTextfield. Suppose we want.
COMP 321 Week 2. Outline Event-Driven Programming Events, Event Sources, Event Listeners Button and Timer Events Mouse Events, Adapters.
Graphics in Java CS 21b. The paint() Method Method in a visual component that specifies what that component looks like Particularly important for applets,
 Definition: An event is an object thrown in response to a user of programmatic action  Definition: A listener is a series of methods that executes in.
Layout Managers Arranges and lays out the GUI components on a container.
For (int i = 1; i
Programming for Interactivity Professor Bill Tomlinson Tuesday & Wednesday 6:00-7:50pm Fall 2005.
Event Handling. 2 GUIs are event driven –Generate events when user interacts with GUI e.g., moving mouse, pressing button, typing in text field, etc.
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.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 10: Event Handling 1 Event Handling.
Kim B. Bruce, Andrea Danyluk & Tom Murtagh Williams College † Partially supported by NSF CCLI grant DUE Java: An Eventful Approach An innovative.
GUI DYNAMICS Lecture 11 CS2110 – Fall GUI Statics and GUI Dynamics  Statics: what’s drawn on the screen  Components buttons, labels, lists, sliders,
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 10: Event Handling 1 Chapter 10 Event Handling.
Events in JAVA Needed for Applets and GUI based programs. Applet uses GUI to interact with user. Most of the events are generated by Mouse, Keyboard.
CS Lecture 04 Mice Lynda Thomas
1 Event Handling – Lecture 4 Prepared by: Ahmad Ramin Rahimee Assistant Professor ICTI.
Laboratory Study November, Demonstrates Life Cycle of an Applet + Mouse Events + Scrolling.
Interfaces Describe what classes should do, without specifying how they should do it Not a class, but a set of requirements for classes that want to conform.
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.
Event Listeners ActionListener –button,list AdjustmentListener-scroll bar ComponentListener-when component hidden…. ContainerListener-comp added or removed.
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.
Event Handling CS 21a: Introduction to Computing I First Semester,
UQC117S2 Graphics Programming Lecture 2 Event Handling Program as a sequence of instructions Event -driven program Need to detect the event and carry out.
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.
Dept. of CSIE, National University of Tainan 10/21/2012 Responding to User Input.
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit7: Event-driven programming 1.
Events and Event Handling
Lecture 09 Applets.
Computer Science 209 Graphics and GUIs.
GUI III IS
Programming in Java Event Handling
Event Handling CS 21a: Introduction to Computing I
GUI Event Handling Nithya Raman.
GUI Programming III: Events
CSE Software Engineering Fall 1999 Updated by J. Brown
Event-driven programming for GUI
ITEC324 Principle of CS III
Objects At Their Best— Java's Abstract Window Toolkit
Programming Graphical User Interface (GUI)
ITEC324 Principle of CS III
Chapter 6 Inheritance.
Presentation transcript:

Hoofdstuk 13 Object-georiënteerd ontwerp

Methode-aanroep: Type van parameters void paint(Graphics g) { g.drawRect( ); class Graphics { void drawRect(int x, int y, int w, int h) {…} void drawString(String s, int x, int y) {…} void setColor(Color c) {…} 10,10,40,50 g.drawRect( t,0,9,9); int t; t=5;for( t<20; t++) 10* g.drawString(“hoi”,5,50); g.setColor(Color.RED); g.setColor(k); Color k; k = new Color( 255,128,0 );

Methode-aanroep: Type van object class Scanner { © Scanner String next () { } boolean hasNext() { } Scanner t; t = new Scanner ( “dit is tekst”, “ ” ); t. hasNext ( ) t. next ( ) while ( ) { } s = ; String s; doeIetsNuttigsMet ( s ); String rest, sep; (String x, String y) { } rest=x; sep=y; ……rest.substring(…)… return ! rest.equals(“”);

Subtypen ook acceptabel class Container { } class Panel extends Container class Applet extends Panel class Button extends Component class Component { } void add(Component c) {…} class Hoi extends Applet { void init( ) { this. add( ); Panel p = new Panel(); p. add( ); Button b = new Button(); Label t = new Label(); b p. add( t ); p void paint(Graphics g) { … } extends Component

Ook acceptabel: class Thread { class Hoi extends Applet { void eenMethode( ) { } © Thread( ) { } void start( ) { } Thread t; t = new Thread( ); t. start( ); a. run( ); Runnable r a = r ; Runnable a; } void run ( ) { while (true) …… } this implements Runnable interface Runnable { void run( ); // geen body! } Implementatie van interface

Ook acceptabel: andere class Thread { class Hoi extends Applet { void eenMethode( ) { } © Thread( ) { } void start( ) { } Thread t; t = new Thread( ); t. start( ); a. run( ); Runnable r a = r ; Runnable a; } void run ( ) { while (true) …… } x interface Runnable { void run( ); // geen body! } Implementatie van interface } class R implements Runnable { x = new R(); Runnable x;

Interface ActionListener: zelf luisteren class Component { class Hoi extends Applet { void init( ) { } void addActionListener (ActionListener a) { …… } } interface ActionListener { void actionPerformed (ActionEvent e ); } Button b; b = new Button(); this.add(b); b. addActionListener(this); void actionPerformed (ActionEvent e ) { …… } implements ActionListener

Interface ActionListener: 2 andere klassen class Hoi extends Applet { } void init( ) { } Button b = new Button(); b. addActionListener( ); TextArea t = new TextArea(); t. addActionListener( ); class AL1 implements ActionListener { } class AL2 implements ActionListener { } void actionPerformed (ActionEvent e) { reageer op de button } void actionPerformed (ActionEvent e) { reageer op het tekstveld } new AL1() new AL2()

Interface ActionListener: 1 andere klasse class Hoi extends Applet { } void init( ) { } Button b = new Button(); b. addActionListener( ); TextArea t = new TextArea(); t. addActionListener( ); class AL implements ActionListener { } © AL (int x) { } new AL (1) new AL (2) void actionPerformed (ActionEvent e) { } int a; a = x; if (a==1) reageer op de button else reageer op het tekstveld

Klasse hierarchieën Component TextArea TextField Panel Window Applet Frame DialogFileDialog Button Canvas Label Scrollbar Text Component Container Event Listener Action Listener Adjustment Listener Component Listener Mouse Listener MouseMotion Listener en interface-hierarchieën

Interface MouseListener: niet 1, maar 5 methoden… interface MouseListener { class ML { void mousePressed(MouseEvent e); void mouseReleased(MouseEvent e); void mouseClicked(MouseEvent e); void mouseEntered(MouseEvent e); void mouseExited(MouseEvent e); } implements MouseListener void mousePressed(MouseEvent e) { doe iets in reactie op indrukken } void mouseReleased(MouseEvent e) {} void mouseClicked(MouseEvent e) {} void mouseEntered(MouseEvent e) {} void mouseExited(MouseEvent e) {} }

Interface MouseListener: niet 1, maar 5 methoden… interface MouseListener { class ML { void mousePressed(MouseEvent e); void mouseReleased(MouseEvent e); void mouseClicked(MouseEvent e); void mouseEntered(MouseEvent e); void mouseExited(MouseEvent e); } extends MouseAdapter void mousePressed(MouseEvent e) { doe iets in reactie op indrukken } } class MouseAdapter implements MouseListener { void mousePressed(MouseEvent e) {} void mouseReleased(MouseEvent e) {} void mouseClicked(MouseEvent e) {} void mouseEntered(MouseEvent e) {} void mouseExited(MouseEvent e) {} } abstract Her definitie de rest hoeft niet gewijzigd!

Zelf maken van abstracte klassen interface MouseMotionListener { class ML { void mouseMoved(MouseEvent e); void mouseDragged(MouseEvent e); } extends MuisBeweger void mouseMoved(MouseEvent e) { doe iets in reactie op bewegen } } class MuisBeweger implements MouseMotionListener { void mouseDragged(MouseEvent e) { this.mouseMoved(e); } } abstract niet compleet, maar deze hebben we alvast! de andere…

Abstracte klasse nOnmogelijk om new objecten van te maken nBedoeld om subklassen van te maken nVaak: onvolledige implementatie van een interface… n…met lege methoden, of elkaar aanroepende methoden

Mouse Adapter MouseMotion Adapter Klasse hierarchieën Component TextArea TextField Panel Window Applet Frame DialogFileDialog Button Canvas Label Scrollbar Text Component Container Event Listener Action Listener Adjustment Listener Component Listener Mouse Listener MouseMotion Listener en interface-hierarchieën Component Text Component Container abstracte klasse in java.awt gemiste kansen in java.awt

Diagram-symbolen Klasse Abstracte Klasse Interface extendsimplements methodeheaders (wensenlijstje) complete methoden en declaraties objectvariabelen gedeeltelijke implementatie

Samenvatting 13.1 nKlasse uGroep methoden uType van een object nInterface u“Wensenlijstje” voor een klasse nAbstracte klasse uGedeeltelijke implementatie uNiet bedoeld voor new, maar voor extends Mouse Listener Runnable Mouse Adapter Container