Question

Write a JavaFX application that presents a button and a circle. Every time the button is...

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. The main should be in a class and the operation in other class

0 0
Add a comment Improve this question Transcribed image text
✔ Recommended Answer
Answer #1

Circle.java

import javafx.application.Application;

import javafx.event.ActionEvent;

import javafx.event.EventHandler;

import javafx.geometry.Insets;

import javafx.geometry.Pos;

import javafx.scene.Scene;

import javafx.scene.canvas.Canvas;

import javafx.scene.canvas.GraphicsContext;

import javafx.scene.control.Button;

import javafx.scene.layout.BorderPane;

import javafx.scene.layout.HBox;

import javafx.stage.Stage;

public class Circle extends Application {

@Override

public void start(Stage primaryStage) {

try {

Button button = new Button("CLICK ME");

HBox buttonPane = new HBox();

buttonPane.setAlignment(Pos.CENTER);

buttonPane.setPadding(new Insets(10.0));

buttonPane.getChildren().add(button);

Canvas canvas = new Canvas(400, 350);

GraphicsContext gc = canvas.getGraphicsContext2D();

Drawing draw = new Drawing(canvas, gc);

button.setOnAction(new EventHandler<ActionEvent>() {

@Override

public void handle(ActionEvent t) {

draw.drawShapes(gc);

}

});

draw.drawShapes(gc);

BorderPane root = new BorderPane();

root.setBottom(buttonPane);

root.setCenter(canvas);

Scene scene = new Scene(root, 400, 400);

primaryStage.setScene(scene);

primaryStage.show();

} catch (Exception e) {

e.printStackTrace();

}

}

public static void main(String[] args) {

launch(args);

}

}

Drawing.java

import java.util.Random;

import javafx.scene.canvas.Canvas;

import javafx.scene.canvas.GraphicsContext;

import javafx.scene.paint.Color;

public class Drawing {

Canvas canvas;

GraphicsContext gc;

public Drawing(Canvas canvas, GraphicsContext gc) {

super();

this.canvas = canvas;

this.gc = gc;

}

public void drawShapes(GraphicsContext gc) {

Random r = new Random();

gc.setFill(Color.PINK);

gc.setLineWidth(5);

gc.clearRect(0, 0, 400, 350);

gc.fillOval(new Double(r.nextInt(300)), new Double(r.nextInt(300)), 50, 50);

}

}

Add a comment
Know the answer?
Add Answer to:
Write a JavaFX application that presents a button and a circle. Every time the button is...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT