Commit d6006128 authored by norbertdajnowski's avatar norbertdajnowski

Commit 1

parents
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Bounce</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.8
import javafx.application.*;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.stage.*;
import javafx.scene.*;
import javafx.scene.paint.*;
import javafx.scene.canvas.*;
import javafx.scene.control.Button;
import javafx.scene.layout.Pane;
import javafx.scene.shape.*;
import javafx.animation.*;
import java.util.*;
import java.math.*;
import javafx.*;
class BallGen {
public int width = 0;
public int height = 0;
public boolean DownDir = true;
public boolean RightDir= true;
private boolean GravIncreased = true;
private Random RandomGen = new Random();
public void Collision() {
if(DownDir == true) {
DownDir = false;
}
else {
DownDir = true;
}
if (RightDir == true) {
RightDir = false;
}
else {
RightDir = false;
}
}
public void WidthAdjust() {
if ((width + 5 < 1024) && (RightDir == true)) {
width = width + (RandomGen.nextInt(1) + 5);
}
else if (width - 5 > 0) {
RightDir = false;
width = width - (RandomGen.nextInt(2) + 3);
}
else {
RightDir = true;
}
}
public void HeightAdjust(double GravMult) {
if ((GravMult == 1) || (GravMult > 1)) {
GravIncreased = true;
}
else {
GravIncreased = false;
}
if(GravIncreased = true) {
if ((height + (5 * GravMult) < 768) && (DownDir == true)) {
height = (int) (height + (5 * GravMult));
}
else if (height - 3 > 0) {
DownDir = false;
height = height - 3;
}
else {
DownDir = true;
}
}
else {
if ((height + 3 < 768) && (DownDir == true)) {
height = height + 3;
}
else if (height - (5 * GravMult) > 0) {
DownDir = false;
height = (int) (height - (5 * GravMult));
}
else {
DownDir = true;
}
}
}
}
public class Bounce extends Application {
/** Boilerplate method to get JavaFX going */
public static void main(String[] args) {
launch(args);
}
/** Width of our screen in pixels */
private final int width = 1024;
/** Height of our screen in pixels */
private final int height = 768;
/** Canvas that we will draw things on */
private Canvas canvas = new Canvas(width, height);
/** Time when the last frame was drawn, in nanoseconds */
private long last = 0;
/** Public ball generated */
ArrayList<BallGen> ball= new ArrayList<BallGen>();
//ball template for generating balls
BallGen ballTemp = new BallGen();
//time collision measure and gravity constant
long CollisionControl = 0;
public double GravMult = 1;
/** Set up anything that is needed later */
private void setup() {
}
/** Draw a frame.
* @param elapsed Time in seconds since the last frame was drawn.
*/
private void draw(GraphicsContext gc, double elapsed, int ballNum) {
gc.setFill(Color.BLUE);
gc.fillOval(ball.get(ballNum).width, ball.get(ballNum).height, 20, 20);
}
/** Set up the window and the timer */
public void start(Stage stage) {
/* Make the JavaFX scene */
Button button = new Button("Add a new Ball");
Button GravButton = new Button("Add more gravity");
Button GravButton2 = new Button("Decrease gravity");
Group root = new Group();
Scene scene = new Scene(root, width, height, Color.ORANGE);
ball.add(ballTemp);
button.setLayoutX(460);
button.setLayoutY(700);
GravButton.setLayoutX(592);
GravButton.setLayoutY(700);
GravButton2.setLayoutX(315);
GravButton2.setLayoutY(700);
//new ball button
button.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent e) {
//create new ball
BallGen ballTemp1 = new BallGen();
ball.add(ballTemp1);
}
});
//gravity increase button
GravButton.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent e) {
GravMult = GravMult * 1.2;
}
});
//gravity decrease button
GravButton2.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent e) {
GravMult = GravMult * 0.8;
}
});
root.getChildren().addAll(canvas, button, GravButton, GravButton2);
stage.setScene(scene);
stage.show();
setup();
/* Set up a timer to update the screen every frame */
new AnimationTimer() {
public void handle(long now) {
if (last == 0) {
last = now;
}
/* Get a context and clear the window */
GraphicsContext gc = canvas.getGraphicsContext2D();
gc.clearRect(0, 0, canvas.getWidth(), canvas.getHeight());
/* Update things */
//updates each ball in arraylist of balls
for(int i = 0; i <= ball.size() - 1; i++) {
//adjusting position of each ball
ball.get(i).HeightAdjust(GravMult);
ball.get(i).WidthAdjust();
for (int x = 0; x <= ball.size() - 1; x ++) {
int tempCheck = ball.get(x).height;
for (int z = 0; z <= ball.size() - 1; z++) { //checking each balls for collisions
if (((tempCheck - ball.get(z).height < 20) && (tempCheck - ball.get(z).height > -20)) && !(x == z)) {
if ((ball.get(x).width - ball.get(z).width < 20) && (ball.get(x).width - ball.get(z).width > -20)) {
//timeout on collision so that the balls can seperate
if (System.nanoTime() - CollisionControl > 50000000) {
ball.get(x).Collision();
ball.get(z).Collision();
CollisionControl = System.nanoTime();
}
else if ((ball.get(x).DownDir == ball.get(z).DownDir) && (ball.get(x).RightDir == ball.get(z).RightDir)) {
//correction of collision if same direction
ball.get(x).Collision();
CollisionControl = System.nanoTime();
}
}
}
}
}
draw(gc, (now - last) * 1e-9, i);
}
last = now;
}
}.start();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment