Cse 114, Computer Science I Stony Brook University Event Programming in Javafx
Cse 114, Computer Science I Stony Brook University Event Programming in Javafx
http://www.cs.stonybrook.edu/~cse114
Event Programming
The OS:
sorts out these events
reports them to the appropriate programs
2
(c) Paul Fodor and Pearson Inc.
Event Handler?
code with response to event
a.k.a. event listener
3
(c) Paul Fodor and Pearson Inc.
http://docs.oracle.com/javase/8/javafx/user-interface-tutorial/ui_controls.htm
4
(c) Paul Fodor and Pearson Inc.
5
(c) Paul Fodor and Pearson Inc.
6
(c) Paul Fodor and Pearson Inc.
Event Objects
Contain information about the event
Like what?
location of mouse click
event source that was interacted with
etc.
7
(c) Paul Fodor and Pearson Inc.
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.scene.control.Button;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
btOK.setOnAction(handler1);
btCancel.setOnAction(handler2);
pane.getChildren().addAll(btOK, btCancel);
primaryStage.setScene(scene); primaryStage.show();
}…/*main*/}
@Override
}}
@Override
public void handle(ActionEvent e) {
}}
8
(c) Paul Fodor and Pearson Inc.
9
(c) Paul Fodor and Pearson Inc.
Event Classes
10
(c) Paul Fodor and Pearson Inc.
Event Information
An event object contains whatever properties are
pertinent to the event:
the source object of the event using the getSource()
instance method in the EventObject class.
11
(c) Paul Fodor and Pearson Inc.
13
(c) Paul Fodor and Pearson Inc.
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.control.Button;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.shape.Circle;
@Override
hBox.getChildren().add(btEnlarge);
hBox.getChildren().add(btShrink);
btEnlarge.setOnAction(new EnlargeHandler());
borderPane.setCenter(circlePane);
borderPane.setBottom(hBox);
BorderPane.setAlignment(hBox, Pos.CENTER);
primaryStage.setScene(scene); primaryStage.show();
}
ControlCircle program that uses two buttons to control the size of a circle
14
(c) Paul Fodor and Pearson Inc.
ControlCircle program that uses two buttons to control the size of a circle
// Inner Class
class EnlargeHandler
implements EventHandler<ActionEvent> {
@Override
circlePane.enlarge();
public CirclePane() {
getChildren().add(circle);
circle.setStroke(Color.BLACK);
circle.setFill(Color.WHITE);
circle.setRadius(circle.getRadius() + 2);
circle.setRadius(circle.getRadius() > 2
? circle.getRadius() - 2 : circle.getRadius());
15
(c) Paul Fodor and Pearson Inc.
16
(c) Paul Fodor and Pearson Inc.
Inner Classes
17
OuterClass(){
y.m2();
data++;
System.out.println(x.data);
class InnerClass {
m1();
}
(c) Paul Fodor and Pearson Inc.
Inner Classes
18
(c) Paul Fodor and Pearson Inc.
new SuperClassName/InterfaceName() {
19
(c) Paul Fodor and Pearson Inc.
20
(c) Paul Fodor and Pearson Inc.
21
(c) Paul Fodor and Pearson Inc.
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.scene.control.Button;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
hBox.getChildren().addAll(btNew, btOpen);
btNew.setOnAction(new EventHandler<ActionEvent>() {
System.out.println("Process New");
});
btOpen.setOnAction(new EventHandler<ActionEvent>() {
System.out.println("Process Open");
});
22
(c) Paul Fodor and Pearson Inc.
primaryStage.setTitle("AnonymousHandlerDemo");
primaryStage.setScene(scene);
primaryStage.show();
launch(args);
23
(c) Paul Fodor and Pearson Inc.
btEnlarge.setOnAction(
new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
// Code for processing event e
}
}
});
(a) Anonymous inner class event handler
btEnlarge.setOnAction(e -> {
// Code for processing event e
});
(b) Lambda expression event handler
24
(c) Paul Fodor and Pearson Inc.
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.scene.control.Button;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
@Override
hBox.setSpacing(10);
hBox.setAlignment(Pos.CENTER);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args); }}
run-single:
Process New
Process Open
Process Save
Process Print
25
(c) Paul Fodor and Pearson Inc.
or
26
(c) Paul Fodor and Pearson Inc.
27
(c) Paul Fodor and Pearson Inc.
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.GridPane;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.geometry.Pos;
import javafx.geometry.HPos;
@Override
// Create UI
gridPane.setHgap(5);
gridPane.setVgap(5);
gridPane.add(tfAnnualInterestRate, 1, 0);
gridPane.add(tfNumberOfYears, 1, 1);
gridPane.add(tfLoanAmount, 1, 2);
gridPane.add(new Label("Monthly Payment:"), 0, 3);
gridPane.add(tfMonthlyPayment, 1, 3);
gridPane.add(tfTotalPayment, 1, 4);
gridPane.add(btCalculate, 1, 5);
Loan Calculator
28
(c) Paul Fodor and Pearson Inc.
MouseEvent
31
(c) Paul Fodor and Pearson Inc.
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.text.Text;
public class KeyEventDemo extends Application {
@Override
public void start(Stage primaryStage) {
Pane pane = new Pane();
Text text = new Text(20, 20, "A");
text.setFocusTraversable(true);
pane.getChildren().add(text);
text.setOnKeyPressed(e -> {
switch (e.getCode()) {
case KeyCode.DOWN: text.setY(text.getY() + 10); break;
case KeyCode.UP: text.setY(text.getY() - 10); break;
case KeyCode.LEFT: text.setX(text.getX() - 10); break;
case KeyCode.RIGHT: text.setX(text.getX() + 10); break;
default:
if (Character.isLetterOrDigit(e.getText().charAt(0)))
text.setText(e.getText());
}
});
Scene scene = new Scene(pane);
primaryStage.setTitle("KeyEventDemo");
primaryStage.setScene(scene);
primaryStage.show();
}
34
(c) Paul Fodor and Pearson Inc.
35
(c) Paul Fodor and Pearson Inc.
37
(c) Paul Fodor and Pearson Inc.
import javafx.beans.Observable;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
balance.addListener(new InvalidationListener() {
balance.doubleValue());
});
balance.set(4.5);
Output:
38
(c) Paul Fodor and Pearson Inc.
Animation
39
(c) Paul Fodor and Pearson Inc.
PathTransition
40
(c) Paul Fodor and Pearson Inc.
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.Circle;
import javafx.animation.PathTransition;
import javafx.animation.Timeline;
import javafx.util.Duration;
public class PathTransitionDemo extends Application {
@Override
public void start(Stage primaryStage) {
Pane pane = new Pane();
Rectangle rectangle = new Rectangle(0, 0, 25, 50);
rectangle.setFill(Color.ORANGE);
Circle circle = new Circle(125, 100, 50);
circle.setFill(Color.WHITE);
circle.setStroke(Color.BLACK);
pane.getChildren().addAll(circle,rectangle);
// Create a path transition
PathTransition pt = new PathTransition();
pt.setDuration(Duration.millis(4000));
pt.setPath(circle);
pt.setNode(rectangle); 41
(c) Paul Fodor and Pearson Inc.
pt.setOrientation(
PathTransition.OrientationType.
ORTHOGONAL_TO_TANGENT);
pt.setCycleCount(Timeline.INDEFINITE);
pt.setAutoReverse(true);
pt.play(); // Start animation
circle.setOnMousePressed(e -> pt.pause());
circle.setOnMouseReleased(e -> pt.play());
Scene scene = new Scene(pane, 250, 200);
primaryStage.setTitle("PathTransitionDemo");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void
main(String[] args){
launch(args);
}
42
(c) Paul Fodor and Pearson Inc.
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Line;
import javafx.animation.PathTransition;
import javafx.scene.image.ImageView;
import javafx.util.Duration;
@Override
pane.getChildren().add(imageView);
Duration.millis(10000),
imageView);
pt.setCycleCount(5);
primaryStage.setScene(scene); primaryStage.show();
43
(c) Paul Fodor and Pearson Inc.
FadeTransition
44
(c) Paul Fodor and Pearson Inc.
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Ellipse;
import javafx.animation.FadeTransition;
import javafx.animation.Timeline;
import javafx.util.Duration;
@Override
ellipse.setFill(Color.RED);
ellipse.setStroke(Color.BLACK);
ellipse.centerXProperty().bind(pane.widthProperty().divide(2));
ellipse.centerYProperty().bind(pane.heightProperty().divide(2));
ellipse.radiusXProperty().bind(pane.widthProperty().multiply(0.4));
ellipse.radiusYProperty().bind(pane.heightProperty().multiply(0.4));
pane.getChildren().add(ellipse);
ft.setFromValue(1.0);
ft.setToValue(0.1);
ft.setCycleCount(Timeline.INDEFINITE);
ft.setAutoReverse(true);
45
(c) Paul Fodor and Pearson Inc.
Timeline
46
(c) Paul Fodor and Pearson Inc.
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.util.Duration;
@Override
text.setFill(Color.RED);
pane.getChildren().add(text);
EventHandler<ActionEvent> eH = e -> {
if (text.getText().length() != 0) {
text.setText("");
} else {
text.setText("Programming is fun");
};
// Start animation
animation.play();
47
(c) Paul Fodor and Pearson Inc.
text.setOnMouseClicked(e -> {
if (animation.getStatus() ==
Animation.Status.PAUSED) {
animation.play();
} else {
animation.pause();
});
primaryStage.setTitle("TimelineDemo");
primaryStage.setScene(scene);
primaryStage.show();
48
(c) Paul Fodor and Pearson Inc.
Clock Animation
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.util.Duration;
@Override
};
animation.setCycleCount(Timeline.INDEFINITE);
primaryStage.setTitle("ClockAnimation");
primaryStage.setScene(scene);
primaryStage.show();
49
(c) Paul Fodor and Pearson Inc.
// ClockPane:
import java.util.Calendar;
import java.util.GregorianCalendar;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.scene.text.Text;
public ClockPane() {
setCurrentTime();
this.hour = hour;
this.minute = minute;
this.second = second;
paintClock();
return hour;
paintClock();
50
(c) Paul Fodor and Pearson Inc.
return minute;
this.minute = minute;
paintClock();
return second;
this.second = second;
paintClock();
return w;
this.w = w;
paintClock();
return h;
this.h = h;
paintClock();
}
public void setCurrentTime() {
this.hour = calendar.get(Calendar.HOUR_OF_DAY);
this.minute = calendar.get(Calendar.MINUTE);
this.second = calendar.get(Calendar.SECOND);
51
(c) Paul Fodor and Pearson Inc.
52
double centerX = w / 2;
double centerY = h / 2;
// Draw circle
circle.setFill(Color.WHITE);
circle.setStroke(Color.BLACK);
sLine.setStroke(Color.RED);
mLine.setStroke(Color.BLUE);
hLine.setStroke(Color.GREEN);
getChildren().clear();
}
(c) Paul Fodor and Pearson Inc.
Bouncing Ball
53
(c) Paul Fodor and Pearson Inc.
import javafx.scene.layout.Pane;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.beans.property.DoubleProperty;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.util.Duration;
private double dx = 1, dy = 1;
public BallPane() {
animation.setCycleCount(Timeline.INDEFINITE);
animation.play();
animation.pause();
animation.setRate(
54
(c) Paul Fodor and Pearson Inc.
return animation.rateProperty();
// Check boundaries
x += dx;
y += dy;
circle.setCenterX(x);
circle.setCenterY(y);
55
(c) Paul Fodor and Pearson Inc.
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.input.KeyCode;
@Override
ballPane.setOnKeyPressed(e -> {
if (e.getCode() == KeyCode.UP) {
ballPane.increaseSpeed();
ballPane.decreaseSpeed();
});
primaryStage.setTitle("BounceBallControl");
primaryStage.setScene(scene);
primaryStage.show();
ballPane.requestFocus();
56
(c) Paul Fodor and Pearson Inc.
javafx.scene.input.SwipeEvent,
javafx.scene.input.TouchEvent,
javafx.scene.input.ZoomEvent.
Example:
http://docs.oracle.com/javase/8/javafx/events-tutorial/gestureeventsjava.htm
http://docs.oracle.com/javase/8/javafx/events-tutorial/toucheventsjava.htm
57