/home/caleb/ASDV-Java/Semester 3/Assignments/MP2-chapter4_CalebFontenot/src/MP2_chapter4_CalebFontenot/BallControl.java |
package MP2_chapter4_CalebFontenot;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.input.KeyCode;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;
public class BallControl extends Application {
private boolean mouseFocus = false;
private int racketPosition = 0;
@Override
public void start(Stage primaryStage) throws Exception
{
BouncingBall bouncingBall = new BouncingBall();
Scene scene = new Scene(bouncingBall, 800, 600);
primaryStage.setTitle("Bouncing Ball Control");
primaryStage.setScene(scene);
primaryStage.show();
bouncingBall.requestFocus();
bouncingBall.showInfoLabel();
bouncingBall.setOnMouseEntered(e
-> {
mouseFocus = true;
bouncingBall.hideInfoLabel();
bouncingBall.play();
});
bouncingBall.setOnMouseExited(e -> {
mouseFocus = false;
});
bouncingBall.setOnMouseMoved(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event)
{
double mouseX = event.getSceneX() - (bouncingBall.RACKET_LENGTH / 2);
bouncingBall.moveRacket(mouseX);
}
});
bouncingBall.setOnKeyPressed(e
-> {
if (e.getCode() == KeyCode.LEFT && mouseFocus == false) {
bouncingBall.hideInfoLabel();
if (racketPosition > 0) {
racketPosition = racketPosition - 30;
}
bouncingBall.moveRacket(racketPosition);
}
if (e.getCode() == KeyCode.RIGHT && mouseFocus == false) {
bouncingBall.hideInfoLabel();
if (racketPosition < bouncingBall.getWidth()) {
racketPosition = racketPosition + 30;
}
bouncingBall.moveRacket(racketPosition);
}
if (e.getCode() == KeyCode.UP) {
bouncingBall.increaseSpeed();
} else if (e.getCode() == KeyCode.DOWN) {
bouncingBall.decreaseSpeed();
}
}
);
}
public static void main(String[] args)
{
launch(args);
}
}