question archive Write a program that creates a GUI displaying an octagon filled in yellow at the centre of a pane of 300x300 that will resize proportionately with the GUI window with a ration of 40% of GUI window

Write a program that creates a GUI displaying an octagon filled in yellow at the centre of a pane of 300x300 that will resize proportionately with the GUI window with a ration of 40% of GUI window

Subject:Computer SciencePrice:3.87 Bought7

Write a program that creates a GUI displaying an octagon filled in yellow at the centre of a pane of 300x300 that will resize proportionately with the GUI window with a ration of 40% of GUI window. The GUI displays a text message (Assignment_5). The text message moves vertically arriving from the middle-top of the screen and disappearing in the middle-bottom circularly. The moving text stops if mouse is pressed and continues its motion when the mouse is released. Text has font type Times New Roman, font size of 20 points and font color blue. The following figure displays a few screenshots of the GUI. Title of GUI display Last Name, First Name, Student ID.

Important: Make sure

1. The class name is Assignment 5

pur-new-sol

Purchase A New Answer

Custom new solution created by our subject matter experts

GET A QUOTE

Answer Preview

Answer:
import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Polygon;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.util.Duration;

public class Assignment_5 extends Application {

  @Override
  public void start(Stage primaryStage) {
    //set the text
    Text text = new Text("Assignment_5");
    //set font
    text.setFont(Font.font("Times New Roman", 20));
    //set color
    text.setFill(Color.BLUE);
    //rotate 90°
    text.setRotate(90);
    //define a group to add the elements
    Group group = new Group();
    //in the node 0 we add the octagon
    group.getChildren().add(0, drawOctagon(300, 300));
    //in the node 1 we add the text
    group.getChildren().add(1, text);
    //set scene
    Scene scene = new Scene(group, 300, 300);

     
    // events
    
    //evt if the windows is width resized
    primaryStage.widthProperty().addListener((obs, oldVal, newVal) -> {
//set the texr at the middle
      text.setX(-50 + scene.getWidth() / 2);
//replace the octagon
      group.getChildren().set(0, drawOctagon(scene.getWidth(), scene.getHeight()));

    });

    //evt if the windows is height resized
    primaryStage.heightProperty().addListener((obs, oldVal, newVal) -> {
      //replace the octagon
      group.getChildren().set(0, drawOctagon(scene.getWidth(), scene.getHeight()));

    });
    // set the animation of the text
    Timeline translateanimation = new Timeline();
    // translate the text from -50 to the scene Height+50 in 5 seconds
    translateanimation.getKeyFrames().addAll(
        new KeyFrame(Duration.ZERO,
            new KeyValue(text.translateYProperty(), -50)),
        new KeyFrame(Duration.seconds(5),
            new KeyValue(text.translateYProperty(), scene.getHeight() + 50)));
    //set infinite cycle
    translateanimation.setCycleCount(Animation.INDEFINITE);
    // play the animation
    translateanimation.play();

    //when the mouse is pressed
    scene.setOnMousePressed((event) -> {
      //pause animation
      translateanimation.pause();

    });
    //when the mouse is released
    scene.setOnMouseReleased((event) -> {
      // play the animation
      translateanimation.play();
    });

    //set the tittle to your convenience the capture is blurred 
    primaryStage.setTitle("LastName");
    primaryStage.setScene(scene);
    primaryStage.show();
  }
// draw the ocotogon

  public Polygon drawOctagon(double w, double h) {

    // coordinates of the points of polygon 
    double xCenter = w / 2;// the center in x
    double yCenter = h / 2;// the center in y

    double radius = ((w + h) / 2) * 0.4;
    // a double array to store x1,y1,x2,y2,x3,y4... coordinates of each point in this case 8 points
    double points[] = new double[16];
    int j = 0;
    for (int i = 0; i < 8; i++) {
      // the octogon have to rotate 8 times with and angle of 45° = 0.25 radians
      double a = i * 0.25 * Math.PI;
      points[j] = xCenter + radius * Math.cos(a); // get the x and store in the array
      points[j + 1] = yCenter + radius * +Math.sin(a);// get the y and store in the array
      j += 2;
    }
    //create the octagon with the points
    Polygon octagon = new Polygon(points);
    //rotate the the polygon to make it equal to the example
    octagon.setRotate(22.5);
    // paint the octagon in yellow
    octagon.setFill(Color.YELLOW);
    return octagon;

  }

  public static void main(String[] args) {
    launch(args);
  }

}

PFA

 

Related Questions