Write a program that displays a string “Welcome to Java” around the circle, as s
ID: 3685188 • Letter: W
Question
Write a program that displays a string “Welcome to Java” around the circle, as shown below. You need to display each character in the right location with appropriate rotation using a loop.
Directions
Create a class named Characters extends Application.
Create a new pane.
Create an object of Font class and define font features as bold, Times New Roman, regular and size of 35.
Create a string of “Welcome to Java”
Create a for loop to scan through each character of “Welcome to Java” string.
Inside the for loop, create an instance of Text on the given coordinates containing the given characters by use constructor of Text(double x, double y, java.lang.String text)
Use setFont( ) and setRotate( ) methods of Text class to define characters and their rotated positions
Add text instance to the pane by using getChildren().add() method
Create a scene with a specific size
Set title to “Characters around circle” and display the stage
The output should look like the screen below.
Provide appropriate Java comments
Explanation / Answer
package welcome;
import javafx.application.Application;
import javafx.geometry.Point2D;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class cirjava extends Application{
@Override
public void start(Stage primaryStage) {
Pane pane = new Pane();
String[] java = "Welcome to Java".split("");
Font font = Font.font("Times New Roman", FontWeight.EXTRA_BOLD, 35);
// x = a+r * cos t
// y = b+r * sin t
// where t is a parametric variable in the range 0 to 2,
Point2D center = new Point2D(200, 200);
double radius = 100;
double angle = 0;
double rotate = 90;
for (int i = 0; i < java.length; i++, angle += 22, rotate += 22) {
double x = center.getX() + radius * Math.cos(Math.toRadians(angle));
double y = center.getY() + radius * Math.sin(Math.toRadians(angle));
Text text = new Text(x, y,java[i]);
text.setRotate(rotate);
text.setFont(font);
pane.getChildren().add(text);
}
Scene scene = new Scene(pane, 400, 400);
primaryStage.setTitle("Welcome to Java");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.