Chapter 15: Event-Driven Programming and...

10
Chapter 15: Event-Driven Programming and Animations DR. JONES KENNESAW STATE UNIVERSITY CS 2302 SUMMER 2015 slides created by Rashaad Jones

Transcript of Chapter 15: Event-Driven Programming and...

Page 1: Chapter 15: Event-Driven Programming and Animationsksuweb.kennesaw.edu/~rjone192/courses/2302/summer2015/...Separate Classes (HIGHLY RECOMMENDED FOR CS2302) •Pros: Code is explicit,

Chapter 15:

Event-Driven

Programming and

AnimationsDR. JONES

KENNESAW STATE UNIVERSITY

CS 2302

SUMMER 2015

slides created by Rashaad Jones

Page 2: Chapter 15: Event-Driven Programming and Animationsksuweb.kennesaw.edu/~rjone192/courses/2302/summer2015/...Separate Classes (HIGHLY RECOMMENDED FOR CS2302) •Pros: Code is explicit,

Overview

Creating Handlers

Registering Handlers

Defining Handlers

Separate class

Inner class

Anonymous

Lambda

Page 3: Chapter 15: Event-Driven Programming and Animationsksuweb.kennesaw.edu/~rjone192/courses/2302/summer2015/...Separate Classes (HIGHLY RECOMMENDED FOR CS2302) •Pros: Code is explicit,

Creating Handlers

// creating different handlers

MyHandlerClass myHandlerObj = new MyHandlerClass();

OKHandlerClass okHandler = new OKHandlerClass();

CancelHandlerClass cancelHandler = new CancelHandlerClass();

Page 4: Chapter 15: Event-Driven Programming and Animationsksuweb.kennesaw.edu/~rjone192/courses/2302/summer2015/...Separate Classes (HIGHLY RECOMMENDED FOR CS2302) •Pros: Code is explicit,

Registering Handlers

//creating different handlers

MyHandlerClass myHandlerObj = new MyHandlerClass();

OKHandlerClass okHandler = new OKHandlerClass();

CancelHandlerClass cancelHandler = new CancelHandlerClass();

//registering handlers

myBtn.setOnAction(myHandlerObj);

okBtn.setOnAction(okHandler);

cancelBtn.setOnAction(cancelHandler);

Page 5: Chapter 15: Event-Driven Programming and Animationsksuweb.kennesaw.edu/~rjone192/courses/2302/summer2015/...Separate Classes (HIGHLY RECOMMENDED FOR CS2302) •Pros: Code is explicit,

Defining Handlers via separate

classes

MyHandlerClass.java

class MyHandlerClass implements EventHandler<ActionEvent>

{

@Override

public void handle(ActionEvent e)

{

//code here will be executed only when event is fired

//example, a button is clicked

System.out.println(“A printout is here only for demonstration purposes”);

}

}

OKHandlerClass.java

class OKHandlerClass implements EventHandler<ActionEvent>

{

@Override

public void handle(ActionEvent e)

{

//code here will be executed only when event is fired

//example, a button is clicked

System.out.println(“OK button is clicked”);

}

}

class CancelHandlerClassimplements EventHandler<ActionEvent>

{

@Override

public void handle(ActionEvent e)

{

//code here will be executed only when event is fired

//example, a button is clicked

System.out.println(“Cancel button is clicked”);

}

}

CancelHandlerClass.java

Page 6: Chapter 15: Event-Driven Programming and Animationsksuweb.kennesaw.edu/~rjone192/courses/2302/summer2015/...Separate Classes (HIGHLY RECOMMENDED FOR CS2302) •Pros: Code is explicit,

Defining Handlers via Inner Classes

public class MyProgram extends Application

{

public void Start(Stage pStage)

{

MyHandlerClass myHandlerObj = new

MyHandlerClass();

Button myBtn = new Button();

myBtn.setOnAction(myHandlerObj);

}

class MyHandlerClass implements

EventHandler<ActionEvent>

{

@Override

public void handle(ActionEvent e)

{

System.out.println(“A printout is here only

for demonstration purposes”);

} //end of handle method

} //end of MyHandlerClass inner class

}

Page 7: Chapter 15: Event-Driven Programming and Animationsksuweb.kennesaw.edu/~rjone192/courses/2302/summer2015/...Separate Classes (HIGHLY RECOMMENDED FOR CS2302) •Pros: Code is explicit,

Defining Handlers via Anonymous Classes

public class MyProgram extends Application

{

public void Start(Stage pStage)

{

MyHandlerClass myHandlerObj = new

MyHandlerClass();

Button myBtn = new Button();

myBtn.setOnAction( new

EventHandler<ActionEvent>()

{

public void handle(ActionEvent e)

{

System.out.println(“A printout is

here only for demonstration purposes”);

} //end of handle method

}); //ends the setOnAction method

} //end of start

} //end of MyProgram

Page 8: Chapter 15: Event-Driven Programming and Animationsksuweb.kennesaw.edu/~rjone192/courses/2302/summer2015/...Separate Classes (HIGHLY RECOMMENDED FOR CS2302) •Pros: Code is explicit,

Defining Handlers via Lambda Expressions

public class MyProgram extends Application

{

public void Start(Stage pStage)

{

MyHandlerClass myHandlerObj = new

MyHandlerClass();

Button myBtn = new Button();

myBtn.setOnAction( e -> {

System.out.println(“A printout is here only for

demonstration purposes”);

}); //ends the setOnAction method

} //end of start

} //end of MyProgram

Page 9: Chapter 15: Event-Driven Programming and Animationsksuweb.kennesaw.edu/~rjone192/courses/2302/summer2015/...Separate Classes (HIGHLY RECOMMENDED FOR CS2302) •Pros: Code is explicit,

(HIGHLY RECOMMENDED FOR CS2302)Separate Classes

•Pros: Code is explicit, simple to implement for beginning programmers

•Cons: Required to write more lines of code (writing more lines is preferred for beginners!)

(Also recommended)Inner Classes

•Cons: For complex UIs, code can become unmanageable

(Not recommended for CS2302)Anonymous Classes

•Pros: Less code is written

•Cons: Need one for each handler

(Not recommended for CS2302)Lambda

•Pros: Useful for Android apps programming in functional programming paradigm (we are in OOP)

•Cons: Code is highly unreadable

Page 10: Chapter 15: Event-Driven Programming and Animationsksuweb.kennesaw.edu/~rjone192/courses/2302/summer2015/...Separate Classes (HIGHLY RECOMMENDED FOR CS2302) •Pros: Code is explicit,

MouseEvent and KeyEvent

Example