Animasi dalam Java memerlukan dua langkah asas, mencipta bingkai animasi dan kemudian membenarkan Java mewarnai bingkai tersebut. Applet, AWT, Swing dan JavaFX, boleh melakukan animasi Java. Animasi Applets adalah untuk aplikasi yang serasi dengan penyemak imbas, manakala AWT, Swing dan JavaFX ialah aplikasi kendiri. Dalam masa nyata, kebanyakan aplikasi adalah kendiri sahaja. Jadi, kami akan berurusan dengan animasi kami dengan JavaFX.
IKLAN Kursus Popular dalam kategori ini JAVA MASTERY - Pengkhususan | 78 Siri Kursus | 15 Ujian Olok-olokMulakan Kursus Pembangunan Perisian Percuma Anda
Pembangunan web, bahasa pengaturcaraan, ujian perisian & lain-lain
Mengapa JavaFX Mengapa tidak AWT dan Swing?
AWT mempunyai komponen berat dan Swing tidak mempunyai UI moden. Jadi, kami telah mempertimbangkan animasi JavaFX. Ia adalah komponen UI moden yang ringan dan canggih untuk memudahkan pembangunan kami.
Jenis Animasi dalam JavaFX:
Pakej animasi JavaFX ialah animasi yang mengandungi semua kelas animasi. Jadi, semasa kita menggunakan animasi, kita mesti mengimportnya. Gunakan animasi pada kelas kami; kita mesti memanjangkan kelas Animasi. Kelas Animasi ini mempunyai semua pakej animasi yang diperlukan di dalamnya.
Animasi ini memberikan ciri putaran. Pakejnya ialah animasi.RotateTransition
Sintaks:
RotateTransition rotate = new RotateTransition(); //creating object for Rotate Transition rotate.play(); //applying rotation by using play() method
Animasi ini menggerakkan objek dalam ketiga-tiga arah X, Y dan Z. Pakejnya ialah animasi.ScaleTransition
Sintaks:
ScaleTransition rotate = new ScaleTransition(); //creating object for scale transition rotate.play(); //applying rotation by using play() method
Animasi ini mengalihkan objek dari satu kedudukan ke kedudukan lain pada selang masa yang tetap. Pakejnya ialah animasi.TranslateTransition
Sintaks:
TranslateTransition rotate = new TranslateTransition(); //creating object for Translate transition rotate.play(); //applying rotation by using play() method
Animasi ini menjadikan objek kusam dengan menyatakan nilai kelegapan. Pakejnya ialah animasi.FadeTransition
Sintaks:
FadeTransition rotate = new FadeTransition(); //creating object for fade transition rotate.play(); //applying rotation by using play() method
Animasi ini menjadikan objek diisi dengan 2 warna, satu demi satu, dengan menentukan selang masa. Pakejnya ialah animasi.FillTransition
Sintaks:
FillTransition rotate = new FillTransition(); //creating object for fill transition rotate.play(); //applying rotation by using play() method
Mari kita lihat contoh animasi java diberikan di bawah:
Kod:
package com.rotate.transition; import javafx.animation.RotateTransition; import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.paint.Color; import javafx.scene.shape.Polygon; import javafx.scene.transform.Rotate; import javafx.stage.Stage; import javafx.util.Duration; public class RotateTransitionAnimation extends Application { @Override public void start(Stage outStage) throws Exception { Polygon traingle = new Polygon();// Creating triangle Double[] doubleValues=new Double[] { 5.0, 5.0, 20.0, 10.0, 10.0, 20.0 }; traingle.getPoints().addAll(doubleValues); traingle.setFill(Color.LIMEGREEN); traingle.setStroke(Color.HOTPINK); traingle.setStrokeWidth(5); RotateTransition rotateTransition = new RotateTransition();// Creating object for Rotate Transition class rotateTransition.setAxis(Rotate.Z_AXIS);// Set Axis rotation in Z axis rotateTransition.setByAngle(360);// Set angle rotation 360 degrees rotateTransition.setCycleCount(500);// Set cycle count rotation 500 rotateTransition.setDuration(Duration.millis(1000));// Set time duration for change the object rotateTransition.setAutoReverse(true);//auto reverse activation rotateTransition.setNode(traingle);//applying rotate transition on triangle rotateTransition.play();// applying rotation by play method Group root = new Group(); //creating group for adding elements root.getChildren().add(traingle); //adding triangle to group Scene scene = new Scene(root, 700, 500, Color.BLACK);//creating scene outStage.setScene(scene);//adding scene to stage for display window outStage.setTitle("Triangle Rotate Transition"); outStage.show(); } public static void main(String[] args) { launch(args);//launch method calls start() method internally } }
Output:
Dengan cara ini, segitiga berputar.
Kod:
package com.scale.transition; import javafx.scene.Group; import javafx.stage.Stage; import javafx.util.Duration; import javafx.scene.Scene; import javafx.scene.paint.Color; import javafx.scene.shape.Circle; import javafx.animation.ScaleTransition; import javafx.application.Application; public class ScaleTransitionAnimation extends Application { @Override public void start(Stage stage) { Circle circle = new Circle(); // Creating Circle circle.setCenterX(280.0f);// position in X direction circle.setCenterY(125.0f);// position in Y direction circle.setRadius(40.0f);// circle radius circle.setFill(Color.AQUAMARINE);// circle color circle.setStrokeWidth(21);// stroke width of circle ScaleTransition scaleTransition = new ScaleTransition();// creating // object for // scale // transition scaleTransition.setDuration(Duration.millis(2000));// set time duration scaleTransition.setNode(circle);// applying rotate transition node on // circle scaleTransition.setByY(1.5);// Y direction movement scaleTransition.setByX(1.5);// X direction movement scaleTransition.setCycleCount(55);// Set cycle count rotation 55 scaleTransition.setAutoReverse(true);// auto reverse activation scaleTransition.play();// applying rotate transition on circle Group root = new Group(); // creating group for adding elements root.getChildren().add(circle); // adding triangle to group Scene scene = new Scene(root, 600, 500, Color. AZURE);// creating scene stage.setScene(scene);// adding scene to stage for display window stage.setTitle("Circle Scale Transition"); stage.show(); } public static void main(String args[]) { launch(args); } }
Output:
Dengan cara ini, bulatan berskala.
Kod:
package com.translate.transition; import javafx.stage.Stage; import javafx.util.Duration; import javafx.scene.Scene; import javafx.scene.paint.Color; import javafx.scene.shape.Rectangle; import javafx.animation.TranslateTransition; import javafx.application.Application; import javafx.scene.Group; public class TranslateTransitionAnimation extends Application { @Override public void start(Stage outStage) throws Exception { Rectangle square = new Rectangle(50, 50); // Creating square square.setFill(Color.AQUA); // square border color square.setStroke(Color.BLUEVIOLET);// square area color TranslateTransition translateTranstion = new TranslateTransition();// creating object for Translate transition translateTranstion.setByY(350);// movement in Y direction translateTranstion.setDuration(Duration.millis(1500));// time duration translateTranstion.setCycleCount(450);// Set cycle count rotation 450 translateTranstion.setAutoReverse(true);// auto reverse activation translateTranstion.setNode(square);// applying rotate transition node on square translateTranstion.play();// applying rotate transition on circle Group root = new Group(); // creating group for adding elements root.getChildren().add(square); // adding square to group Scene scene = new Scene(root, 600, 500, Color.CHOCOLATE);// creating scene outStage.setScene(scene);// adding scene to stage for display window outStage.setTitle("Square Translate Transition"); outStage.show(); } public static void main(String[] args) { launch(args); } }
Output:
Beginilah cara membuat pergerakan peralihan skala kuasa dua.
Kod:
package com.fade.transition; import javafx.animation.FadeTransition; import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.paint.Color; import javafx.scene.shape.Ellipse; import javafx.scene.shape.Rectangle; import javafx.stage.Stage; import javafx.util.Duration; public class FadeTransitionAnimation extends Application { @Override public void start(Stage outStage) throws Exception { Ellipse ellipse = new Ellipse(); // Creating Ellipse object ellipse.setCenterX(300.0f); //setting ellipse center distance in X direction ellipse.setCenterY(150.0f); //setting ellipse center distance in Y direction ellipse.setRadiusX(150.0f); //setting radius in X direction ellipse.setRadiusY(75.0f);//setting radius in y direction ellipse.setFill(Color.AQUA); // ellipse border color ellipse.setStroke(Color.BLUEVIOLET);// ellipse area color FadeTransition fadeTransition = new FadeTransition();// creating Fade transition object fadeTransition.setDuration(Duration.millis(5000));// time duration fadeTransition.setFromValue(10);//setting opacity value for fading fadeTransition.setToValue(0.1); fadeTransition.setCycleCount(900);// Set cycle count rotation 900 fadeTransition.setAutoReverse(true);// auto reverse activation fadeTransition.setNode(ellipse);// applying fade transition node on ellipse fadeTransition.play();// applying fade transition on ellipse Group root = new Group(); // creating group for adding elements root.getChildren().add(ellipse); // adding ellipse to group Scene scene = new Scene(root, 600, 500, Color.CHOCOLATE);// creating scene outStage.setScene(scene);// adding scene to stage for display window outStage.setTitle("Ellipse Fade Transition"); outStage.show(); } public static void main(String[] args) { launch(args); } }
Output:
Dengan cara ini, peralihan pudar berlaku.
Atas ialah kandungan terperinci Animasi Java. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!