【问题标题】:Animation jumps to the right动画向右跳
【发布时间】:2016-03-11 06:18:27
【问题描述】:

忙于研究一些简单的 MVC 模型,有一件事我想不通。当我运行动画时,圆圈向右跳了一下(似乎是半径的一半)。

这是我的 4 节课:

型号:

public class Model {
    private int radius;
    private int xAs;
    private int yAs;

    public Model(int xAs, int yAs, int radius){
        this.xAs = xAs;
        this.yAs = yAs;
        this.radius = radius;
    }

    public int getRadius(){
        return radius;
    }

    public void setRadius(int radius){
        this.radius = radius;
    }

    public int getXAs(){
        return xAs;
    }

    public void setXAs(int xAs){
        this.xAs = xAs;
    }

    public int getYAs(){
        return yAs;
    }

    public void setYAs(int yAs){
        this.yAs = yAs;
    }
}

观点:

import javafx.scene.control.Button;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;

public class View extends Pane{

    private Circle bal;
    private Button start;
    private Button stop;
    private Model model;

    public View(Model model){
        this.model = model;
        createBal();
        this.getChildren().add(bal);


        start = new Button("Start");
        start.setMinWidth(75);
        stop = new Button("Stop");
        stop.setMinWidth(75);
    }

    public void createBal(){
        bal = new Circle();
        bal.setRadius(model.getRadius());
        bal.setCenterX(model.getXAs()+bal.getRadius()/2);
        bal.setCenterY(model.getYAs());
        bal.setFill(Color.RED);
    }

    public Button getStart(){
        return this.start;
    }

    public Button getStop(){
        return this.stop;
    }

    public void adjust(){
        bal.relocate(model.getXAs(), model.getYAs());
    }

}

控制器:

import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.util.Duration;

public class Controller {
    View view;
    Model model;
    Timeline animatie;

    private int yAsPositie = 1;

    public Controller(View view, Model model){
        this.view = view;
        this.model = model;

        animatie = new Timeline(new KeyFrame(Duration.millis(10), e -> beweegBal()));
        animatie.setCycleCount(Timeline.INDEFINITE);
        view.getStart().setOnAction(e -> animatie.play());
        view.getStop().setOnAction(e -> animatie.stop());

    }

    public void beweegBal(){
        model.setYAs(model.getYAs() + yAsPositie);

        view.adjust();
    }
}

开始(应用程序)

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class App extends Application {
    public void start(Stage primaryStage) throws Exception {
        Model bal = new Model(20, 20, 20);

        View view = new View(bal);
        Controller controller = new Controller(view, bal);

        //horizontale box aanmaken voor buttons
        HBox botMenu = new HBox(30);

        //buttons toevoegen aan bottom menu
        botMenu.getChildren().addAll(view.getStart(), view.getStop());

        //stackpane als container voor canvas
        Pane canvas = new Pane();

        //canvas toevoegen aan zn container
        canvas.setBackground(new Background(new BackgroundFill(Color.DARKGRAY, CornerRadii.EMPTY, Insets.EMPTY)));

        //borderpane aanmaken
        BorderPane pane = new BorderPane();

        //bottom menu als onderste gedeelte indelen
        pane.setBottom(botMenu);
        botMenu.setAlignment(Pos.BOTTOM_CENTER);
        botMenu.setPadding(new Insets(10, 10, 10, 10));

        //canvascontainer toevoegen in het midden
        pane.setCenter(canvas);

        //view toevoegen
        canvas.getChildren().add(view);

        Scene scene = new Scene(pane, 500, 500);
        primaryStage.setTitle("De vallende bal");
        primaryStage.setScene(scene);
        primaryStage.setMaxHeight(500);
        primaryStage.setMaxWidth(500);
        primaryStage.setMinHeight(500);
        primaryStage.setMinWidth(500);
        primaryStage.show();
    }

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

如果你运行它,你会注意到它向右跳了一下,已经卡了几个小时了。

【问题讨论】:

标签: java animation javafx


【解决方案1】:

您似乎将model.xAs 解释为球左边缘的x 坐标,因为您在重新定位它时将它用作x 坐标。在这种情况下,你肯定需要用

初始化中心
bal.setCenterX(model.getXAs()+bal.getRadius());

因为圆的半径是圆心到边缘的距离。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多