JAVA 14: DEVELOPMENT OF APPLICATIONS WITH JAVAFX by POUL KLAUSEN

JAVA 14: DEVELOPMENT OF APPLICATIONS WITH JAVAFX by POUL KLAUSEN

Author:POUL KLAUSEN [KLAUSEN, POUL]
Language: eng
Format: azw3
Publisher: UNKNOWN
Published: 2021-01-14T16:00:00+00:00


WITH JAVAFX: SOFTWARE DEVELOPMENT

WITH JAVAFX: SOFTWARE DEVELOPMENT COMPONENTS private Label createLabel() {

Label label = new Label("Enter to start"); label.setLayoutX(20);

label.setLayoutY(20);

return label;

}

public static void main(String[] args) {

launch(args);

}

}

The class defines a variable for a transition, which is the animation that rotates the square. It is created by the method createRotate(), which does not contain anything new in terms of animations. The actual Transition object is created in the method start(). Here you should also notice how to set the background color for the window. The square is created in the method createRect(). Here, the method setOnMouseClicked() is used to attach an event handler for mouse clicks to the square, and you should note how the event handler is defined using a lambda expression.

Then there is the method createField() that creates the entry field. It is a TextField, and the most important is the association of the event handler. It is a filter and the handler is associated for a KeyEvent of the type KEY_TYPED. The handler uses the method getCharacter() to test what has been entered and has the first character the code 13, it is the Enter key that is pressed, and if so, the animation will start. For any event, the event is marked as cosumed so that it is not passed on in the chain, and the effect is that the character for the key pressed is never displayed in the input field.

The example thus shows how to catch events for mouse and keyboard. In the example, it happens in two different ways, where in createField() it takes place at low level with addEventFilter() while in createRect() it occurs using a convenience method. Many of the JavaFX classes defines event handlers as properties, and you can then register an event handler using a set method for that convenience method. The goal is to make it easier to associate event handlers what the following program HandlerProgram should illustrate:

WITH JAVAFX: SOFTWARE DEVELOPMENT componentS

Clicking the Start button starts an animation where the circle moves along the curve and the animation runs until you click the stop button. If you click on the circle, it changes color.

.

WITH JAVAFX: SOFTWARE DEVELOPMENT

WITH JAVAFX: SOFTWARE DEVELOPMENT COMPONENTS public class HandlerProgram extends Application {

private boolean blue = true; private Transition trans;

@Override

public void start(Stage stage) {

Circle circle = createCircle();

Path path = createPath();

trans = createTrans(circle, path);

Group root = new Group(createButton("Start", 20, e -> trans.play()),

createButton("Stop", 120, e -> trans.stop()), path, circle); Scene scene = new Scene(root, 560, 400);

scene.setFill(Color.BEIGE);

stage.setTitle("Move");

stage.setScene(scene);

stage.show();

}

private Button createButton(String text, int pos, EventHandler<ActionEvent> handler)

{

Button cmd = new Button(text);

cmd.setLayoutX(pos);

cmd.setLayoutY(20);

cmd.setPrefSize(80, 25);

cmd.setOnAction(handler);

return cmd;

}

private Transition createTrans(Shape shape, Path path) {

PathTransition trans = new PathTransition();

trans.setDuration(Duration.millis(3000));

trans.setNode(shape);

trans.setPath(path);

trans.setOrientation(PathTransition.OrientationType.ORTHOGONAL_TO_TANGENT); trans.setCycleCount(50);

trans.setAutoReverse(true);

return trans;

}

private Circle createCircle() { Circle circle = new Circle(40, 80, 25, Color.BLUE); circle.setStrokeWidth(20);

circle.setOnMouseClicked(



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.