I need help understanding key events in JAVAFX. The following should respond to keys pressed. What's wrong? import javafx.application.Application; import javafx.scene.Scene; import javafx.stage.Stage; import javafx.scene.text.Text; import javafx.scene.paint.Color; import javafx.scene.layout.Pane; import javafx.scene.shape.Line; import javafx.scene.shape.Circle; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.animation.Timeline; import javafx.animation.KeyFrame; import javafx.util.Duration; // Exercise 15.30 public class Chap15Liang extends Application {   @Override   public void start(Stage primaryStage){     Pane pane1 = new Pane(); // Pivot, Bob and rod form the pendulum     Circle Pivot = new Circle(500,10,10);     Circle Bob = new Circle(500,700,10);     double radius = 700;     Line rod = new Line(500,10,500,700);     rod.setStrokeWidth(2);     rod.setFill(Color.BLACK);     Pivot.setFill(Color.RED);     pane1.getChildren().addAll(Pivot,Bob,rod);     Text dir = new Text("R"); // Pendulum swings back and forth     EventHandler eventHandler = e-> { // towards the right       if(((dir.getText().equals("R")) && (Bob.getCenterX() < 950)) ||           ((dir.getText().equals("L")) && (Bob.getCenterX()==10))){         Bob.setCenterX(Bob.getCenterX()+10);         Bob.setCenterY(Pivot.getCenterY()             +Math.sqrt(Math.pow(radius,2)             -Math.pow(Bob.getCenterX()-Pivot.getCenterX(),2)));       rod.setEndX(Bob.getCenterX());       rod.setEndY(Bob.getCenterY());       dir.setText("R");}     else { // towards the left       Bob.setCenterX(Bob.getCenterX()-10);       Bob.setCenterY(Pivot.getCenterY()         +Math.sqrt(Math.pow(radius,2)         -Math.pow(Bob.getCenterX()-Pivot.getCenterX(),2)));     rod.setEndX(Bob.getCenterX());     rod.setEndY(Bob.getCenterY());     dir.setText("L");     }   };   KeyFrame penduframe = new KeyFrame(Duration.millis(10),eventHandler);   Timeline animation = new Timeline(penduframe);   animation.setCycleCount(Timeline.INDEFINITE);   animation.play(); // on keys // UP = increase speed; DOWN = decrease speed // S = pause; R = resume   Bob.setOnKeyPressed(e->{     switch (e.getCode()){       case UP: animation.setRate(animation.getRate()+.1);                     break;       case DOWN: animation.setRate(animation.getRate()+.1);                     break;       case S: animation.pause(); break;       case R: animation.play(); break; }   });   Scene scene = new Scene(pane1,1000,1000);   primaryStage.setTitle("");   primaryStage.setScene(scene);   primaryStage.hide();   primaryStage.show();   }   public static void main(String[] args) {     Application.launch(args);   } }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

I need help understanding key events in JAVAFX. The following should respond to keys pressed. What's wrong?

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.text.Text;
import javafx.scene.paint.Color;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Line;
import javafx.scene.shape.Circle;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.animation.Timeline;
import javafx.animation.KeyFrame;
import javafx.util.Duration;

// Exercise 15.30
public class Chap15Liang extends Application {
  @Override
  public void start(Stage primaryStage){
    Pane pane1 = new Pane();
// Pivot, Bob and rod form the pendulum
    Circle Pivot = new Circle(500,10,10);
    Circle Bob = new Circle(500,700,10);
    double radius = 700;
    Line rod = new Line(500,10,500,700);
    rod.setStrokeWidth(2);
    rod.setFill(Color.BLACK);
    Pivot.setFill(Color.RED);
    pane1.getChildren().addAll(Pivot,Bob,rod);
    Text dir = new Text("R");
// Pendulum swings back and forth
    EventHandler<ActionEvent> eventHandler = e-> {
// towards the right
      if(((dir.getText().equals("R")) && (Bob.getCenterX() < 950)) ||
          ((dir.getText().equals("L")) && (Bob.getCenterX()==10))){
        Bob.setCenterX(Bob.getCenterX()+10);
        Bob.setCenterY(Pivot.getCenterY()
            +Math.sqrt(Math.pow(radius,2)
            -Math.pow(Bob.getCenterX()-Pivot.getCenterX(),2)));
      rod.setEndX(Bob.getCenterX());
      rod.setEndY(Bob.getCenterY());
      dir.setText("R");}
    else {
// towards the left
      Bob.setCenterX(Bob.getCenterX()-10);
      Bob.setCenterY(Pivot.getCenterY()
        +Math.sqrt(Math.pow(radius,2)
        -Math.pow(Bob.getCenterX()-Pivot.getCenterX(),2)));
    rod.setEndX(Bob.getCenterX());
    rod.setEndY(Bob.getCenterY());
    dir.setText("L");
    }
  };
  KeyFrame penduframe = new KeyFrame(Duration.millis(10),eventHandler);
  Timeline animation = new Timeline(penduframe);

  animation.setCycleCount(Timeline.INDEFINITE);
  animation.play();
// on keys
// UP = increase speed; DOWN = decrease speed
// S = pause; R = resume
  Bob.setOnKeyPressed(e->{
    switch (e.getCode()){
      case UP: animation.setRate(animation.getRate()+.1);
                    break;
      case DOWN: animation.setRate(animation.getRate()+.1);
                    break;
      case S: animation.pause(); break;
      case R: animation.play(); break; }
  });
  Scene scene = new Scene(pane1,1000,1000);
  primaryStage.setTitle("");
  primaryStage.setScene(scene);
  primaryStage.hide();
  primaryStage.show();
  }
  public static void main(String[] args) {
    Application.launch(args);
  }
}

 

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
JQuery and Javascript
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education