BouncingBall
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
|
||||
*/
|
||||
package lab3_calebfontenot;
|
||||
|
||||
import javafx.application.Application;
|
||||
import javafx.event.EventHandler;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.scene.input.MouseEvent;
|
||||
import javafx.stage.Stage;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author caleb
|
||||
*/
|
||||
public class BallControl extends Application {
|
||||
|
||||
@Override
|
||||
public void start(Stage primaryStage) throws Exception
|
||||
{
|
||||
BouncingBall bouncingBall = new BouncingBall();
|
||||
// Create a scene and place it in the stage
|
||||
Scene scene = new Scene(bouncingBall, 800, 600);
|
||||
primaryStage.setTitle("Bouncing Ball Control");
|
||||
primaryStage.setScene(scene);
|
||||
primaryStage.show();
|
||||
bouncingBall.requestFocus();
|
||||
|
||||
bouncingBall.setOnMousePressed(new EventHandler<MouseEvent>() {
|
||||
@Override
|
||||
public void handle(MouseEvent event)
|
||||
{
|
||||
bouncingBall.pause();
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
bouncingBall.setOnMouseReleased(e ->
|
||||
{
|
||||
bouncingBall.play();
|
||||
});
|
||||
}
|
||||
// Increase and decrease animation
|
||||
bouncingBall.setOnKeyPressed(e ->
|
||||
{
|
||||
if (e.getCode() == KeyCode.UP) {
|
||||
bouncingBall.increaseSpeed();
|
||||
} else if (e.getCode() == KeyCode.DOWN) {
|
||||
bouncingBall.decreaseSpeed();
|
||||
}
|
||||
});
|
||||
public static void main(String[] args)
|
||||
{
|
||||
launch(args);
|
||||
}
|
||||
}
|
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
|
||||
*/
|
||||
package lab3_calebfontenot;
|
||||
|
||||
import javafx.animation.KeyFrame;
|
||||
import javafx.animation.Timeline;
|
||||
import static javafx.application.Application.launch;
|
||||
import javafx.beans.property.DoubleProperty;
|
||||
import javafx.event.ActionEvent;
|
||||
import javafx.event.EventHandler;
|
||||
import javafx.scene.layout.Pane;
|
||||
import javafx.scene.paint.Color;
|
||||
import javafx.scene.shape.Circle;
|
||||
import javafx.util.Duration;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author caleb
|
||||
*/
|
||||
public class BouncingBall extends Pane {
|
||||
|
||||
final double radius = 20;
|
||||
private double x = radius, y = radius;
|
||||
private double dx = 1, dy = 1;
|
||||
private Circle circle = new Circle(x, y, radius);
|
||||
private Timeline animation;
|
||||
|
||||
public BouncingBall()
|
||||
{
|
||||
circle.setFill(Color.BROWN); // Set ball color
|
||||
getChildren().add(circle); // Place a ball into this pane
|
||||
|
||||
// Create an animation for moving the ball
|
||||
animation = new Timeline(new KeyFrame(Duration.millis(1), new EventHandler<ActionEvent>() {
|
||||
@Override
|
||||
public void handle(ActionEvent t)
|
||||
{
|
||||
moveBall();
|
||||
}
|
||||
})
|
||||
);
|
||||
animation.setRate(animation.getRate() * 20);
|
||||
animation.setCycleCount(Timeline.INDEFINITE);
|
||||
animation.play();
|
||||
}
|
||||
|
||||
private void moveBall() {
|
||||
// Check boundaries
|
||||
if (x < radius || x > getWidth() - radius) {
|
||||
dx *= -1; // Change ball move direction
|
||||
}
|
||||
if (y < radius || y > getHeight() - radius) {
|
||||
dy *= -1; // Change ball move direction
|
||||
}
|
||||
|
||||
// Adjust ball position by 1 or -1
|
||||
x += dx;
|
||||
y += dy;
|
||||
circle.setCenterX(x);
|
||||
circle.setCenterY(y);
|
||||
}
|
||||
public void play() {
|
||||
animation.play();
|
||||
}
|
||||
|
||||
public void pause() {
|
||||
animation.pause();
|
||||
}
|
||||
|
||||
public void increaseSpeed() {
|
||||
animation.setRate(animation.getRate() * 1.5);
|
||||
}
|
||||
|
||||
public void decreaseSpeed() {
|
||||
animation.setRate(animation.getRate() * 1.5 > 0 ? animation.getRate() / 1.5 : 0);
|
||||
}
|
||||
|
||||
public DoubleProperty rateProperty() {
|
||||
return animation.rateProperty();
|
||||
}
|
||||
}
|
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/javafx/FXMain.java to edit this template
|
||||
*/
|
||||
package lab3_calebfontenot;
|
||||
|
||||
import javafx.application.Application;
|
||||
import javafx.event.ActionEvent;
|
||||
import javafx.event.EventHandler;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.scene.control.Button;
|
||||
import javafx.scene.layout.StackPane;
|
||||
import javafx.stage.Stage;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author caleb
|
||||
*/
|
||||
public class Lab3_CalebFontenot extends Application {
|
||||
|
||||
@Override
|
||||
public void start(Stage primaryStage)
|
||||
{
|
||||
Button btn = new Button();
|
||||
btn.setText("Say 'Hello World'");
|
||||
btn.setOnAction(new EventHandler<ActionEvent>() {
|
||||
|
||||
@Override
|
||||
public void handle(ActionEvent event)
|
||||
{
|
||||
System.out.println("Hello World!");
|
||||
}
|
||||
});
|
||||
|
||||
StackPane root = new StackPane();
|
||||
root.getChildren().add(btn);
|
||||
|
||||
Scene scene = new Scene(root, 300, 250);
|
||||
|
||||
primaryStage.setTitle("Hello World!");
|
||||
primaryStage.setScene(scene);
|
||||
primaryStage.show();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param args the command line arguments
|
||||
*/
|
||||
public static void main(String[] args)
|
||||
{
|
||||
launch(args);
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user