2023-09-05 23:50:10 -05:00
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
< html >
< head >
< title > BallControl.java< / title >
< meta http-equiv = "content-type" content = "text/html; charset=UTF-8" >
< style type = "text/css" >
<!--
body {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
pre {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
table {color: #888888; background-color: #313335; font-family: monospace; font-weight: bold}
.literal {color: #cc7832}
.number {color: #6897bb}
.string {color: #6a8759}
.comment {color: #808080}
.whitespace {color: #505050}
-->
< / style >
< / head >
< body >
< table width = "100%" > < tr > < td align = "center" > /home/caleb/ASDV-Java/Semester 3/Assignments/MP2-chapter4_CalebFontenot/src/MP2_chapter4_CalebFontenot/BallControl.java< / td > < / tr > < / table >
< pre >
< span class = "comment" > /*< / span >
2023-09-08 09:17:53 -05:00
< span class = "comment" > * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license< / span >
< span class = "comment" > * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template< / span >
2023-09-05 23:50:10 -05:00
< span class = "comment" > */< / span >
< span class = "literal" > package< / span > MP2_chapter4_CalebFontenot;
< span class = "literal" > import< / span > javafx.application.Application;
< span class = "literal" > import< / span > javafx.event.EventHandler;
< span class = "literal" > import< / span > javafx.scene.Scene;
< span class = "literal" > import< / span > javafx.scene.input.KeyCode;
< span class = "literal" > import< / span > javafx.scene.input.MouseEvent;
< span class = "literal" > import< / span > javafx.stage.Stage;
< span class = "comment" > /**< / span >
< span class = "comment" > *< / span >
< span class = "comment" > * < / span > < span class = "comment" > @author< / span > < span class = "comment" > caleb< / span >
< span class = "comment" > */< / span >
< span class = "literal" > public< / span > < span class = "literal" > class< / span > BallControl < span class = "literal" > extends< / span > Application {
2023-09-08 09:17:53 -05:00
< span class = "literal" > private< / span > < span class = "literal" > boolean< / span > mouseFocus = < span class = "literal" > false< / span > ;
< span class = "literal" > private< / span > < span class = "literal" > int< / span > racketPosition = < span class = "number" > 0< / span > ;
2023-09-05 23:50:10 -05:00
@Override
2023-09-08 09:17:53 -05:00
< span class = "literal" > public< / span > < span class = "literal" > void< / span > start(Stage primaryStage) < span class = "literal" > throws< / span > Exception
{
2023-09-05 23:50:10 -05:00
BouncingBall bouncingBall = < span class = "literal" > new< / span > BouncingBall();
< span class = "comment" > // Create a scene and place it in the stage< / span >
2023-09-08 09:17:53 -05:00
Scene scene = < span class = "literal" > new< / span > Scene(bouncingBall, < span class = "number" > 800< / span > , < span class = "number" > 600< / span > );
2023-09-05 23:50:10 -05:00
primaryStage.setTitle(< span class = "string" > " < / span > < span class = "string" > Bouncing Ball Control< / span > < span class = "string" > " < / span > );
primaryStage.setScene(scene);
primaryStage.show();
bouncingBall.requestFocus();
2023-09-08 09:17:53 -05:00
bouncingBall.showInfoLabel();
2023-09-05 23:50:10 -05:00
bouncingBall.setOnMouseEntered(e
-> {
2023-09-08 09:17:53 -05:00
mouseFocus = < span class = "literal" > true< / span > ;
2023-09-05 23:50:10 -05:00
bouncingBall.hideInfoLabel();
bouncingBall.play();
});
bouncingBall.setOnMouseExited(e -> {
2023-09-08 09:17:53 -05:00
mouseFocus = < span class = "literal" > false< / span > ;
2023-09-05 23:50:10 -05:00
});
bouncingBall.setOnMouseMoved(< span class = "literal" > new< / span > EventHandler< MouseEvent> () {
@Override
2023-09-08 09:17:53 -05:00
< span class = "literal" > public< / span > < span class = "literal" > void< / span > handle(MouseEvent event)
{
< span class = "literal" > double< / span > mouseX = event.getSceneX() - (bouncingBall.RACKET_LENGTH / < span class = "number" > 2< / span > ); < span class = "comment" > // set relative to center of racket< / span >
2023-09-05 23:50:10 -05:00
bouncingBall.moveRacket(mouseX);
}
});
< span class = "comment" > // Increase and decrease animation< / span >
bouncingBall.setOnKeyPressed(e
-> {
2023-09-08 09:17:53 -05:00
< span class = "literal" > if< / span > (e.getCode() == KeyCode.LEFT & & mouseFocus == < span class = "literal" > false< / span > ) {
bouncingBall.hideInfoLabel();
< span class = "literal" > if< / span > (racketPosition > < span class = "number" > 0< / span > ) {
racketPosition = racketPosition - < span class = "number" > 30< / span > ;
}
bouncingBall.moveRacket(racketPosition);
}
< span class = "literal" > if< / span > (e.getCode() == KeyCode.RIGHT & & mouseFocus == < span class = "literal" > false< / span > ) {
bouncingBall.hideInfoLabel();
< span class = "literal" > if< / span > (racketPosition < bouncingBall.getWidth()) {
racketPosition = racketPosition + < span class = "number" > 30< / span > ;
}
bouncingBall.moveRacket(racketPosition);
}
< span class = "literal" > if< / span > (e.getCode() == KeyCode.UP) {
2023-09-05 23:50:10 -05:00
bouncingBall.increaseSpeed();
2023-09-08 09:17:53 -05:00
} < span class = "literal" > else< / span > < span class = "literal" > if< / span > (e.getCode() == KeyCode.DOWN) {
2023-09-05 23:50:10 -05:00
bouncingBall.decreaseSpeed();
}
}
);
}
2023-09-08 09:17:53 -05:00
< span class = "literal" > public< / span > < span class = "literal" > static< / span > < span class = "literal" > void< / span > main(String[] args)
{
launch(args);
2023-09-05 23:50:10 -05:00
}
}
< / pre > < / body >
< / html >