Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Java program using concepts up to chapter 16 that meets the following re

ID: 3803907 • Letter: W

Question

Write a Java program using concepts up to chapter 16 that meets the following requirements:
From EBOOK 489 “#15.27 Control a Moving Text”, then adapt it to complete “16.1 use radio buttons.”

“#15.27 (Control a Moving Text)”: Write a program that displays a moving text, as shown in Figure 15.35a and b. The text moves from left to right circularly. When it disappears in the right, it reappears from the left. The text freezes when the mouse is pressed and moves again when the button is released.

“16.1 Use radio buttons”: Write a GUI program as shown in Figure 16.36a. You can use buttons to move the message to the left and right and use the radio buttons to change the color for the message displayed.

So, in case it wasn’t clear, the assignment is to write a program that moves the text “Programming is fun” across the screen with radio buttons (as depicted in exercise 16_01), and when it disappears from the right, make it reappear from the left. Also, the color options “Red, Yellow, Black, Orange, Green” need to be available as separate radio buttons at the top of the program.

Exercise15 27 Programming is fun (a) Exercise15 27 Programming is fun (b)

Explanation / Answer

Exercise_16_01.java

/*********************************************************************************
* (Use radio buttons) Write a GUI program as shown in Figure 16.36a. You can use *
* buttons to move the message to the left and right and use the radio buttons to *
* change the color for the message displayed.                                    *
*********************************************************************************/
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.BorderPane;
import javafx.scene.text.Text;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.paint.Color;

public class Exercise_16_01 extends Application {
   protected Text text = new Text(50, 50, "Programming is fun");

   @Override // Override the stage method in the Application class
   public void start(Stage primaryStage) {
       HBox paneForButtons = new HBox(20);
       Button btLeft = new Button("<=");
       Button btRight = new Button("=>");
       paneForButtons.getChildren().addAll(btLeft, btRight);
       paneForButtons.setAlignment(Pos.CENTER);
       BorderPane pane = new BorderPane();
       pane.setBottom(paneForButtons);

       HBox paneForRadioButtons = new HBox(20);
       RadioButton rbRed = new RadioButton("Red");
       RadioButton rbYellow = new RadioButton("Yellow");
       RadioButton rbBlack = new RadioButton("Black");
       RadioButton rbOrange = new RadioButton("Orange");
       RadioButton rbGreen = new RadioButton("Green");
       paneForRadioButtons.getChildren().addAll(rbRed, rbYellow,
           rbBlack, rbOrange, rbGreen);

       ToggleGroup group = new ToggleGroup();
       rbRed.setToggleGroup(group);
       rbYellow.setToggleGroup(group);
       rbBlack.setToggleGroup(group);
       rbOrange.setToggleGroup(group);
       rbGreen.setToggleGroup(group);

       Pane paneForText = new Pane();
       paneForText.setStyle("-fx-border-color: black");
       paneForText.getChildren().add(text);
       pane.setCenter(paneForText);
       pane.setTop(paneForRadioButtons);

       btLeft.setOnAction(e -> text.setX(text.getX() - 10));
       btRight.setOnAction(e -> text.setX(text.getX() + 10));

       rbRed.setOnAction(e -> {
           if (rbRed.isSelected()) {
               text.setFill(Color.RED);
           }
       });

       rbYellow.setOnAction(e -> {
           if (rbYellow.isSelected()) {
               text.setFill(Color.YELLOW);
           }
       });

       rbBlack.setOnAction(e -> {
           if (rbBlack.isSelected()) {
               text.setFill(Color.BLACK);
           }
       });

       rbOrange.setOnAction(e -> {
           if (rbOrange.isSelected()) {
               text.setFill(Color.ORANGE);
           }
       });

       rbGreen.setOnAction(e -> {
           if (rbGreen.isSelected()) {
               text.setFill(Color.GREEN);
           }
       });

       // Create a scene and place it in the stage
       Scene scene = new Scene(pane, 450, 150);
       primaryStage.setTitle("Exercise_16_01"); // Set the stage title
       primaryStage.setScene(scene); // Place the scene in the stage
       primaryStage.show(); // Display the stage
   }
}

Exercise_15_27.java

/*********************************************************************************
* (Control a moving text) Write a program that displays a moving text, as shown *
* in Figure 15.33a and b. The text moves from left to right circularly. When it *
* disappears in the right, it reappears from the left. The text freezes when the *
* mouse is pressed and moves again when the button is released.                  *
*********************************************************************************/
import javafx.application.Application;
import javafx.animation.PathTransition;
import javafx.animation.Timeline;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.text.Text;
import javafx.scene.shape.Line;
import javafx.stage.Stage;
import javafx.util.Duration;

public class Exercise_15_27 extends Application {
   @Override // Override the start method in the Application class
   public void start(Stage primaryStage) {
       // Create a pane
       Pane pane = new Pane();

       // Create a text
       Text text = new Text("Programing is fun");
       pane.getChildren().add(text);

       // Create a path transition
       PathTransition pt = new PathTransition(Duration.millis(10000),
           new Line(-50, 50, 250, 50), text);
       pt.setCycleCount(Timeline.INDEFINITE);
       pt.play(); // Start animation

       // Create and register the handle
       pane.setOnMousePressed(e -> {
           pt.pause();
       });

       pane.setOnMouseReleased(e -> {
           pt.play();
       });

       // Create a scene and place it in the stage
       Scene scene = new Scene(pane, 200, 100);
       primaryStage.setTitle("Exercise_15_27"); // Set the stage title
       primaryStage.setScene(scene); // Place the scene in the stage
       primaryStage.show(); // Display the stage
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote