Answered You can buy a ready-made answer or pick a professional tutor to order an original one.

QUESTION

The following program draws an BB-8 as shown below. Modify the program to move the BB-8 left or right using the arrow keys. The program can be download from the CMS. Please use Javafx   import java

The following program draws an BB-8 as shown below.  Modify the program to move the BB-8 left or right using the arrow keys.  The program can be download from the CMS. Please use Javafx 

import javafx.application.Application;

import javafx.scene.Scene;

import javafx.scene.input.KeyCode;

import javafx.scene.layout.Pane;

import javafx.scene.paint.Color;

import javafx.scene.shape.Circle;

import javafx.scene.shape.Line;

import javafx.stage.Stage;

public class MovingBB8 extends Application {

     @Override // Override the start method in the Application class

public void start(Stage primaryStage) { 

           BB8Pane pane = new BB8Pane();

           pane.setOnKeyPressed(e -> { 

               // Write code here

           });

           // Create  scene and place it in the stage

           Scene scene = new Scene(pane, 400, 120);

           primaryStage.setTitle("MovingBB8");

           primaryStage.setScene(scene);

           primaryStage.show();

           pane.requestFocus();

     }

class BB8Pane extends Pane {

private Circle circle1 = new Circle(200, 50, 15);

private Circle circle2 = new Circle(200, 70, 20);

private Line line = new Line(200, 25, 200, 35);

public BB8Pane() {

           line.setStroke(Color.BLACK);

           circle1.setFill(Color.CORAL);

           circle2.setFill(Color.CORAL);

           getChildren().addAll(circle1, circle2, line);

     }

public void moveLeft() {

           // Write  code here

     }

public void moveRight() {

     // write  code here

     }

}

Show more
ANSWER

import javafx.application.Application;

import javafx.scene.Scene;

import javafx.scene.input.KeyCode;

import javafx.scene.layout.Pane;

import javafx.scene.paint.Color;

import javafx.scene.shape.Circle;

import javafx.scene.shape.Line;

import javafx.stage.Stage;

public class MovingBB8 extends Application {

      @Override // Override the start method in the Application class

      public void start(Stage primaryStage) {

            BB8Pane pane = new BB8Pane();

            pane.setOnKeyPressed(e -> {

                // Write code here

// switch to get the movement action from arrows

switch (e.getCode()) {

case LEFT :

// call method moveLeft

pane.moveLeft();

break;

case RIGHT :

// call method moveRight

pane.moveRight();

break;

}

            });

            // Create  scene and place it in the stage

            Scene scene = new Scene(pane, 400, 120);

            primaryStage.setTitle("MovingBB8");

            primaryStage.setScene(scene);

            primaryStage.show();

            pane.requestFocus();

      }

}

class BB8Pane extends Pane {

      private Circle circle1 = new Circle(200, 50, 15);

      private Circle circle2 = new Circle(200, 70, 20);

      private Line line = new Line(200, 25, 200, 35);

      public BB8Pane() {

            line.setStroke(Color.BLACK);

            circle1.setFill(Color.CORAL);

            circle2.setFill(Color.CORAL);

            getChildren().addAll(circle1, circle2, line);

      }

      public void moveLeft() {

            // Write  code here

            // change the center x and y for circle1 and circle2

 circle1.setCenterX(circle1.getCenterX() > circle1.getRadius() ? circle1.getCenterX() - 10 : circle1.getCenterX());

 circle2.setCenterX(circle2.getCenterX() > circle2.getRadius() ? circle2.getCenterX() - 10 : circle2.getCenterX());

            // change the StartX and EndX for line

line.setStartX(line.getStartX()   -10 );

line.setEndX(line.getEndX() -10)   ;

      }

      public void moveRight() {

            // write  code here

                        // change the center x and y for circle1 and circle2

circle1.setCenterX(circle1.getCenterX() <  getWidth() - circle1.getRadius() ? circle1.getCenterX() + 10: circle1.getCenterX());

circle2.setCenterX(circle2.getCenterX() <  getWidth() - circle2.getRadius() ? circle2.getCenterX() + 10: circle2.getCenterX());

            // change the StartX and EndX for line

line.setStartX(line.getStartX()  +10 );

line.setEndX(line.getEndX() +10)   ;

      }

}

LEARN MORE EFFECTIVELY AND GET BETTER GRADES!
Ask a Question