簡體   English   中英

javaFx中的動畫

[英]animation in javaFx

我正在處理一個與 javaFx 中的動畫有關的項目,我為它編寫了 GUI,它是一個 Stick Figure。 我試圖讓簡筆畫走到窗格的右側,然后當它接觸到右側時,轉而從另一邊一直回到窗格的左牆。 我有這個代碼是一個移動的球,它完全符合我想讓我的簡筆畫做的事情,但我似乎無法將該代碼修改到我的簡筆畫程序中。 關於如何做到這一點的任何想法? 兩個代碼都在下面:

    package animationdemo;

    import javafx.animation.KeyFrame;
    import javafx.animation.Timeline;
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.layout.Pane;
    import javafx.scene.paint.Color;
    import javafx.scene.shape.Circle;
    import javafx.stage.Stage;
    import javafx.util.Duration;

    public class MovingBallDemo_3 extends Application {

@Override
public void start(Stage primaryStage) {

    BallPane ballPane = new BallPane(); // Create a ball pane
    // Pause and resume animation
    ballPane.setOnMousePressed(e -> ballPane.pause());
    ballPane.setOnMouseReleased(e -> ballPane.play());
    // Create a scene and place it in the stage
    Scene scene = new Scene(ballPane, 250, 150);
    primaryStage.setTitle("Bouncing Ball Control"); // Set the stage title
    primaryStage.setScene(scene); // Place the scene in the stage
    primaryStage.show(); // Display the stage
}

public static void main(String[] args) {
    launch(args);
  }
}

 class BallPane extends Pane {

public final double radius = 20;
private double x = 2 * radius, y = 3 * radius;
private double dx = 3; // Number of pixels to move each time
private Circle circle = new Circle(x, y, radius);
private Timeline animation;

public BallPane() {
    circle.setFill(Color.RED); // Set ball color
    getChildren().add(circle); // Place a ball into this pane
    // Create the animation for 25 millisecond events
    animation = new Timeline(new KeyFrame(Duration.millis(25), e -> moveBall()));
    animation.setCycleCount(Timeline.INDEFINITE);
    animation.play(); // Start animation
}

public void play() {
    animation.play();
}

public void pause() {
    animation.pause();
}
// Move the ball. When a wall is encountered, reverse direction
protected void moveBall() {
    if (x <= radius || x >= getWidth() - radius) {
        dx *= -1; // Change direction
    }
    // Adjust ball position
    x += dx;
    circle.setCenterX(x);
}
}

上面是我想讓簡筆圖 GUI 做的動畫。 這是我嘗試將我的代碼與上面的代碼合並以使棒圖工作但沒有任何反應:

     package Stickfigure;

     import java.awt.Graphics;
     import javafx.animation.KeyFrame;
     import javafx.animation.PathTransition;
     import javafx.animation.Timeline;
     import javafx.application.Application;
     import javafx.event.ActionEvent;
     import javafx.event.EventHandler;
     import javafx.scene.Scene;
     import javafx.scene.control.Button;
     import javafx.scene.layout.Pane;
     import javafx.scene.layout.StackPane;
     import javafx.scene.paint.Color;
     import javafx.scene.shape.Arc;
     import javafx.scene.shape.ArcType;
     import javafx.scene.shape.Circle;
     import javafx.scene.shape.Line;
     import javafx.stage.Stage;
     import javafx.util.Duration;


     public class Stickfigure extends Application {

      @Override
     public void start(Stage primaryStage) {
      BallPane ballPane = new BallPane();
      ballPane.setOnMousePressed(e -> ballPane.pause());
      ballPane.setOnMouseReleased(e -> ballPane.play());

    Circle circle = new Circle(100, 100, 0);//head
    Circle circle1 = new Circle(120, 80, 50);//eye
    circle1.setRadius(5);//radius of eye
    circle.setRadius(50);//radius of head
    circle.setStroke(Color.BLACK);//circle color
    circle1.setStroke(Color.BLACK);//circle color
    circle.setFill(null);//makes the head empty(no brain haha)
    circle.setStrokeWidth(5);//sets the line thickness of circle (head)

    Arc arc = new Arc();//mouth
    arc.setCenterX(110.0f);//mouth position
    arc.setCenterY(120.0f);//mouth position
    arc.setRadiusX(35.0f);//mouth size
    arc.setRadiusY(25.0f);//mouth size
    arc.setStartAngle(1.0f);//angle of mouth
    arc.setLength(5.0f);//length of mouth
    arc.setType(ArcType.ROUND);

    Line line1 = new Line(100, 250, 100, 150); //body of stick figure
    Line line2 = new Line(); //left leg
    Line line3 = new Line();//right leg
    Line line4 = new Line();//right arm
    Line line5 = new Line();//left arm

    line2.setStartX(30.0f); //left leg starting position y
    line2.setStartY(350.0f);//left leg starting position y
    line2.setEndX(100.0f);//left leg end pos x
    line2.setEndY(250.0f);//left leg end pos y

    line3.setStartX(200.0f); //right leg start pos x
    line3.setStartY(350.0f);// right leg start pos y
    line3.setEndX(100.0f); //right leg end pos x
    line3.setEndY(250.0f); //right leg end pos y

    line4.setStartX(100.0f);//right arm start pos x
    line4.setStartY(200.0f); //right arm start pos y
    line4.setEndX(200.0f); //right arm end pos x
    line4.setEndY(170.0f); //right arm end pos y

    line5.setStartX(30.0f);//left arm arm statt pos x
    line5.setStartY(250.0f); // left arm start pos y
    line5.setEndX(100.0f);//left arm end pos x
    line5.setEndY(200.0f);//left arm end pos y

    line1.setStrokeWidth(5); //thickness of line
    line1.setStroke(Color.BLACK);//color of line
    line2.setStrokeWidth(5);//thickness of line
    line2.setStroke(Color.BLACK);//color of line
    line3.setStrokeWidth(5);//thickness of line
    line3.setStroke(Color.BLACK);//color of line
    line4.setStrokeWidth(5);//thickness of line
    line4.setStroke(Color.BLACK);//color of line
    line5.setStrokeWidth(5);//thickness of line
    line5.setStroke(Color.BLACK);//color of line

    // Create a pane to hold the circle 

    ballPane.getChildren().add(circle); //adds circle to picture
     ballPane.getChildren().add(circle1);//adds circle to picture
    ballPane.getChildren().add(line1);//adds line
    ballPane.getChildren().add(line2);//adds line
    ballPane.getChildren().add(line3);//adds line
   ballPane.getChildren().add(line4);//adds line
    ballPane.getChildren().add(line5);//adds line
    ballPane.getChildren().add(arc);//adds arc





    Scene scene = new Scene( ballPane, 400, 400);

    primaryStage.setTitle("stick figure");//title
    primaryStage.setScene(scene);
    primaryStage.show();
}

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    launch(args);
}
class BallPane extends Pane {

public final double radius = 20;
private double x = 2 * radius, y = 3 * radius;
private double dx = 3; // Number of pixels to move each time

private Timeline animation;

public BallPane() {


    // Create the animation for 25 millisecond events
    animation = new Timeline(new KeyFrame(Duration.millis(25), e ->    moveBall()));
    animation.setCycleCount(Timeline.INDEFINITE);
    animation.play(); // Start animation
}

public void play() {
    animation.play();
}

public void pause() {
    animation.pause();
}
// Move the ball. When a wall is encountered, reverse direction
protected void moveBall() {
    if (x <= radius || x >= getWidth() - radius) {
        dx *= -1; // Change direction
    }
    // Adjust ball position
    x += dx;

}
}

}

您沒有在 moveBall() 方法中進行任何移動。 您只需更改本地值。 查看 Pane 繼承的Node類和translateXProperty 你想改變它,而不僅僅是你的領域。
你甚至在你的 Ball 類中有實際的翻譯: circle.setCenterX(x);

protected void moveBall() {
    if (x <= radius || x >= getWidth() - radius) {
        dx *= -1; // Change direction
    }
    // Adjust ball position
    x += dx; 

    //#######################################
    setTranslateX(x); // Move the Pane!!! ###
    //#######################################
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM