112 lines
5.3 KiB
HTML
112 lines
5.3 KiB
HTML
<!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>
|
|
<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>
|
|
<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 {
|
|
|
|
<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>;
|
|
|
|
@Override
|
|
<span class="literal">public</span> <span class="literal">void</span> start(Stage primaryStage) <span class="literal">throws</span> Exception
|
|
{
|
|
|
|
BouncingBall bouncingBall = <span class="literal">new</span> BouncingBall();
|
|
<span class="comment">// Create a scene and place it in the stage</span>
|
|
Scene scene = <span class="literal">new</span> Scene(bouncingBall, <span class="number">800</span>, <span class="number">600</span>);
|
|
primaryStage.setTitle(<span class="string">"</span><span class="string">Bouncing Ball Control</span><span class="string">"</span>);
|
|
primaryStage.setScene(scene);
|
|
primaryStage.show();
|
|
bouncingBall.requestFocus();
|
|
bouncingBall.showInfoLabel();
|
|
|
|
bouncingBall.setOnMouseEntered(e
|
|
-> {
|
|
mouseFocus = <span class="literal">true</span>;
|
|
bouncingBall.hideInfoLabel();
|
|
bouncingBall.play();
|
|
});
|
|
|
|
bouncingBall.setOnMouseExited(e -> {
|
|
mouseFocus = <span class="literal">false</span>;
|
|
});
|
|
bouncingBall.setOnMouseMoved(<span class="literal">new</span> EventHandler<MouseEvent>() {
|
|
@Override
|
|
<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>
|
|
bouncingBall.moveRacket(mouseX);
|
|
}
|
|
|
|
});
|
|
|
|
<span class="comment">// Increase and decrease animation</span>
|
|
bouncingBall.setOnKeyPressed(e
|
|
-> {
|
|
<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) {
|
|
bouncingBall.increaseSpeed();
|
|
} <span class="literal">else</span> <span class="literal">if</span> (e.getCode() == KeyCode.DOWN) {
|
|
bouncingBall.decreaseSpeed();
|
|
}
|
|
|
|
}
|
|
);
|
|
}
|
|
|
|
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> main(String[] args)
|
|
{
|
|
launch(args);
|
|
}
|
|
}
|
|
|
|
</pre></body>
|
|
</html>
|