Java Class Circle

Actually no, the term of – on click event – is not right because usually said term is only used when handling JavaScript stuff. This time instead of saying on click event, we’re simply going to say – when X button is pressed – which sounds horrible!. Today’s post is going to be a not so easy exercise in which we work with JavaFX and some shapes such as the circle.

The task sounds as simple as the instruction below suggests but it actually is a bit more complex due to having a more deeper discussion in tidying, let’s say, two objects; a button and a circle.

/*
* 
* Write a JavaFX application that presents a button and a circle.
* Every time the button is pushed, the circle should be moved to a new random location within the window.
* 
*/

Not only that but a counter that is going to increment as many times as the circle moves. To make it more clear , I decided to show you the final result of what you’re going to end up with:

Here it is:

//************************************************************************
//  CircleJumper.java       Author: Kevin Uriel Fonseca
//
//  Write a JavaFX application that presents a button and a circle.
//  Every time the button is pushed, the circle should be moved to a new random location within the window.
//************************************************************************
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.layout.FlowPane;
import javafx.scene.text.Text;
import javafx.scene.control.Button;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.Group;
import java.util.Random;

/**
 *
 * @author Kevin Uriel Fonseca
 */
public class CircleJumper extends Application {
    
    // Declare Variables
    private int count = 0;
    private Circle initCircle;
    private Button button;
    private Random randomNumbers;
    private Text countText;
    private FlowPane panel;
    private static final double MAX_X = 600;
    private static final double MAX_Y = 300;
    
    /*
    *
    * Write function here
    *
    */
    @Override
    public void start(Stage primaryStage){

        // Initiate Variables
        count = 0;
        countText = new Text("Pushes: 0");
        initCircle = new Circle(MAX_X /2 , MAX_Y/2, 30);
        initCircle.setFill(Color.WHITE);
        initCircle.setStroke(Color.BLACK);
        initCircle.setStrokeWidth(3);
        button = new Button("Click Me!");
        randomNumbers = new Random();

        button.setOnAction((event) -> {
            count++;
            countText.setText("Pushes: " + count);
            initCircle.setCenterX(randomNumbers.nextInt((int) MAX_X));
            initCircle.setCenterY(randomNumbers.nextInt((int) MAX_Y));
        });
        
        panel = new FlowPane(button, countText);
        
        Group baseDemo = new Group(initCircle, panel);

        Scene scene = new Scene(baseDemo, MAX_X, MAX_Y);
        scene.setFill(Color.WHITE);

        primaryStage.setTitle("Draw a Circle when Button is Pressed");
        primaryStage.setScene(scene);
        primaryStage.setResizable(false);
        primaryStage.show();
        
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        launch(args);
    }
    
}

Remember, most of these mini-programs are class assignments that I need to submit to my school. If you found yourself with the same exercise, please try to understand the code instead of just copying it as your code. Bye-Bye 🙂 .

Please read any of my other snippets by clicking on any of the thumbnails found below, otherwise you can just click over me!.

Leave a Reply

Back to Top