Commit ec715dcb authored by User's avatar User

This is the bouncing balls work

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-11">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Bouncing Balls</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=11
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=11
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.release=enabled
org.eclipse.jdt.core.compiler.source=11
import javafx.application.*;
import javafx.stage.*;
import javafx.scene.*;
import javafx.scene.paint.*;
import javafx.scene.canvas.*;
import javafx.scene.shape.*;
import javafx.animation.*;
import java.util.*;
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;
int xspeed = 1;
int x =0;
int yspeed = 1;
int y =0;
/** 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 x, int y) {
gc.setFill(Color.BLUE);
gc.fillOval(x, y, 20,20);
gc.setFill(Color.RED);
gc.fillOval(x+100, y+100, 20,20);
}
/** Set up the window and the timer */
public void start(Stage stage) {
/* Make the JavaFX scene */
Group root = new Group();
Scene scene = new Scene(root, width, height, Color.WHITE);
root.getChildren().add(canvas);
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;
}
x+=xspeed;
y+=yspeed;
if(width > x) {
y-=yspeed;
}
else {
if(height>y) {
x-=xspeed;
}
}
if(height < x) {
y+=yspeed;
}
else
{
if(width < y) {
x+=xspeed;
}
}
/* Get a context and clear the window */
GraphicsContext gc = canvas.getGraphicsContext2D();
gc.clearRect(0, 0, canvas.getWidth(), canvas.getHeight());
/* Update things */
draw(gc, (now + last) * 1e-9, x ,y);
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