Java moment

This commit is contained in:
2023-10-18 13:38:56 -05:00
parent 7da3542ede
commit f9babdde19
42 changed files with 1616 additions and 0 deletions

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<actions>
<action>
<actionName>run</actionName>
<packagings>
<packaging>jar</packaging>
</packagings>
<goals>
<goal>clean</goal>
<goal>javafx:run</goal>
</goals>
</action>
<action>
<actionName>debug</actionName>
<goals>
<goal>clean</goal>
<goal>javafx:run@ide-debug</goal>
</goals>
<properties>
<jpda.listen>true</jpda.listen>
</properties>
</action>
<action>
<actionName>profile</actionName>
<goals>
<goal>clean</goal>
<goal>javafx:run@ide-profile</goal>
</goals>
</action>
<action>
<actionName>CUSTOM-jlink</actionName>
<displayName>jlink</displayName>
<goals>
<goal>clean</goal>
<!-- compile not needed with javafx-maven-plugin v0.0.5 -->
<goal>compile</goal>
<goal>javafx:jlink</goal>
</goals>
</action>
</actions>

View File

@@ -0,0 +1,76 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>edu.slcc.asdv.caleb</groupId>
<artifactId>JavaFXBallsWithComparator</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>20</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<release>17</release>
</configuration>
</plugin>
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.4</version>
<configuration>
<mainClass>edu.slcc.asdv.caleb.javafxballswithcomparator.App</mainClass>
</configuration>
<executions>
<execution>
<!-- Default configuration for running -->
<!-- Usage: mvn clean javafx:run -->
<id>default-cli</id>
</execution>
<execution>
<!-- Configuration for manual attach debugging -->
<!-- Usage: mvn clean javafx:run@debug -->
<id>debug</id>
<configuration>
<options>
<option>-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=localhost:8000</option>
</options>
</configuration>
</execution>
<execution>
<!-- Configuration for automatic IDE debugging -->
<id>ide-debug</id>
<configuration>
<options>
<option>-agentlib:jdwp=transport=dt_socket,server=n,address=${jpda.address}</option>
</options>
</configuration>
</execution>
<execution>
<!-- Configuration for automatic IDE profiling -->
<id>ide-profile</id>
<configuration>
<options>
<option>${profiler.jvmargs.arg1}</option>
<option>${profiler.jvmargs.arg2}</option>
<option>${profiler.jvmargs.arg3}</option>
<option>${profiler.jvmargs.arg4}</option>
<option>${profiler.jvmargs.arg5}</option>
</options>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@@ -0,0 +1,42 @@
/*
* 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 edu.slcc.asdv.caleb.javafxballswithcomparator;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
/**
*
* @author caleb
*/
public class A1 implements Comparable<A1> {
int x;
public A1() {}
public A1(int x) {this.x = x;}
@Override
public int compareTo(A1 o)
{
return this.x - o.x;
}
@Override
public String toString()
{
return "A1{" + "x=" + x + '}';
}
public static void main(String[] args)
{
System.out.println("Sorting in ascending order");
List<A1> list1 = Arrays.asList(new A1(3), new A1(), new A1(2));
Collections.sort(list1);
System.out.println(list1);
System.out.println("Sorting in descending order");
Collections.sort(list1, Collections.reverseOrder());
System.out.println(list1);
}
}

View File

@@ -0,0 +1,41 @@
/*
* 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 edu.slcc.asdv.caleb.javafxballswithcomparator;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
*
* @author caleb
*/
public class A2 {
int x;
public A2() {}
public A2(int x) {this.x = x;}
@Override
public String toString()
{
return "A2{" + "x=" + x + '}';
}
public static void main(String[] args)
{
System.out.println("Sorting in ascending order");
List<A2> list1 = Arrays.asList(new A2(4), new A2(), new A2(2));
Comparator<A2> c = new Comparator<A2>() {
@Override
public int compare(A2 o1, A2 o2)
{
return o1.x - o2.x;
}
};
Collections.sort(list1, c);
System.out.println(list1);
}
}

View File

@@ -0,0 +1,30 @@
package edu.slcc.asdv.caleb.javafxballswithcomparator;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
/**
* JavaFX App
*/
public class App extends Application {
@Override
public void start(Stage stage) {
var javaVersion = SystemInfo.javaVersion();
var javafxVersion = SystemInfo.javafxVersion();
var label = new Label("Hello, JavaFX " + javafxVersion + ", running on Java " + javaVersion + ".");
var scene = new Scene(new StackPane(label), 640, 480);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}

View File

@@ -0,0 +1,51 @@
/*
* 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 edu.slcc.asdv.caleb.javafxballswithcomparator;
/**
*
* @author caleb
*/
public class Circle extends GeometricObject {
private double radius;
public Circle() {
}
public Circle(double radius) {
this.radius = radius;
}
/** Return radius */
public double getRadius() {
return radius;
}
/** Set a new radius */
public void setRadius(double radius) {
this.radius = radius;
}
@Override /** Return area */
public double getArea() {
return radius * radius * Math.PI;
}
/** Return diameter */
public double getDiameter() {
return 2 * radius;
}
@Override /** Return perimeter */
public double getPerimeter() {
return 2 * radius * Math.PI;
}
/* Print the circle info */
public void printCircle() {
System.out.println("The circle is created " + getDateCreated() +
" and the radius is " + radius);
}
}

View File

@@ -0,0 +1,66 @@
/*
* 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 edu.slcc.asdv.caleb.javafxballswithcomparator;
/**
*
* @author caleb
*/
public abstract class GeometricObject {
private String color = "white";
private boolean filled;
private java.util.Date dateCreated;
/** Construct a default geometric object */
protected GeometricObject() {
dateCreated = new java.util.Date();
}
/** Construct a geometric object with color and filled value */
protected GeometricObject(String color, boolean filled) {
dateCreated = new java.util.Date();
this.color = color;
this.filled = filled;
}
/** Return color */
public String getColor() {
return color;
}
/** Set a new color */
public void setColor(String color) {
this.color = color;
}
/** Return filled. Since filled is boolean,
* the get method is named isFilled */
public boolean isFilled() {
return filled;
}
/** Set a new filled */
public void setFilled(boolean filled) {
this.filled = filled;
}
/** Get dateCreated */
public java.util.Date getDateCreated() {
return dateCreated;
}
@Override
public String toString() {
return "created on " + dateCreated + "\ncolor: " + color +
" and filled: " + filled;
}
/** Abstract method getArea */
public abstract double getArea();
/** Abstract method getPerimeter */
public abstract double getPerimeter();
}

View File

@@ -0,0 +1,19 @@
/*
* 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 edu.slcc.asdv.caleb.javafxballswithcomparator;
/**
*
* @author caleb
*/
import java.util.Comparator;
public class GeometricObjectComparator
implements Comparator<GeometricObject>, java.io.Serializable {
public int compare(GeometricObject o1, GeometricObject o2) {
return o1.getArea() > o2.getArea() ?
1 : o1.getArea() == o2.getArea() ? 0 : -1;
}
}

View File

@@ -0,0 +1,185 @@
package edu.slcc.asdv.caleb.javafxballswithcomparator;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.beans.property.DoubleProperty;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollBar;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.util.Duration;
public class MultipleBallsWithComparator extends Application
{
@Override // Override the start method in the Application class
public void start(Stage primaryStage)
{
MultipleBallPane ballPane = new MultipleBallPane();
Button btAdd = new Button("+");
Button btSubtract = new Button("-");
HBox hBox = new HBox(10);
hBox.getChildren().addAll(btAdd, btSubtract);
hBox.setAlignment(Pos.CENTER);
// Add or remove a ball
btAdd.setOnAction(e -> ballPane.add());
btSubtract.setOnAction(e -> ballPane.subtract());
// Pause and resume animation
ballPane.setOnMousePressed(e -> ballPane.pause());
ballPane.setOnMouseReleased(e -> ballPane.play());
// Use a scroll bar to control animation speed
ScrollBar sbSpeed = new ScrollBar();
sbSpeed.setMax(20);
sbSpeed.setValue(10);
ballPane.rateProperty().bind(sbSpeed.valueProperty());
BorderPane pane = new BorderPane();
pane.setCenter(ballPane);
pane.setTop(sbSpeed);
pane.setBottom(hBox);
// Create a scene and place the pane in the stage
Scene scene = new Scene(pane, 250, 150);
primaryStage.setTitle("Multiple Bouncing Balls"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
}
private class MultipleBallPane extends Pane
{
private Timeline animation;
public MultipleBallPane()
{
// Create an animation for moving the ball
animation = new Timeline(
new KeyFrame(Duration.millis(50), e -> moveBall()));
animation.setCycleCount(Timeline.INDEFINITE);
animation.play(); // Start animation
}
public void add()
{
Color color = new Color(Math.random(),
Math.random(), Math.random(), 0.5);
getChildren().add(new Ball(30, 30, Math.random() * 16 + 5, color));
}
public void subtract()
{
if (getChildren().size() > 0)
{
//> Locate the ball with the largest radius
Ball ball = (Ball) (getChildren().get(0));
for (Node node : getChildren())
{
if (((Ball) node).getRadius() > ball.getRadius())
{
ball = (Ball) node;
}
}
getChildren().remove(ball);
}
}
public void play()
{
animation.play();
}
public void pause()
{
animation.pause();
}
public void increaseSpeed()
{
animation.setRate(animation.getRate() + 0.1);
}
public void decreaseSpeed()
{
animation.setRate(
animation.getRate() > 0 ? animation.getRate() - 0.1 : 0);
}
public DoubleProperty rateProperty()
{
return animation.rateProperty();
}
protected void moveBall()
{
for (Node node : this.getChildren())
{
Ball ball = (Ball) node;
// Check boundaries
if (ball.getCenterX() < ball.getRadius()
|| ball.getCenterX() > getWidth() - ball.getRadius())
{
ball.dx *= -1; // Change ball move direction
}
if (ball.getCenterY() < ball.getRadius()
|| ball.getCenterY() > getHeight() - ball.getRadius())
{
ball.dy *= -1; // Change ball move direction
}
// Adjust ball position
ball.setCenterX(ball.dx + ball.getCenterX());
ball.setCenterY(ball.dy + ball.getCenterY());
}
}
}
class Ball extends Circle implements Comparable<Ball>
{
private double dx = 1, dy = 1;
Ball(double x, double y, double radius, Color color)
{
super(x, y, radius);
setFill(color); // Set ball color
}
public int compareTo(Ball b)
{
if (this.getRadius() - b.getRadius() < 0)
{
return -1;
}
else if (this.getRadius() - b.getRadius() == 0)
{
return 0;
}
else
{
return 1;
}
}
}
/**
* The main method is only needed for the IDE with limited JavaFX support.
* Not needed for running from the command line.
*/
public static void main(String[] args)
{
launch(args);
}
}

View File

@@ -0,0 +1,53 @@
/*
* 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 edu.slcc.asdv.caleb.javafxballswithcomparator;
/**
*
* @author caleb
*/
public class Rectangle extends GeometricObject {
private double width;
private double height;
public Rectangle() {
}
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
/** Return width */
public double getWidth() {
return width;
}
/** Set a new width */
public void setWidth(double width) {
this.width = width;
}
/** Return height */
public double getHeight() {
return height;
}
/** Set a new height */
public void setHeight(double height) {
this.height = height;
}
@Override /** Return area */
public double getArea() {
return width * height;
}
@Override /** Return perimeter */
public double getPerimeter() {
return 2 * (width + height);
}
}

View File

@@ -0,0 +1,13 @@
package edu.slcc.asdv.caleb.javafxballswithcomparator;
public class SystemInfo {
public static String javaVersion() {
return System.getProperty("java.version");
}
public static String javafxVersion() {
return System.getProperty("javafx.version");
}
}

View File

@@ -0,0 +1,45 @@
package edu.slcc.asdv.caleb.javafxballswithcomparator;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
public class TestArrayAndLinkedList
{
public static void main(String[] args)
{
List<Integer> arrayList = new ArrayList<>();
arrayList.add(1);
arrayList.add(2);
arrayList.add(3);
arrayList.add(1);
arrayList.add(4);
arrayList.add(0, 10);
arrayList.add(3, 30);
System.out.println("A list of integers in the array list:");
System.out.println(arrayList);
LinkedList<Object> linkedList = new LinkedList<>(arrayList);
linkedList.add(1, "red");
linkedList.removeLast();
linkedList.addFirst("green");
System.out.println("Display the linked list backward:");
ListIterator<Object> listIterator = linkedList.listIterator();
while (listIterator.hasNext())
{
System.out.print(listIterator.next() + " ");
}
System.out.println();
System.out.println("Display the linked list backward:");
listIterator = linkedList.listIterator(linkedList.size());
while (listIterator.hasPrevious())
{
System.out.print(listIterator.previous() + " ");
}
}
}

View File

@@ -0,0 +1,29 @@
/*
* 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 edu.slcc.asdv.caleb.javafxballswithcomparator;
/**
*
* @author caleb
*/
import java.util.Comparator;
public class TestComparator {
public static void main(String[] args) {
GeometricObject g1 = new Rectangle(5, 5);
GeometricObject g2 = new Circle(5);
GeometricObject g =
max(g1, g2, new GeometricObjectComparator());
System.out.println("The area of the larger object is " +
g.getArea());
}
public static GeometricObject max(GeometricObject g1,
GeometricObject g2, Comparator<GeometricObject> c) {
return c.compare(g1, g2) > 0 ? g1 : g2;
}
}

View File

@@ -0,0 +1,4 @@
module edu.slcc.asdv.caleb.javafxballswithcomparator {
requires javafx.controls;
exports edu.slcc.asdv.caleb.javafxballswithcomparator;
}