Commit d3a0565d authored by ryanw's avatar ryanw

upload of bouncing balls assessment

parents
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-13">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.fx.ide.jdt.core.JAVAFX_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/JavaFX"/>
<classpathentry kind="output" path="bin"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Bounce_Prog</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.xtext.ui.shared.xtextBuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.xtext.ui.shared.xtextNature</nature>
<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=13
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=13
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.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
org.eclipse.jdt.core.compiler.release=enabled
org.eclipse.jdt.core.compiler.source=13
<?xml version="1.0" encoding="ASCII"?>
<anttasks:AntTask xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:anttasks="http://org.eclipse.fx.ide.jdt/1.0" buildDirectory="${project}/build">
<deploy>
<application name="Bounce_Prog"/>
<info/>
</deploy>
<signjar/>
</anttasks:AntTask>
package application;
import java.awt.Color;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.util.Duration;
public class BounceMainRun extends Application
{
public static void main(String[] args)
{
launch(args);
}
private int width = 600;
private int height = 500;
private int ballSize = 15;
@Override
public void start(Stage primaryStage) throws Exception
{
Group root = new Group();
//creating 10 balls from the more balls class
for (int i = 0; i < 10; i++)
{
MoreBalls.balls.add(new MoreBalls(ballSize, width, height, MoreBalls.balls, Color.RED));
}
root.getChildren().addAll(MoreBalls.balls);
Scene scene = new Scene(root, width, height);
primaryStage.setTitle("Multi Bouncing Balls");
primaryStage.setScene(scene);
primaryStage.show();
KeyFrame kFrame = new KeyFrame(Duration.millis(10),
e ->
{
for(MoreBalls ball : MoreBalls.balls)
{
ball.move();
}
});
Timeline time = new Timeline(kFrame);
time.setCycleCount(Timeline.INDEFINITE);
time.play();
}
}
\ No newline at end of file
package application;
import java.awt.Color;
import java.util.ArrayList;
import java.util.List;
import javafx.scene.paint.CycleMethod;
import javafx.scene.paint.LinearGradient;
import javafx.scene.paint.RadialGradient;
import javafx.scene.paint.Stop;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Shape;
public class MoreBalls extends Circle
{
private double dx;
private double dy;
private double radius;
private double areaWidth;
private double areaHeight;
private Color colour;
public static ArrayList<MoreBalls> balls = new ArrayList<MoreBalls>();
//constructor for making new balls
public MoreBalls(double radius, double areaWidth, double areaHeight, ArrayList<MoreBalls> balls, Color colour) {
super();
this.radius = radius;
this.areaWidth = areaWidth;
this.areaHeight = areaHeight;
this.balls = balls;
this.colour = colour;
super.setRadius(radius);
super.setCenterX(Math.random() * (areaWidth - this.radius) + 1);
super.setCenterY(Math.random() * (areaHeight - this.radius) + 1);
dx = Math.random() * 5 + 1;
dy = Math.random() * 5 + 1;
//doesn't work Stop not recognised **look at more
// RadialGradient gradient = new RadialGradient(0, 0, 0.35, 0.35, 0.5, true, CycleMethod.NO_CYCLE,
// new Stop(0.0, Color.WHITE), new Stop(1, Color.RED));
// RadialGradient gradient = new RadialGradient(0, 0,
// 0.35, 0.35,
// 0.5,
// true,
// CycleMethod.NO_CYCLE,
// new Stop(0, Color.WHITE),
// new Stop(1, Color.RED));
// super.setFill(gradient);
}
//setting ball movement and wall and ball collision
public void move()
{
super.setCenterX(super.getCenterX() + dx);
super.setCenterY(super.getCenterY() + dy);
if (super.getCenterX() <= radius)
{
super.setCenterX(radius);
dx = - dx;
}
if (super.getCenterX() >= areaWidth - radius)
{
super.setCenterX(areaWidth - radius);
dx = - dx;
}
if (super.getCenterY() <= radius)
{
super.setCenterY(radius);
dy = -dy;
}
if (super.getCenterY() >= areaHeight - radius)
{
super.setCenterY(areaHeight - radius);
dy = -dy;
}
for (MoreBalls b : balls)
{
if (b != this && b.intersects(super.getLayoutBounds()))
{
double tempx = dx;
double tempy = dy;
dx = b.dx;
dy = b.dy;
b.dx = tempx;
b.dy = tempy;
break;
}
}
}
}
/* JavaFX CSS - Leave this comment until you have at least create one rule which uses -fx-Property */
\ No newline at end of file
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