Question: Write an JavaFX application that displays a circle. The application should contain two buttons; larger and smaller. This buttons will increase/decrease the radius of the
Write an JavaFX application that displays a circle. The application should contain two buttons; larger and smaller. This buttons will increase/decrease the radius of the circle. The application should also contain drop down box which allows you to change the color of the circle, this drop down box should contain at least four colors. The application should contain a textbox which will place a label on the circle
import java.util.Random;
import javafx.application.Application; import javafx.collections.FXCollections; import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button; import javafx.scene.control.ComboBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
public class BigBall extends Application {
Circle circle;
Button b1, b2;
Pane pane;
Scene scene;
Stage stage;
public EventHandler
return new EventHandler
@Override
public void handle(ActionEvent arg0) {
double radius;
Random generator = new Random(); //Create random integers or doubles ...etc
pane.getChildren().remove(circle); //Remove previous circle
pane.getChildren().add(circle); //add a new circle
radius = 20; //Radius of the circle is 20 u can change it if want
int X = generator.nextInt(460); //Generate random x location within the scene
int Y = generator.nextInt(260); //Generate random y location within the scene
circle.setCenterX(X);
circle.setCenterY(Y); //Set circle x, y coordinates
circle.setRadius(radius); //set radius
circle.setFill(Color.RED); //set color to fill
}
};
}
@Override
public void start(Stage stage) {
stage.setTitle("Drawing a Circle at random location");
circle = new Circle(); //Create just a empty circle
b1 = new Button("Larger"); //Creates button 1
b1.setOnAction(getActionHandler()); //Add event listener for button1
b2 = new Button("Smaller"); // creates button2
b2.setOnAction(getActionHandler()); //Add event listener for button2
VBox box = new VBox(2);
box.getChildren().addAll(b1,b2); comboBox.setOnAction(e -> { if(comboBox.getValue() == "Red") { circle.setFill(Color.RED); } else if(comboBox.getValue() == "Blue") { circle.setFill(Color.BLUE); } else if(comboBox.getValue() == "Green") { circle.setFill(Color.GREEN); } else if(comboBox.getValue() == "Yellow") { circle.setFill(Color.YELLOW); } });
ComboBox comboBox = new ComboBox(colors);
comboBox.setOnAction(e -> { if(comboBox.equals("Red")) { circle.setFill(Color.RED); } else if(comboBox.equals("Blue")) { circle.setFill(Color.BLUE); } else if(comboBox.equals("Green")) { circle.setFill(Color.GREEN); } else if(comboBox.equals("Yellow")) { circle.setFill(Color.YELLOW); } }); ObservableList
pane = new Pane();
pane.getChildren().addAll(box);
scene = new Scene(pane, 500, 300);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
