Chapter 15 Event-Driven Programming and Animations Part 1

Slides:



Advertisements
Similar presentations
Inner Classes. Nested Classes  An nested class is a class that is defined inside another class.  To this point we have only studied top-level classes.
Advertisements

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 Chapter 12 More OOP, Interfaces, and Inner Classes.
1 Chapter 7 Graphics and Event Handling. 2 Overview The java.awt and javax.swing packages and their subpackages support graphics and event handling. Many.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 15 Event-Driven Programming.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 14 Event-Driven Programming.
Event-Driven Programming Event:  A signal to the program that something has happened.  It can be triggered either by external user actions, such as mouse.
Event-Driven Programming
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 16 Event-Driven Programming.
1 Event-Driven Programming Just like designing GUIs, you also will probably not write Java code like the program examples given in these notes. You will.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
ACM/JETT Workshop - August 4-5, ExceptionHandling and User Interfaces (Event Delegation, Inner classes) using Swing.
1 1 Abstract Classes and Interfaces. 22 Motivations You learned how to write simple programs to display GUI components. Can you write the code to respond.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 14 Event-Driven Programming.
Dale Roberts GUI Programming using Java - Event Handling Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about.
Concurrent Programming and Threads Threads Blocking a User Interface.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 12 Event-Driven Programming.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Creating a GUI with JFC/Swing. What are the JFC and Swing? JFC –Java Foundation Classes –a group of features to help people build graphical user interfaces.
Lesson 6 Programming Techniques Event Handling /EvH/ AUBG ICoSCIS Team Assoc. Prof. Stoyan Bonev March, , 2013 SWU, Blagoevgrad.
Object Oriented Programming.  Interface  Event Handling.
Agenda Introduction. Event Model. Creating GUI Application. Event Examples.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 27 JavaBeans and.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Creating User Interfaces Event-Driven Programming.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 15 Event-Driven Programming and.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 15 Event-Driven Programming and.
Event-Driven Programming F Procedural programming is executed in procedural order. F In event-driven programming, code is executed upon activation of events.
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.
1 Chapter 3 Event-Driven Programming. 2 Objectives F To explain the concept of event-driven programming (§12.2). F To understand event, event source,
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Java - hello world example public class HelloWorld { public static void main (String args[]) { System.out.println("Hello World"); }
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Object Oriented Programming in Java Habib Rostami Lecture 10.
Section §15.1 Introduction  Suppose you want to develop a GUI application to calculate loan payments:  The user should be able type the pertinent.
Lecture 8:Event Driven Programming Michael Hsu CSULA.
1 DemoBasic_v3, DemoBasic_v4 JButton JLabel. 2 Registering an ActionListener Register by invoking the following from within constructor DemoBasicFrame.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Chapter 32 JavaBeans and Bean Events
GUI Programming using Java - Event Handling
CSC 205 Programming II Lecture 5 AWT - I.
Events and Event Handling
Chapter 14 Event-Driven Programming
CompSci 230 S Programming Techniques
Lecture 8:Event Driven Programming
Sections Inheritance and Abstract Classes
Chapter 15 Event-Driven Programming and Animations
Chapter 36 JavaBeans and Bean Events
Chapter 12 Event-Driven Programming
Java Events. Java Events Important definitions Overridden method Class vs. abstract class vs. interface Event Handling Definition Main components Windows.
Ellen Walker Hiram College
Nested class.
Event Driven Programming
EE 422C Java FX.
Event Driven Programming and Graphical User Interface
Chapter 15 Event-Driven Programming and Animations
Chapter 15 Event-Driven Programming and Animations
Java Programming Language
Event Driven Systems and Modeling
Chapter 16 Event-Driven Programming
Exception Handling and Event Handling
Events, Event Handlers, and Threads
Constructors, GUI’s(Using Swing) and ActionListner
Inheritance and Polymorphism
Tonga Institute of Higher Education
Chapter 8 Class Inheritance and Interfaces
Chapter 11 Inheritance and Polymorphism Part 1
Chapter 15 Event-Driven Programming and Animations Part 2
Presentation transcript:

Chapter 15 Event-Driven Programming and Animations Part 1

Procedural vs. Event-Driven Programming Procedural programming is executed in procedural order In event-driven programming, code is executed upon activation of events. We can write code to process events such as: Button clicks Mouse movements Keystrokes Clock events

Events and Event Sources An event can be defined as a type of signal to the program that something has happened Event are generated by external user actions such as mouse movements, mouse clicks, or keystrokes. Events can also be generated by a computer’s clock When an event is detected at an event source, an event object is created. This event object is then passed on to an event handler which takes a pre-determined action

Example: Event-Driven Program Displays two buttons and display a message on the console when a button is clicked Extend Application and override start(Stage) Create nodes Create event handlers by implementing EventHandler<> and overriding handle() Register event handlers with event sources (e.g. nodes) using event registration methods Place node in a Parent Place the Parent in the Scene Place the Scene on Stage Show Stage HandleEvent Run

Detecting & Handling GUI Events A source object can detect several types of events Several different event handlers may be registered with a single source object to handle different types of events When an event occurs, an event object with info on the source object and the specifics of the event is created This event object is then handled by the appropriate event handler registered with the source object 5

1. Start from the main method to create a window and display it animation Trace Execution public class HandleEvent extends Application { public void start(Stage primaryStage) { … OKHandlerClass handler1 = new OKHandlerClass(); btOK.setOnAction(handler1); CancelHandlerClass handler2 = new CancelHandlerClass(); btCancel.setOnAction(handler2); primaryStage.show(); // Display the stage } class OKHandlerClass implements EventHandler<ActionEvent> { @Override public void handle(ActionEvent e) { System.out.println("OK button clicked"); 1. Start from the main method to create a window and display it 6

Trace Execution public class HandleEvent extends Application { animation Trace Execution public class HandleEvent extends Application { public void start(Stage primaryStage) { … OKHandlerClass handler1 = new OKHandlerClass(); btOK.setOnAction(handler1); CancelHandlerClass handler2 = new CancelHandlerClass(); btCancel.setOnAction(handler2); primaryStage.show(); // Display the stage } class OKHandlerClass implements EventHandler<ActionEvent> { @Override public void handle(ActionEvent e) { System.out.println("OK button clicked"); 2. Click OK 7

3. The JVM invokes the handler’s handle method animation Trace Execution public class HandleEvent extends Application { public void start(Stage primaryStage) { … OKHandlerClass handler1 = new OKHandlerClass(); btOK.setOnAction(handler1); CancelHandlerClass handler2 = new CancelHandlerClass(); btCancel.setOnAction(handler2); primaryStage.show(); // Display the stage } class OKHandlerClass implements EventHandler<ActionEvent> { @Override public void handle(ActionEvent e) { System.out.println("OK button clicked"); 3. The JVM invokes the handler’s handle method 8

Event Classes

Contents of the Event Object An event object contains whatever properties are pertinent to the event For example, we can identify the source object of the event using getSource() in the EventObject class Subclasses of EventObject deal with special types of events, such as button actions, window events, mouse movements, and keystrokes

Selected User Actions & Handlers

The Delegation Model Java uses a delegation-based model for event handling: a source object fires an event, and a handler (or listener) object interested in the event handles it, i.e. the source object delegates the handling of the event to the handler object

Example: The Delegation Model Button btOK = new Button("OK"); OKHandlerClass handler = new OKHandlerClass(); btOK.setOnAction(handler); //register listener Source Listener

Example: Event Handling We would like to use buttons to control the size of a circle ControlCircleWithoutEventHandling Run ControlCircle Run

Inner Class Listeners A listener (or handler) class is designed specifically to create a listener object for a GUI component (e.g., a button) As it will not be shared by other applications, it is appropriate to define the listener class inside the main class as an inner class

Inner Classes An inner class, or nested class, is a class defined within the scope of another class. In some applications, inner classes make the program easier to understand An inner class can reference the data and methods defined in the outer class in which it nests, so you do not need to pass the reference of the outer class to the constructor of the inner class ShowInnerClass

Example: Inner Classes

Inner Classes (cont.) An inner class supports the work of its outer class and is compiled into a class named OuterClassName$InnerClassName.class Example: inner class B in outer class A is compiles to A$B.class An inner class can be declared subject to the same visibility rules applied to any other member of a class An inner class can be declared static, but it cannot access non-static members of the outer class. A static inner class can be accessed using the outer class name

Anonymous Inner Classes (AIC) They allow us to declare & instantiate a class all at once which makes the code more concise They are like inner classes except that they do not have a name We use them if an inner class needs to be used only once AIC declaration: new SuperClassName/InterfaceName() { // Implement or override methods in superclass or interface // Other methods if necessary } Inner class listeners can be shortened using AICs

Anonymous Inner Classes (cont.) An anonymous inner class must always extend a superclass or implement an interface, but it cannot have an explicit extends or implements clause An anonymous inner class must implement all the abstract methods in the superclass or interface An anonymous inner class always uses the no-arg constructor from its superclass to create an instance. If an anonymous inner class implements an interface, the constructor is Object() An anonymous inner class is compiled into a class named OuterClassName$n.class. For example, if the outer class Test has two anonymous inner classes, these two classes are compiled into Test$1.class and Test$2.class

Example: Anonymous Inner Class AnonymousHandlerDemo Run