动画跳到右边

时间:2015-12-06 17:00:16

标签: java animation javafx

忙着为我的学习做一些简单的MVC模型,有一件事我无法理解......当我运行我的动画时,圆圈向右跳一点(看起来像是#&# 39;是半径的一半)。

这里是我的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();
    }
}

开始(app)

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);
    }
}

如果你运行它,你会注意到它向右跳了一下,现在卡在它上好几个小时。

1 个答案:

答案 0 :(得分:1)

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

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

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