Java 20 👀

master
Caleb Fontenot 2023-09-10 08:45:41 +07:00
parent 6f5582f86e
commit 118d63c1c2
43 changed files with 2383 additions and 3 deletions

3
.gitignore vendored

@ -153,3 +153,6 @@
/Semester 3/Assignments/MP2FX_Stelly/nbproject/private/
/Semester 3/Assignments/DonorProject/nbproject/private/
/Semester 3/Assignments/DonorProject/build/
/Semester 3/Assignments/MP2-chapter4_Java20_CalebFontenot/nbproject/private/
/Semester 3/Assignments/MP2-chapter4_Java20_CalebFontenot/dist/
/Semester 3/Assignments/MP2-chapter4_Java20_CalebFontenot/build/

@ -37,7 +37,8 @@ includes=**
# Non-JavaFX jar file creation is deactivated in JavaFX 2.0+ projects
jar.archive.disabled=true
jar.compress=false
javac.classpath=
javac.classpath=\
${libs.JavaFX20.classpath}
# Space-separated list of extra javac options
javac.compilerargs=
javac.deprecation=false
@ -46,8 +47,8 @@ javac.modulepath=
javac.processormodulepath=
javac.processorpath=\
${javac.classpath}
javac.source=1.8
javac.target=1.8
javac.source=20
javac.target=20
javac.test.classpath=\
${javac.classpath}:\
${build.classes.dir}

@ -0,0 +1,73 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- You may freely edit this file. See commented blocks below for -->
<!-- some examples of how to customize the build. -->
<!-- (If you delete it and reopen the project it will be recreated.) -->
<!-- By default, only the Clean and Build commands use this build script. -->
<!-- Commands such as Run, Debug, and Test only use this build script if -->
<!-- the Compile on Save feature is turned off for the project. -->
<!-- You can turn off the Compile on Save (or Deploy on Save) setting -->
<!-- in the project's Project Properties dialog box.-->
<project name="MP2-chapter4_Java20_CalebFontenot" default="default" basedir=".">
<description>Builds, tests, and runs the project MP2-chapter4_Java20_CalebFontenot.</description>
<import file="nbproject/build-impl.xml"/>
<!--
There exist several targets which are by default empty and which can be
used for execution of your tasks. These targets are usually executed
before and after some main targets. They are:
-pre-init: called before initialization of project properties
-post-init: called after initialization of project properties
-pre-compile: called before javac compilation
-post-compile: called after javac compilation
-pre-compile-single: called before javac compilation of single file
-post-compile-single: called after javac compilation of single file
-pre-compile-test: called before javac compilation of JUnit tests
-post-compile-test: called after javac compilation of JUnit tests
-pre-compile-test-single: called before javac compilation of single JUnit test
-post-compile-test-single: called after javac compilation of single JUunit test
-pre-jar: called before JAR building
-post-jar: called after JAR building
-post-clean: called after cleaning build products
(Targets beginning with '-' are not intended to be called on their own.)
Example of inserting an obfuscator after compilation could look like this:
<target name="-post-compile">
<obfuscate>
<fileset dir="${build.classes.dir}"/>
</obfuscate>
</target>
For list of available properties check the imported
nbproject/build-impl.xml file.
Another way to customize the build is by overriding existing main targets.
The targets of interest are:
-init-macrodef-javac: defines macro for javac compilation
-init-macrodef-junit: defines macro for junit execution
-init-macrodef-debug: defines macro for class debugging
-init-macrodef-java: defines macro for class execution
-do-jar: JAR building
run: execution of project
-javadoc-build: Javadoc generation
test-report: JUnit report generation
An example of overriding the target for project execution could look like this:
<target name="run" depends="MP2-chapter4_Java20_CalebFontenot-impl.jar">
<exec dir="bin" executable="launcher.exe">
<arg file="${dist.jar}"/>
</exec>
</target>
Notice that the overridden target depends on the jar target and not only on
the compile target as the regular run target does. Again, for a list of available
properties which you can use, check the target you are overriding in the
nbproject/build-impl.xml file.
-->
</project>

@ -0,0 +1,3 @@
Manifest-Version: 1.0
X-COMMENT: Main-Class will be added automatically by build

@ -0,0 +1,8 @@
build.xml.data.CRC32=3b569e2a
build.xml.script.CRC32=413afed0
build.xml.stylesheet.CRC32=f85dc8f2@1.107.0.48
# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml.
# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you.
nbproject/build-impl.xml.data.CRC32=3b569e2a
nbproject/build-impl.xml.script.CRC32=80ea1a25
nbproject/build-impl.xml.stylesheet.CRC32=12e0a6c2@1.107.0.48

@ -0,0 +1,114 @@
annotation.processing.enabled=true
annotation.processing.enabled.in.editor=false
annotation.processing.processors.list=
annotation.processing.run.all.processors=true
annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output
application.title=MP2-chapter4_Java20_CalebFontenot
application.vendor=caleb
build.classes.dir=${build.dir}/classes
build.classes.excludes=**/*.java,**/*.form
# This directory is removed when the project is cleaned:
build.dir=build
build.generated.dir=${build.dir}/generated
build.generated.sources.dir=${build.dir}/generated-sources
# Only compile against the classpath explicitly listed here:
build.sysclasspath=ignore
build.test.classes.dir=${build.dir}/test/classes
build.test.results.dir=${build.dir}/test/results
# Uncomment to specify the preferred debugger connection transport:
#debug.transport=dt_socket
debug.classpath=\
${run.classpath}
debug.modulepath=\
${run.modulepath}
debug.test.classpath=\
${run.test.classpath}
debug.test.modulepath=\
${run.test.modulepath}
# Files in build.classes.dir which should be excluded from distribution jar
dist.archive.excludes=
# This directory is removed when the project is cleaned:
dist.dir=dist
dist.jar=${dist.dir}/MP2-chapter4_Java20_CalebFontenot.jar
dist.javadoc.dir=${dist.dir}/javadoc
dist.jlink.dir=${dist.dir}/jlink
dist.jlink.output=${dist.jlink.dir}/MP2-chapter4_Java20_CalebFontenot
endorsed.classpath=
excludes=
includes=**
jar.archive.disabled=${jnlp.enabled}
jar.compress=false
jar.index=${jnlp.enabled}
javac.classpath=\
${libs.JavaFX17.classpath}
# Space-separated list of extra javac options
javac.compilerargs=
javac.deprecation=false
javac.external.vm=true
javac.modulepath=
javac.processormodulepath=
javac.processorpath=\
${javac.classpath}
javac.source=17
javac.target=17
javac.test.classpath=\
${javac.classpath}:\
${build.classes.dir}:\
${libs.JavaFX17.classpath}
javac.test.modulepath=\
${javac.modulepath}
javac.test.processorpath=\
${javac.test.classpath}
javadoc.additionalparam=
javadoc.author=false
javadoc.encoding=${source.encoding}
javadoc.html5=false
javadoc.noindex=false
javadoc.nonavbar=false
javadoc.notree=false
javadoc.private=false
javadoc.splitindex=true
javadoc.use=true
javadoc.version=false
javadoc.windowtitle=
# The jlink additional root modules to resolve
jlink.additionalmodules=
# The jlink additional command line parameters
jlink.additionalparam=
jlink.launcher=true
jlink.launcher.name=MP2-chapter4_Java20_CalebFontenot
jnlp.codebase.type=no.codebase
jnlp.descriptor=application
jnlp.enabled=false
jnlp.mixed.code=default
jnlp.offline-allowed=false
jnlp.signed=false
jnlp.signing=
jnlp.signing.alias=
jnlp.signing.keystore=
main.class=MP2_chapter4_CalebFontenot.BallControl
# Optional override of default Application-Library-Allowable-Codebase attribute identifying the locations where your signed RIA is expected to be found.
manifest.custom.application.library.allowable.codebase=
# Optional override of default Caller-Allowable-Codebase attribute identifying the domains from which JavaScript code can make calls to your RIA without security prompts.
manifest.custom.caller.allowable.codebase=
# Optional override of default Codebase manifest attribute, use to prevent RIAs from being repurposed
manifest.custom.codebase=
# Optional override of default Permissions manifest attribute (supported values: sandbox, all-permissions)
manifest.custom.permissions=
manifest.file=manifest.mf
meta.inf.dir=${src.dir}/META-INF
mkdist.disabled=false
platform.active=default_platform
run.classpath=\
${javac.classpath}:\
${build.classes.dir}
run.modulepath=\
${javac.modulepath}
run.test.classpath=\
${javac.test.classpath}:\
${build.test.classes.dir}
run.test.modulepath=\
${javac.test.modulepath}
source.encoding=UTF-8
src.dir=src
test.src.dir=test

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://www.netbeans.org/ns/project/1">
<type>org.netbeans.modules.java.j2seproject</type>
<configuration>
<data xmlns="http://www.netbeans.org/ns/j2se-project/3">
<name>MP2-chapter4_Java20_CalebFontenot</name>
<source-roots>
<root id="src.dir"/>
</source-roots>
<test-roots>
<root id="test.src.dir"/>
</test-roots>
</data>
</configuration>
</project>

@ -0,0 +1,90 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package MP2_chapter4_CalebFontenot;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.input.KeyCode;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;
/**
*
* @author caleb
*/
public class BallControl extends Application {
private boolean mouseFocus = false;
private int racketPosition = 0;
@Override
public void start(Stage primaryStage) throws Exception
{
//Parent root = FXMLLoader.load(getClass()).getResource("hellofx.fxml");
BouncingBall bouncingBall = new BouncingBall();
// Create a scene and place it in the stage
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); // set relative to center of racket
bouncingBall.moveRacket(mouseX);
}
});
// Increase and decrease animation
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);
}
}

@ -0,0 +1,258 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package MP2_chapter4_CalebFontenot;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.beans.property.DoubleProperty;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.util.Duration;
/**
*
* @author caleb
*/
public class BouncingBall extends Pane {
private List<MediaPlayer> mediaPlayers = new ArrayList<>();
GridPane textPane = new GridPane();
private String currentlyPlaying;
private final int COLLISION_COOLDOWN = 20;
private long timeSinceLastCollisionEvent = 0;
final double RACKET_LENGTH = 100;
final double radius = 20;
private double x = radius, y = radius;
private double dx = 1, dy = 1;
private Circle circle = new Circle(x, y, radius);
private Rectangle racket = new Rectangle(RACKET_LENGTH, 20);
private Label infoLabel = new Label("Your mouse cursor must be inside the bounds of the window to play!");
private Label racketTime = new Label();
private Label ballCords = new Label();
private Label playerScoreLabel = new Label("Player Score: 0");
private Label computerScoreLabel = new Label("Computer Score: 0");
private Label nowPlaying = new Label();
private Button restartBgm = new Button("Restart BGM");
private Label jvmVersion = new Label("Java version: Java " + System.getProperty("java.version"));
private Timeline animation;
private int computerScore, playerScore = 0;
public BouncingBall() {
restartBgm.setOnMouseClicked(e -> {
playSound("bgm");
});
circle.setFill(Color.RED); // Set ball color
racket.setFill(Color.BLUE);
textPane.add(racketTime, 0, 0);
textPane.add(ballCords, 0, 1);
textPane.add(playerScoreLabel, 0, 2);
textPane.add(computerScoreLabel, 0, 3);
textPane.add(nowPlaying, 0, 4);
textPane.add(jvmVersion, 0, 5);
textPane.add(restartBgm, 0, 6);
getChildren().addAll(circle, racket, textPane); // Place a ball into this pane
racket.relocate(0, 580);
playSound("bgm");
// Create an animation for moving the ball
animation = new Timeline(new KeyFrame(Duration.millis(1), new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent t) {
timeSinceLastCollisionEvent++;
racketTime.setText("Frames since last collision: " + timeSinceLastCollisionEvent);
moveBall();
// Process collisions
if (y >= (getHeight() - 20) && (timeSinceLastCollisionEvent > COLLISION_COOLDOWN)) {
timeSinceLastCollisionEvent = 0;
playSound("collision");
incrementComputerScore();
}
if ((x <= 20 && (timeSinceLastCollisionEvent > COLLISION_COOLDOWN)) || ((x >= getWidth() - 20) && (timeSinceLastCollisionEvent > COLLISION_COOLDOWN))) {
timeSinceLastCollisionEvent = 0;
playSound("collision");
}
if (y <= 20 && (timeSinceLastCollisionEvent > COLLISION_COOLDOWN)) {
timeSinceLastCollisionEvent = 0;
playSound("collision");
}
}
})
);
animation.setRate(animation.getRate() * .5);
animation.setCycleCount(Timeline.INDEFINITE);
}
private boolean processRacketCollision() {
boolean racketCollision = racket.getBoundsInParent().intersects(circle.getBoundsInParent());
if (racketCollision && (timeSinceLastCollisionEvent > COLLISION_COOLDOWN)) { // This is second condition is a cooldown. It prevents odd behavior happening with the ball and the racket if the racket hits the ball at certain angles.
System.out.println("Racket collision detected!");
timeSinceLastCollisionEvent = 0;
incrementPlayerScore();
playSound("racket");
return true;
} else {
return false;
}
}
private void moveBall() {
// Check boundaries
if (x < radius || x > getWidth() - radius) {
dx *= -1; // Change ball move direction
}
if (y < radius || y > getHeight() - radius || processRacketCollision()) {
dy *= -1; // Change ball move direction
}
// Adjust ball position by 1 or -1
x += dx;
y += dy;
circle.setCenterX(x);
circle.setCenterY(y);
ballCoordsToLabel();
}
public void play() {
animation.play();
}
public void pause() {
animation.pause();
}
public void increaseSpeed() {
animation.setRate(animation.getRate() * 1.5);
System.out.println(animation.getRate());
}
public void decreaseSpeed() {
animation.setRate(animation.getRate() * 1.5 > 0 ? animation.getRate() / 1.5 : 0);
System.out.println(animation.getRate());
}
public DoubleProperty rateProperty() {
return animation.rateProperty();
}
public void moveRacket(double x) {
racket.relocate(x, getHeight() - 20);
}
public void showInfoLabel() {
double paneHeight = getHeight();
double paneWidth = getWidth();
getChildren().add(infoLabel);
// Center Text
infoLabel.relocate(paneWidth / 4.0, paneHeight / 2.0);
}
public void hideInfoLabel() {
getChildren().remove(infoLabel);
}
private void incrementPlayerScore() {
playerScoreLabel.setText("Player score: " + ++playerScore);
}
private void incrementComputerScore() {
computerScoreLabel.setText("Computer score: " + ++computerScore);
}
private void ballCoordsToLabel() {
ballCords.setText("Ball coords: " + x + ", " + y);
}
private void playSound(String sampleType) {
boolean isSfx = true;
String sample = "sound/sfx/Sample_00";
int randInt = (int) (Math.random() * 2);
switch (sampleType) {
case "collision":
randInt = (int) (Math.random() * 2);
isSfx = true;
switch (randInt) {
case 0:
sample += "05";
break;
case 1:
sample += "07";
break;
}
break;
case "racket":
randInt = (int) (Math.random() * 2);
isSfx = true;
switch (randInt) {
case 0:
sample += "10";
break;
case 1:
sample += "12";
break;
}
break;
case "bgm":
sample = "sound/bgm/";
randInt = (int) (Math.random() * 3);
isSfx = false;
switch (randInt) {
case 0:
currentlyPlaying = "Caleb Fontenot - Blips";
nowPlaying.setText("Now playing: " + currentlyPlaying);
break;
case 1:
currentlyPlaying = "Caleb Fontenot - DEAL WITH IT";
nowPlaying.setText("Now playing: " + currentlyPlaying);
break;
case 2:
currentlyPlaying = "Caleb Fontenot - Tomodachi Moment";
nowPlaying.setText("Now playing: " + currentlyPlaying);
break;
}
sample += currentlyPlaying;
break;
}
Media sound = new Media(new File(sample + ".wav").toURI().toString());
MediaPlayer mediaPlayer = new MediaPlayer(sound);
mediaPlayer.setStartTime(Duration.ZERO);
mediaPlayer.setAutoPlay(true);
// If we're playing BGM, lower the volume a little
if (!isSfx) {
mediaPlayer.setVolume(0.6);
mediaPlayer.setCycleCount(MediaPlayer.INDEFINITE);
}
mediaPlayer.play();
mediaPlayers.add(mediaPlayer); // Prevent the JVM's GC from deleting our media player if there's already one running
// Free the media player when it has finished playing a sound.
mediaPlayer.setOnEndOfMedia(new Runnable() {
@Override
public void run() {
//System.out.println("I'm done playing sound! Please take me to the ether, Java GC!");
mediaPlayer.stop();
mediaPlayer.dispose();
mediaPlayers.remove(mediaPlayer);
}
});
}
}

@ -0,0 +1,26 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package MP2_chapter4_CalebFontenot;
import javafx.scene.media.MediaPlayer;
/**
*
* @author caleb
*/
public class MediaType {
private MediaPlayer mediaPlayer;
private boolean isBgm;
public MediaType(MediaPlayer mediaPlayer, boolean isBgm) {
this.mediaPlayer = mediaPlayer;
this.isBgm = isBgm;
}
public boolean isIsBgm() {
return isBgm;
}
}

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<project-shared-configuration>
<!--
This file contains additional configuration written by modules in the NetBeans IDE.
The configuration is intended to be shared among all the users of project and
therefore it is assumed to be part of version control checkout.
Without this configuration present, some functionality in the IDE may be limited or fail altogether.
-->
<properties xmlns="http://www.netbeans.org/ns/maven-properties-data/1">
<!--
Properties that influence various parts of the IDE, especially code formatting and the like.
You can copy and paste the single properties, into the pom.xml file and the IDE will pick them up.
That way multiple projects can share the same settings (useful for formatting rules for example).
Any value defined here will override the pom.xml file value but is only applicable to the current project.
-->
<netbeans.hint.jdkPlatform>JDK_20</netbeans.hint.jdkPlatform>
</properties>
</project-shared-configuration>