210 lines
11 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>MultipleBallsWithComparator.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}
.string {color: #6a8759}
.number {color: #6897bb}
.whitespace {color: #505050}
.comment {color: #808080}
.literal {color: #cc7832}
-->
</style>
</head>
<body>
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Semester 3/Assignments/JavaFXBallsWithComparator/src/main/java/edu/slcc/asdv/caleb/javafxballswithcomparator/MultipleBallsWithComparator.java</td></tr></table>
<pre>
<span class="literal">package</span> edu.slcc.asdv.caleb.javafxballswithcomparator;
<span class="literal">import</span> javafx.animation.KeyFrame;
<span class="literal">import</span> javafx.animation.Timeline;
<span class="literal">import</span> javafx.application.Application;
<span class="literal">import</span> javafx.beans.property.DoubleProperty;
<span class="literal">import</span> javafx.geometry.Pos;
<span class="literal">import</span> javafx.scene.Node;
<span class="literal">import</span> javafx.stage.Stage;
<span class="literal">import</span> javafx.scene.Scene;
<span class="literal">import</span> javafx.scene.control.Button;
<span class="literal">import</span> javafx.scene.control.ScrollBar;
<span class="literal">import</span> javafx.scene.layout.BorderPane;
<span class="literal">import</span> javafx.scene.layout.HBox;
<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="literal">public</span> <span class="literal">class</span> MultipleBallsWithComparator <span class="literal">extends</span> Application
{
@Override <span class="comment">// Override the start method in the Application class</span>
<span class="literal">public</span> <span class="literal">void</span> start(Stage primaryStage)
{
MultipleBallPane ballPane = <span class="literal">new</span> MultipleBallPane();
Button btAdd = <span class="literal">new</span> Button(<span class="string">&quot;</span><span class="string">+</span><span class="string">&quot;</span>);
Button btSubtract = <span class="literal">new</span> Button(<span class="string">&quot;</span><span class="string">-</span><span class="string">&quot;</span>);
HBox hBox = <span class="literal">new</span> HBox(<span class="number">10</span>);
hBox.getChildren().addAll(btAdd, btSubtract);
hBox.setAlignment(Pos.CENTER);
<span class="comment">// Add or remove a ball</span>
btAdd.setOnAction(e -&gt; ballPane.add());
btSubtract.setOnAction(e -&gt; ballPane.subtract());
<span class="comment">// Pause and resume animation</span>
ballPane.setOnMousePressed(e -&gt; ballPane.pause());
ballPane.setOnMouseReleased(e -&gt; ballPane.play());
<span class="comment">// Use a scroll bar to control animation speed</span>
ScrollBar sbSpeed = <span class="literal">new</span> ScrollBar();
sbSpeed.setMax(<span class="number">20</span>);
sbSpeed.setValue(<span class="number">10</span>);
ballPane.rateProperty().bind(sbSpeed.valueProperty());
BorderPane pane = <span class="literal">new</span> BorderPane();
pane.setCenter(ballPane);
pane.setTop(sbSpeed);
pane.setBottom(hBox);
<span class="comment">// Create a scene and place the pane in the stage</span>
Scene scene = <span class="literal">new</span> Scene(pane, <span class="number">250</span>, <span class="number">150</span>);
primaryStage.setTitle(<span class="string">&quot;</span><span class="string">Multiple Bouncing Balls</span><span class="string">&quot;</span>); <span class="comment">// Set the stage title</span>
primaryStage.setScene(scene); <span class="comment">// Place the scene in the stage</span>
primaryStage.show(); <span class="comment">// Display the stage</span>
}
<span class="literal">private</span> <span class="literal">class</span> MultipleBallPane <span class="literal">extends</span> Pane
{
<span class="literal">private</span> Timeline animation;
<span class="literal">public</span> MultipleBallPane()
{
<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">50</span>), e -&gt; moveBall()));
animation.setCycleCount(Timeline.INDEFINITE);
animation.play(); <span class="comment">// Start animation</span>
}
<span class="literal">public</span> <span class="literal">void</span> add()
{
Color color = <span class="literal">new</span> Color(Math.random(),
Math.random(), Math.random(), <span class="number">0.5</span>);
getChildren().add(<span class="literal">new</span> Ball(<span class="number">30</span>, <span class="number">30</span>, Math.random() * <span class="number">16</span> + <span class="number">5</span>, color));
}
<span class="literal">public</span> <span class="literal">void</span> subtract()
{
<span class="literal">if</span> (getChildren().size() &gt; <span class="number">0</span>)
{
<span class="comment">//&gt; Locate the ball with the largest radius</span>
Ball ball = (Ball) (getChildren().get(<span class="number">0</span>));
<span class="literal">for</span> (Node node : getChildren())
{
<span class="literal">if</span> (((Ball) node).getRadius() &gt; ball.getRadius())
{
ball = (Ball) node;
}
}
getChildren().remove(ball);
}
}
<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">0.1</span>);
}
<span class="literal">public</span> <span class="literal">void</span> decreaseSpeed()
{
animation.setRate(
animation.getRate() &gt; <span class="number">0</span> ? animation.getRate() - <span class="number">0.1</span> : <span class="number">0</span>);
}
<span class="literal">public</span> DoubleProperty rateProperty()
{
<span class="literal">return</span> animation.rateProperty();
}
<span class="literal">protected</span> <span class="literal">void</span> moveBall()
{
<span class="literal">for</span> (Node node : <span class="literal">this</span>.getChildren())
{
Ball ball = (Ball) node;
<span class="comment">// Check boundaries</span>
<span class="literal">if</span> (ball.getCenterX() &lt; ball.getRadius()
|| ball.getCenterX() &gt; getWidth() - ball.getRadius())
{
ball.dx *= -<span class="number">1</span>; <span class="comment">// Change ball move direction</span>
}
<span class="literal">if</span> (ball.getCenterY() &lt; ball.getRadius()
|| ball.getCenterY() &gt; getHeight() - ball.getRadius())
{
ball.dy *= -<span class="number">1</span>; <span class="comment">// Change ball move direction</span>
}
<span class="comment">// Adjust ball position</span>
ball.setCenterX(ball.dx + ball.getCenterX());
ball.setCenterY(ball.dy + ball.getCenterY());
}
}
}
<span class="literal">class</span> Ball <span class="literal">extends</span> Circle <span class="literal">implements</span> Comparable&lt;Ball&gt;
{
<span class="literal">private</span> <span class="literal">double</span> dx = <span class="number">1</span>, dy = <span class="number">1</span>;
Ball(<span class="literal">double</span> x, <span class="literal">double</span> y, <span class="literal">double</span> radius, Color color)
{
<span class="literal">super</span>(x, y, radius);
setFill(color); <span class="comment">// Set ball color</span>
}
<span class="literal">public</span> <span class="literal">int</span> compareTo(Ball b)
{
<span class="literal">if</span> (<span class="literal">this</span>.getRadius() - b.getRadius() &lt; <span class="number">0</span>)
{
<span class="literal">return</span> -<span class="number">1</span>;
}
<span class="literal">else</span> <span class="literal">if</span> (<span class="literal">this</span>.getRadius() - b.getRadius() == <span class="number">0</span>)
{
<span class="literal">return</span> <span class="number">0</span>;
}
<span class="literal">else</span>
{
<span class="literal">return</span> <span class="number">1</span>;
}
}
}
<span class="comment">/**</span>
<span class="comment"> * </span><span class="comment">The</span> <span class="comment">main</span> <span class="comment">method</span> <span class="comment">is</span> <span class="comment">only</span> <span class="comment">needed</span> <span class="comment">for</span> <span class="comment">the</span> <span class="comment">IDE</span> <span class="comment">with</span> <span class="comment">limited</span> <span class="comment">JavaFX</span> <span class="comment">support</span><span class="comment">.</span>
<span class="comment"> * </span><span class="comment">Not</span> <span class="comment">needed</span> <span class="comment">for</span> <span class="comment">running</span> <span class="comment">from</span> <span class="comment">the</span> <span class="comment">command</span> <span class="comment">line</span><span class="comment">.</span>
<span class="comment">*/</span>
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> main(String[] args)
{
launch(args);
}
}
</pre></body>
</html>