0
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import javafx.scene.control.*;
import javafx.scene.shape.*;
import javafx.scene.paint.Color;
import javafx.animation.*;
import static javafx.application.Application.launch;
import javafx.util.Duration;

public class CarsRacing extends Application 
{
  public static void main(String[] args) 
  {
    launch(args);
  }
  public void start(Stage primaryStage) 
  {
    Pane pane = new Pane();
    
    Button btn = new Button("Start Race"); ``` creating button with lable "Start Race"
Polygon p1 = new Polygon();
Polygon p2 = new Polygon();
Polygon p3 = new Polygon();
    
Circle wheel1 = new Circle(90,110,10);
Circle wheel2 = new Circle(140,110,10);
Circle wheel3 = new Circle(90,220,10);
Circle wheel4 = new Circle(140,220,10);
Circle wheel5 = new Circle(90,320,10);
Circle wheel6 = new Circle(140,320,10);

Line l1 = new Line(50,140,1050, 140);
Line l2 = new Line(50,240,1050, 240);
Line l3 = new Line(50,340,1050, 340);

p1.getPoints().addAll
(
 new Double[]
 {        
    50.0,110.0,
    50.0,80.0,
    80.0,80.0,
    100.0,60.0,
    130.0,60.0,
    150.0,80.0,
    180.0,80.0,
    180.0,110.0,
 }
);
p2.getPoints().addAll
(
 new Double[]
 {        
    50.0,220.0,
    50.0,190.0,
    80.0,190.0,
    100.0,170.0,
    130.0,170.0,
    150.0,190.0,
    180.0,190.0,
    180.0,220.0,
 }
);

p3.getPoints().addAll
(
 new Double[]
 {        
    50.0,320.0,
    50.0,290.0,
    80.0,290.0,
    100.0,270.0,
    130.0,270.0,
    150.0,290.0,
    180.0,290.0,
    180.0,320.0,
 }
);

p1.setFill(Color.ORANGE);
p1.setStroke(Color.BLUE);
p2.setFill(Color.GREEN);
p2.setStroke(Color.RED);
p3.setFill(Color.BLUE);
p3.setStroke(Color.ORANGE);

wheel1.setFill(Color.BLACK);
wheel2.setFill(Color.BLACK);
wheel3.setFill(Color.BLACK);
wheel4.setFill(Color.BLACK);
wheel5.setFill(Color.BLACK);
wheel6.setFill(Color.BLACK);

Path path1 = new Path();
Path path2 = new Path();
Path path3 = new Path();
Path path4 = new Path();                    
Path path5 = new Path();
Path path6 = new Path();

Path path7 = new Path();                    
Path path8 = new Path();
Path path9 = new Path();

path1.getElements().addAll(new MoveTo( 90, 110), new HLineTo(950));
path2.getElements().addAll(new MoveTo(140, 110), new HLineTo(1000));
path3.getElements().addAll(new MoveTo( 90, 220), new HLineTo(950));
path4.getElements().addAll(new MoveTo(140, 220), new HLineTo(1000));
path5.getElements().addAll(new MoveTo( 90, 320), new HLineTo(950));
path6.getElements().addAll(new MoveTo(140, 320), new HLineTo(1000));

path7.getElements().addAll(new MoveTo(  115, 85), new HLineTo(970));
path8.getElements().addAll(new MoveTo(  115, 195), new HLineTo(970));
path9.getElements().addAll(new MoveTo(  115, 295), new HLineTo(970));

PathTransition pt1 = new PathTransition(Duration.millis(4000), path1, wheel1);
PathTransition pt2 = new PathTransition(Duration.millis(3970), path2, wheel2);
PathTransition pt3 = new PathTransition(Duration.millis(3000), path3, wheel3);
PathTransition pt4 = new PathTransition(Duration.millis(2970), path4, wheel4);  
PathTransition pt5 = new PathTransition(Duration.millis(5000), path5, wheel5);
PathTransition pt6 = new PathTransition(Duration.millis(4970), path6, wheel6);

PathTransition pt7 = new PathTransition(Duration.millis(4000), path7, p1);
PathTransition pt8 = new PathTransition(Duration.millis(3000), path8, p2);
PathTransition pt9 = new PathTransition(Duration.millis(5000), path9, p3);

btn.setOnAction```

//Instead of using this button to start the event how can i make it start with pressing enter on the keyboard

    (
      e -> 
      {
         pt1.play();
         pt2.play();
         pt3.play();
         pt4.play();
         pt5.play();
         pt6.play();
         pt7.play();
         pt8.play();
         pt9.play();
      }
    );
    
    pane.getChildren().addAll(btn, p1, p2, p3, wheel1, wheel2, wheel3, wheel4, wheel5, wheel6, l1, l2, l3);
``` adding the button to the pane```
    
    Scene scene = new Scene(pane, 1100, 370);
    primaryStage.setTitle("Cars Racing"); 
    primaryStage.setScene(scene); 
    primaryStage.show(); 
  }
  
}```


James_D
  • 201,275
  • 16
  • 291
  • 322
tof-tof
  • 11
  • 2
    You need add a event listener in your button, and later indicate what event you want listen, in your case only the evento produced by enter key. https://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html – Adrian Lagartera Oct 26 '21 at 14:42
  • 2
    `scene.setOnKeyPressed(...)`? – James_D Oct 26 '21 at 14:43

1 Answers1

0

Make your button a default button:

 button.setDefaultButton(true);

From the javadoc:

A default Button is the button that receives a keyboard VK_ENTER press, if no other node in the scene consumes it.

Alternately see:

Which provides this solution:

scene.setOnKeyPressed( event -> {
  if( event.getCode() == KeyCode.ENTER ) {
    doSomething();
  }
} );

If you want to intercept enter when something else has focus you can use an event filter instead of the set on key pressed.

jewelsea
  • 150,031
  • 14
  • 366
  • 406