I forgor to push
This commit is contained in:
160
Semester 3/Assignments/DonorProject/src/anim/BouncingBall.java
Normal file
160
Semester 3/Assignments/DonorProject/src/anim/BouncingBall.java
Normal file
@@ -0,0 +1,160 @@
|
||||
/*
|
||||
* 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 anim;
|
||||
|
||||
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.Label;
|
||||
import javafx.scene.layout.GridPane;
|
||||
import javafx.scene.layout.Pane;
|
||||
import javafx.scene.paint.Color;
|
||||
import javafx.scene.shape.Circle;
|
||||
import javafx.scene.shape.Rectangle;
|
||||
import javafx.util.Duration;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author asdv
|
||||
*/
|
||||
public class BouncingBall extends Pane
|
||||
{
|
||||
|
||||
public int pScore = 0;
|
||||
public int cScore = 0;
|
||||
GridPane textDisplay = new GridPane();
|
||||
Rectangle racket = new Rectangle();
|
||||
|
||||
public final double radius = 10; // ball size 40
|
||||
private double x = radius, y = radius; // coordinates of ball
|
||||
private double dx = 1, dy = 1; // direction of the ball
|
||||
private Circle circle = new Circle(x, y, radius); // her majesty, the ball
|
||||
private Timeline animation; // animate the ball
|
||||
private Label playerScore = new Label("Your Score: 0");
|
||||
private Label compScore = new Label("Computer Score: 0");
|
||||
|
||||
public BouncingBall()
|
||||
{
|
||||
racket.setWidth(70);
|
||||
racket.setHeight(20);
|
||||
racket.relocate(0, 580);
|
||||
textDisplay.add(playerScore, 0, 0);
|
||||
textDisplay.add(compScore, 0, 1);
|
||||
|
||||
circle.setFill(Color.BROWN);
|
||||
getChildren().addAll(circle, racket, textDisplay);
|
||||
|
||||
animation = new Timeline(
|
||||
new KeyFrame(Duration.millis(50), (ActionEvent event)
|
||||
->
|
||||
{
|
||||
moveBall();
|
||||
if (y >= (getHeight() - 20))
|
||||
{
|
||||
boolean intersected = intersection();
|
||||
if (intersected && circle.getCenterX() <= racket.getX() / 2)
|
||||
{
|
||||
System.out.println("Intersected.");
|
||||
dx *= -1;
|
||||
dy *= -1;
|
||||
playerScored();
|
||||
}
|
||||
else
|
||||
{
|
||||
if (intersected && circle.getCenterX() >= racket.getX() / 2)
|
||||
{
|
||||
playerScored();
|
||||
dy *= -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (y >= (getHeight() - 40))
|
||||
{
|
||||
computerScored();
|
||||
dy *= -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}));
|
||||
animation.setCycleCount(Timeline.INDEFINITE);
|
||||
animation.setRate(animation.getRate() * 20);
|
||||
animation.play();
|
||||
}
|
||||
|
||||
private void moveBall()
|
||||
{
|
||||
if (x < radius || x > getWidth() - radius)
|
||||
{
|
||||
dx *= -1;
|
||||
}
|
||||
if (y < radius || y > getHeight() - radius)
|
||||
{
|
||||
dy *= -1;
|
||||
}
|
||||
|
||||
x += dx;
|
||||
y += dy;
|
||||
circle.setCenterX(x);
|
||||
circle.setCenterY(y);
|
||||
}
|
||||
|
||||
public void playerScored()
|
||||
{
|
||||
playerScore.setText("Your Score: " + (++this.pScore));
|
||||
}
|
||||
|
||||
public void computerScored()
|
||||
{
|
||||
compScore.setText("Computer Score: " + (++this.cScore));
|
||||
}
|
||||
|
||||
public boolean intersection()
|
||||
{
|
||||
boolean intersected = racket.getBoundsInParent().intersects(circle.getBoundsInParent());
|
||||
|
||||
if (intersected)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public void moveRacket(double x)
|
||||
{
|
||||
racket.relocate(x, 580);
|
||||
}
|
||||
|
||||
public void play()
|
||||
{
|
||||
animation.play();
|
||||
}
|
||||
|
||||
public void pause()
|
||||
{
|
||||
animation.pause();
|
||||
}
|
||||
|
||||
public void increaseSpeed()
|
||||
{
|
||||
animation.setRate(animation.getRate() * 1.5);
|
||||
}
|
||||
|
||||
public void decreaseSpeed()
|
||||
{
|
||||
animation.setRate(animation.getRate() * 1.5 > 0 ? animation.getRate() / 1.5 : 0);
|
||||
}
|
||||
|
||||
public DoubleProperty rateProperty()
|
||||
{
|
||||
return animation.rateProperty();
|
||||
}
|
||||
|
||||
}
|
65
Semester 3/Assignments/DonorProject/src/anim/Tennis.java
Normal file
65
Semester 3/Assignments/DonorProject/src/anim/Tennis.java
Normal file
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
* 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 anim;
|
||||
|
||||
import javafx.application.Application;
|
||||
import javafx.event.EventHandler;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.scene.input.KeyCode;
|
||||
import javafx.scene.input.MouseEvent;
|
||||
import javafx.scene.shape.Rectangle;
|
||||
import javafx.stage.Stage;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author asdv
|
||||
*/
|
||||
public class Tennis extends Application
|
||||
{
|
||||
|
||||
@Override
|
||||
public void start(Stage primaryStage) throws Exception
|
||||
{
|
||||
|
||||
BouncingBall bouncingBall = new BouncingBall();
|
||||
|
||||
Scene scene = new Scene(bouncingBall, 800, 600);
|
||||
primaryStage.setTitle("Play Tennis");
|
||||
primaryStage.setScene(scene);
|
||||
primaryStage.show();
|
||||
|
||||
bouncingBall.requestFocus();
|
||||
bouncingBall.setOnMousePressed((MouseEvent event) ->
|
||||
{
|
||||
bouncingBall.pause();
|
||||
});
|
||||
bouncingBall.setOnMouseReleased((MouseEvent event) ->
|
||||
{
|
||||
bouncingBall.play();
|
||||
});
|
||||
bouncingBall.setOnKeyPressed(e ->
|
||||
{
|
||||
if(e.getCode() == KeyCode.UP)
|
||||
{
|
||||
bouncingBall.increaseSpeed();
|
||||
}
|
||||
else if(e.getCode() == KeyCode.DOWN)
|
||||
{
|
||||
bouncingBall.decreaseSpeed();
|
||||
}
|
||||
});
|
||||
bouncingBall.setOnMouseMoved((MouseEvent event) ->
|
||||
{
|
||||
bouncingBall.moveRacket(event.getSceneX() - bouncingBall.racket.getWidth() / 2);
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
launch(args);
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/javafx/FXMain.java to edit this template
|
||||
*/
|
||||
package donorproject;
|
||||
|
||||
import javafx.application.Application;
|
||||
import javafx.event.ActionEvent;
|
||||
import javafx.event.EventHandler;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.scene.control.Button;
|
||||
import javafx.scene.layout.StackPane;
|
||||
import javafx.stage.Stage;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author caleb
|
||||
*/
|
||||
public class DonorProject extends Application {
|
||||
|
||||
@Override
|
||||
public void start(Stage primaryStage) {
|
||||
Button btn = new Button();
|
||||
btn.setText("Say 'Hello World'");
|
||||
btn.setOnAction(new EventHandler<ActionEvent>() {
|
||||
|
||||
@Override
|
||||
public void handle(ActionEvent event) {
|
||||
System.out.println("Hello World!");
|
||||
}
|
||||
});
|
||||
|
||||
StackPane root = new StackPane();
|
||||
root.getChildren().add(btn);
|
||||
|
||||
Scene scene = new Scene(root, 300, 250);
|
||||
|
||||
primaryStage.setTitle("Hello World!");
|
||||
primaryStage.setScene(scene);
|
||||
primaryStage.show();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param args the command line arguments
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
launch(args);
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user