首頁 > Java > java教程 > 主體

Java動畫

WBOY
發布: 2024-08-30 16:16:40
原創
956 人瀏覽過

Java 中的動畫需要兩個基本步驟,建立動畫幀,然後允許 Java 為該幀著色。 Applet、AWT、Swing 和 JavaFX 都可以做 Java 動畫。 Applet 動畫適用於瀏覽器相容的應用程序,而 AWT、Swing 和 JavaFX 是獨立的應用程式。實際上,大多數應用程式都是獨立的。因此,我們將使用 JavaFX 處理我們的動畫。

廣告 該類別中的熱門課程 JAVA 掌握 - 專業化 | 78 課程系列 | 15 次模擬測驗

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

為什麼選擇 JavaFX 為什麼不選 AWT 和 Swing?

AWT 擁有重量級元件,而 Swing 沒有現代 UI。因此,我們考慮了 JavaFX 動畫。它是輕量級且先進的現代 UI 元件,使我們的開發更加輕鬆。

JavaFX 中的動畫類型:

  1. 旋轉過渡
  2. 尺度轉換
  3. 翻譯過渡
  4. 淡入淡出過渡
  5. 填充過渡
  6. 筆畫過渡
  7. 順序轉換
  8. 並行過渡
  9. 暫停過渡
  10. 路徑轉換

JavaFX 動畫在 Java 中如何運作?

JavaFX動畫包是一個包含所有動畫類別的動畫。因此,當我們應用動畫時,我們必須導入它們。將動畫應用到我們的班級;我們必須擴展動畫類別。此動畫類別包含所有必要的動畫包。

1.旋轉過渡

這個動畫提供了旋轉功能。包是animation.RotateTransition

文法:

RotateTransition rotate = new RotateTransition();  //creating object for Rotate Transition
rotate.play();  //applying rotation by using play() method
登入後複製

2.尺度轉換

這個動畫使物體在X、Y、Z三個方向上移動。封裝為animation.ScaleTransition

文法:

ScaleTransition rotate = new ScaleTransition();  //creating object for scale transition
rotate.play();  //applying rotation by using play() method
登入後複製

3.翻譯過渡

此動畫以固定的時間間隔將物件從一個位置移動到另一個位置。包是animation.TranslateTransition

文法:

TranslateTransition rotate = new TranslateTransition();  //creating object for Translate transition
rotate.play();  //applying rotation by using play() method
登入後複製

4.淡入淡出過渡

此動畫透過指定不透明度值而使物件變得暗淡。包是animation.FadeTransition

文法:

FadeTransition rotate = new FadeTransition();  //creating object for fade transition
rotate.play();  //applying rotation by using play() method
登入後複製

5.填充過渡

此動畫透過指定時間間隔使物件依序填滿兩種顏色。封裝的是animation.FillTransition

文法:

FillTransition rotate = new FillTransition();  //creating object for fill transition
rotate.play();  //applying rotation by using play() method
登入後複製

範例

讓我們看看下面給出的java動畫範例:

範例 #1 – 旋轉過渡

代碼:

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
}
}
登入後複製

輸出:

Java動畫

Java動畫

這樣,三角形就旋轉了。

範例 #2 – 尺度轉換

代碼:

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);
}
}
登入後複製

輸出:

Java動畫

Java動畫

這樣,圓就會縮放。

範例 #3 – 翻譯轉換

代碼:

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);
}
}
登入後複製

輸出:

Java動畫

Java動畫

這是如何平方縮放過渡移動。

範例 #4 – 淡入淡出過渡

代碼:

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);
}
}
登入後複製

輸出:

Java動畫

Java動畫

這樣,淡入淡出過渡就發生了。

以上是Java動畫的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!