Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA create a new Java class, Ant, that draws the path of an ant that is walking

ID: 2247304 • Letter: J

Question

JAVA create a new Java class, Ant, that draws the path of an ant that is walking randomly around a JavaFX window. This should look a little like a scribbling on a page. If the ant walks off the edge of the screen re-center the ant and continue the random walk. Use the basic template from the code from lecture X01. In addition to that, you may find the following helpful:

...
primaryStage.setScene(scene);

Timeline timeline = new Timeline(new KeyFrame(Duration.millis(100),
                            ae -> {
                                   /* your code goes here */
                                  }));
timeline.setCycleCount(Animation.INDEFINITE);
timeline.play();
primaryStage.show();
...

lecture

public class JavaFXSquare extends javafx.application.Application @override public void start(Stage primaryStage) throws Exception f primaryStage.setTitle ("Sqaure"); Group root new Group); Scene scene = new Scene (root, width 300, height: 300); primaryStage.setscene(scene) height: 100); Rectangle r = new Rectangle( r.setFill(color.RED); root.getChildren ( ) .add(r);1 x: 100, y: 100, width: 100, primaryStage. show);

Explanation / Answer

hi..

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.animation.AnimationTimer;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.effect.Lighting;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.text.Text;
import javafx.util.Duration;

public class TimelineEvents extends Application {
  
//main timeline
private Timeline timeline;
private AnimationTimer timer;

//variable for storing actual frame
private Integer i=0;

@Override public void start(Stage stage) {
Group p = new Group();
Scene scene = new Scene(p);
stage.setScene(scene);
stage.setWidth(500);
stage.setHeight(500);
p.setTranslateX(80);
p.setTranslateY(80);

//create a circle with effect
final Circle circle = new Circle(20, Color.rgb(156,216,255));
circle.setEffect(new Lighting());
//create a text inside a circle
final Text text = new Text (i.toString());
text.setStroke(Color.BLACK);
//create a layout for circle with text inside
final StackPane stack = new StackPane();
stack.getChildren().addAll(circle, text);
stack.setLayoutX(30);
stack.setLayoutY(30);

p.getChildren().add(stack);
stage.show();

//create a timeline for moving the circle
timeline = new Timeline();
timeline.setCycleCount(Timeline.INDEFINITE);
timeline.setAutoReverse(true);

//You can add a specific action when each frame is started.
timer = new AnimationTimer() {
@Override
public void handle(long l) {
text.setText(i.toString());
i++;
}

};

//create a keyValue with factory: scaling the circle 2times
KeyValue keyValueX = new KeyValue(stack.scaleXProperty(), 2);
KeyValue keyValueY = new KeyValue(stack.scaleYProperty(), 2);

//create a keyFrame, the keyValue is reached at time 2s
Duration duration = Duration.millis(2000);
//one can add a specific action when the keyframe is reached
EventHandler EventHandler<ActionEvent>() {
public void handle(ActionEvent t) {
stack.setTranslateX(java.lang.Math.random()*200-100);
//reset counter
i = 0;
}
};

KeyFrame keyFrame = new KeyFrame(duration, onFinished , keyValueX, keyValueY);

//add the keyframe to the timeline
timeline.getKeyFrames().add(keyFrame);

timeline.play();
timer.start();
}
  
  
public static void main(String[] args) {
Application.launch(args);
}
  

Thanks