Circle_Move_KeyEvent1 |
|
![]() |
Code:package javafxtemplate1; //import javafx.animation.TranslateTransition; import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.event.EventType; import javafx.scene.*; import javafx.scene.control.Label; import javafx.scene.input.*; import javafx.scene.layout.StackPane; import javafx.scene.paint.Color; import javafx.scene.shape.Circle; import javafx.stage.Stage; import javafx.util.Duration; //port ExternalClass1; /** * * @author Manas14 */ public class JavaFXTemplate1 extends Application { private static final double deltaX = 5; String str1 ="", str2=""; double orgSceneX, orgSceneY; double orgTranslateX, orgTranslateY; @Override public void start(Stage primaryStage) { // final Circle circle = createCircle(); Label label1 = new Label(str1); label1.setLayoutX(70); label1.setLayoutY(20); //circle constructor //setCenterX, setCenterY, setRadius Circle circle1 = new Circle(200, 150, 50, Color.GREEN); //final Group group = new Group(createInstructions(), circle); Group root = new Group(label1, circle1); Scene scene = new Scene(root, 350, 200, Color.CORNSILK); // Create a MouseEvent filter // Create two EventHandlders moveShapeOnKeyPress(scene, circle1, label1); // primaryStage.setTitle("Application"); primaryStage.setScene(scene); primaryStage.show(); } private void moveShapeOnKeyPress(Scene scene, final Circle scircle, Label label1) { scene.setOnKeyPressed(new EventHandler |
Runtime view : Here scene tenders an event to move children(scircle) with key press. Top^ arrow key pressed (UP) Left < arrow key pressed Right > Arrow key pressed Down \/ Arrow Key
|