ASDV-Java/Semester 3/Assignments/lab3_CalebFontenot/PrintedHTMLs/BouncingBall.html
2023-09-01 00:14:38 -05:00

109 lines
5.1 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>BouncingBall.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}
.comment {color: #808080}
.whitespace {color: #505050}
-->
</style>
</head>
<body>
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Semester 3/Assignments/lab3_CalebFontenot/src/lab3_calebfontenot/BouncingBall.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> lab3_calebfontenot;
<span class="literal">import</span> javafx.animation.KeyFrame;
<span class="literal">import</span> javafx.animation.Timeline;
<span class="literal">import</span> <span class="literal">static</span> javafx.application.Application.launch;
<span class="literal">import</span> javafx.beans.property.DoubleProperty;
<span class="literal">import</span> javafx.event.ActionEvent;
<span class="literal">import</span> javafx.event.EventHandler;
<span class="literal">import</span> javafx.scene.layout.Pane;
<span class="literal">import</span> javafx.scene.paint.Color;
<span class="literal">import</span> javafx.scene.shape.Circle;
<span class="literal">import</span> javafx.util.Duration;
<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> BouncingBall <span class="literal">extends</span> Pane {
<span class="literal">final</span> <span class="literal">double</span> radius = <span class="number">20</span>;
<span class="literal">private</span> <span class="literal">double</span> x = radius, y = radius;
<span class="literal">private</span> <span class="literal">double</span> dx = <span class="number">1</span>, dy = <span class="number">1</span>;
<span class="literal">private</span> Circle circle = <span class="literal">new</span> Circle(x, y, radius);
<span class="literal">private</span> Timeline animation;
<span class="literal">public</span> BouncingBall()
{
circle.setFill(Color.BROWN); <span class="comment">// Set ball color</span>
getChildren().add(circle); <span class="comment">// Place a ball into this pane</span>
<span class="comment">// Create an animation for moving the ball</span>
animation = <span class="literal">new</span> Timeline(<span class="literal">new</span> KeyFrame(Duration.millis(<span class="number">1</span>), <span class="literal">new</span> EventHandler&lt;ActionEvent&gt;() {
@Override
<span class="literal">public</span> <span class="literal">void</span> handle(ActionEvent t)
{
moveBall();
}
})
);
animation.setRate(animation.getRate() * <span class="number">20</span>);
animation.setCycleCount(Timeline.INDEFINITE);
animation.play();
}
<span class="literal">private</span> <span class="literal">void</span> moveBall() {
<span class="comment">// Check boundaries</span>
<span class="literal">if</span> (x &lt; radius || x &gt; getWidth() - radius) {
dx *= -<span class="number">1</span>; <span class="comment">// Change ball move direction</span>
}
<span class="literal">if</span> (y &lt; radius || y &gt; getHeight() - radius) {
dy *= -<span class="number">1</span>; <span class="comment">// Change ball move direction</span>
}
<span class="comment">// Adjust ball position by 1 or -1</span>
x += dx;
y += dy;
circle.setCenterX(x);
circle.setCenterY(y);
}
<span class="literal">public</span> <span class="literal">void</span> play() {
animation.play();
}
<span class="literal">public</span> <span class="literal">void</span> pause() {
animation.pause();
}
<span class="literal">public</span> <span class="literal">void</span> increaseSpeed() {
animation.setRate(animation.getRate() * <span class="number">1.5</span>);
System.out.println(animation.getRate());
}
<span class="literal">public</span> <span class="literal">void</span> decreaseSpeed() {
animation.setRate(animation.getRate() * <span class="number">1.5</span> &gt; <span class="number">0</span> ? animation.getRate() / <span class="number">1.5</span> : <span class="number">0</span>);
System.out.println(animation.getRate());
}
<span class="literal">public</span> DoubleProperty rateProperty() {
<span class="literal">return</span> animation.rateProperty();
}
}
</pre></body>
</html>