From 73c3e5577bf11870e404a2f21402b61eaf776758 Mon Sep 17 00:00:00 2001 From: Pedro Rolon Date: Wed, 7 Nov 2018 13:04:17 -0300 Subject: [PATCH] Se agregaron botones y control por teclado --- Something.java | 156 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 152 insertions(+), 4 deletions(-) diff --git a/Something.java b/Something.java index e5f326a..eda903b 100644 --- a/Something.java +++ b/Something.java @@ -10,6 +10,11 @@ import java.io.FileInputStream; import java.io.FileNotFoundException; import javafx.scene.image.Image; import javafx.scene.image.ImageView; +import javafx.scene.control.Button; +import javafx.event.ActionEvent; +import javafx.event.EventHandler; +import javafx.scene.input.KeyEvent; +import javafx.scene.input.KeyCode; //import Bicicleta; public class Something extends Application @@ -22,14 +27,15 @@ public class Something extends Application @Override public void start(Stage stage) { - Canvas canvas = new Canvas(400, 400); + double distancia = 10; + Canvas canvas = new Canvas(700, 700); GraphicsContext gc = canvas.getGraphicsContext2D(); gc.setLineWidth(2.0); Bicicleta bicicleta = new Bicicleta(); - + /* mover(gc, bicicleta, 50); mover(gc, bicicleta, 50, 1.5708); mover(gc, bicicleta, 50, 0); @@ -40,6 +46,7 @@ public class Something extends Application mover(gc, bicicleta, 25, 4.71239); mover(gc, bicicleta, 100, 0.523599); mover(gc, bicicleta, 100, 5.23599); + */ @@ -63,19 +70,160 @@ public class Something extends Application System.out.println(e.getMessage()); } + //Crea los botones + Button boton1 = new Button(); + Button boton2 = new Button(); + Button boton3 = new Button(); + Button boton4 = new Button(); + Button boton5 = new Button(); + Button boton6 = new Button(); + Button boton7 = new Button(); + Button boton8 = new Button(); + + + boton1.setText("NE"); + boton1.setPrefSize(40, 40); + boton1.setLayoutX(0); + boton1.setLayoutY(0); + boton1.setOnAction(new EventHandler() { + @Override + public void handle(ActionEvent event) { + mover(gc, bicicleta, distancia, 2.09439510238807); + } + }); + + boton2.setText("N"); + boton2.setPrefSize(40, 40); + boton2.setLayoutX(50); + boton2.setLayoutY(0); + boton2.setOnAction(new EventHandler() { + @Override + public void handle(ActionEvent event) { + mover(gc, bicicleta, distancia, 1.570796326791001); + } + }); + + boton3.setText("NO"); + boton3.setPrefSize(40, 40); + boton3.setLayoutX(100); + boton3.setLayoutY(0); + boton3.setOnAction(new EventHandler() { + @Override + public void handle(ActionEvent event) { + mover(gc, bicicleta, distancia, 0.523598775597001); + } + }); + + boton4.setText("E"); + boton4.setPrefSize(40, 40); + boton4.setLayoutX(0); + boton4.setLayoutY(50); + boton4.setOnAction(new EventHandler() { + @Override + public void handle(ActionEvent event) { + mover(gc, bicicleta, distancia, 3.141592653582001); + } + }); + + boton5.setText("O"); + boton5.setPrefSize(40, 40); + boton5.setLayoutX(100); + boton5.setLayoutY(50); + boton5.setOnAction(new EventHandler() { + @Override + public void handle(ActionEvent event) { + mover(gc, bicicleta, distancia, 0); + } + }); + + boton6.setText("SE"); + boton6.setPrefSize(40, 40); + boton6.setLayoutX(0); + boton6.setLayoutY(100); + boton6.setOnAction(new EventHandler() { + @Override + public void handle(ActionEvent event) { + mover(gc, bicicleta, distancia, 3.665191429179); + } + }); + + boton7.setText("S"); + boton7.setPrefSize(40, 40); + boton7.setLayoutX(50); + boton7.setLayoutY(100); + boton7.setOnAction(new EventHandler() { + @Override + public void handle(ActionEvent event) { + mover(gc, bicicleta, distancia, 4.712388980373); + } + }); + + boton8.setText("SO"); + boton8.setPrefSize(40, 40); + boton8.setLayoutX(100); + boton8.setLayoutY(100); + boton8.setOnAction(new EventHandler() { + @Override + public void handle(ActionEvent event) { + mover(gc, bicicleta, distancia, 5.23598775597); + } + }); + Pane root = new Pane(); - root.setStyle("-fx-padding: 10;" + - "-fx-border-style: solid inside;" + + root.setStyle("-fx-border-style: solid inside;" + "-fx-border-width: 2;" + "-fx-border-insets: 5;" + "-fx-border-radius: 5;" + "-fx-border-color: blue;"); root.getChildren().add(canvas); + root.getChildren().add(boton1); + root.getChildren().add(boton2); + root.getChildren().add(boton3); + root.getChildren().add(boton4); + root.getChildren().add(boton5); + root.getChildren().add(boton6); + root.getChildren().add(boton7); + root.getChildren().add(boton8); + Scene scene = new Scene(root); + scene.setOnKeyPressed(e -> { + if (e.getCode() == KeyCode.NUMPAD1) { + mover(gc, bicicleta, distancia, 3.665191429179); + } + else if (e.getCode() == KeyCode.NUMPAD2) { + mover(gc, bicicleta, distancia, 4.712388980373); + } + else if (e.getCode() == KeyCode.NUMPAD3) { + mover(gc, bicicleta, distancia, 5.23598775597); + } + else if (e.getCode() == KeyCode.NUMPAD4) { + mover(gc, bicicleta, distancia, 3.141592653582001); + } + else if (e.getCode() == KeyCode.NUMPAD6) { + mover(gc, bicicleta, distancia, 0); + } + else if (e.getCode() == KeyCode.NUMPAD7) { + mover(gc, bicicleta, distancia, 2.09439510238807); + } + else if (e.getCode() == KeyCode.NUMPAD8) { + mover(gc, bicicleta, distancia, 1.570796326791001); + } + else if (e.getCode() == KeyCode.NUMPAD9) { + mover(gc, bicicleta, distancia, 0.523598775597001); + } + else if (e.getCode() == KeyCode.PLUS) { + //distancia++; + } + else if (e.getCode() == KeyCode.MINUS) { + //distancia--; + } + + }); + stage.setScene(scene); stage.setTitle("Óyeme Carlos llévame en tu Bicicleta"); stage.show(); -- libgit2 0.26.0