Download source: robotsrc1.zip or JAR file RobotTest2.jar
The robot starts at the center and heads off to explore, only to be stopped at the window edge.
RobotTest2.java
See Robot object code.
import javafx.animation.Animation; import javafx.application.Application; import javafx.stage.Stage; import javafx.animation.KeyFrame; import javafx.animation.Timeline; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.Group; import javafx.scene.Node; import javafx.scene.shape.*; import javafx.scene.layout.Pane; import javafx.scene.layout.BorderPane; import javafx.scene.control.Label; import javafx.scene.paint.Color; import javafx.util.Duration; import javafx.collections.*; import java.util.*; class TestPanel extends Pane { int wait; double wd, ht; Robot robot; double simtime = 0.0; int stage = 0; Label status; TestPanel() { Path p = new Path(); ObservableList<PathElement> pe = p.getElements(); for (int n=0; n<9; n++) { pe.add(new MoveTo(10,10+n*50)); pe.add(new LineTo(410,10+n*50)); pe.add(new MoveTo(10+n*50,10)); pe.add(new LineTo(10+n*50,410)); } p.setStroke(Color.RED); getChildren().add(p); robot = new Robot(210,210,24,22,Color.rgb(180,180,255)); robot.draw(this); } public void showVelocity() { status.setText(" " + robot.showVelocity()); } public void update() { wd = getWidth(); ht = getHeight(); double tstep = 0.05; double tmin = 1.0; robot.updateVelocity(); double t = robot.intersect_window(wd,ht); if (t<tmin) tmin = t; if (tmin<=tstep) { robot.move(tmin); robot.setVelocity(0,0); showVelocity(); } else robot.move(tstep); simtime += tstep; } } public class RobotTest2 extends Application { @Override // Override the start method in the Application class public void start(Stage primaryStage) { // Create a pane TestPanel pane = new TestPanel(); // Create a handler for animation EventHandler<ActionEvent> eventHandler = e -> { pane.update(); }; // Create an animation Timeline animation = new Timeline( new KeyFrame(Duration.millis(50), eventHandler)); animation.setCycleCount(Timeline.INDEFINITE); animation.play(); // Start animation BorderPane layout = new BorderPane(); Label label = new Label(); pane.status = label; layout.setCenter(pane); layout.setBottom(label); // Create a scene and place it in the stage Scene scene = new Scene(layout, 440, 460); primaryStage.setTitle("robot test 2"); // Set the stage title primaryStage.setScene(scene); // Place the scene in the stage primaryStage.show(); // Display the stage } public static void main(String args[] ) { launch(args); } }
Maintained by John Loomis, updated Tue Feb 20 17:41:48 2018