Java 拖动StackPane(由圆圈[]组成)并放置在GridPane上
我正在为学校学习JavaFX,并尝试使用JavaFX重新创建游戏《粉碎之环》(Android)。我们使用的是MVP模型(模型视图演示者)方法 目前,我一直在尝试获取我的StackPanes(类StackView)的拖动功能,该功能由一组圆组成,并将它们放置在GridPane(类GridPaneView)上。我试过很多东西,但似乎都没用 拖放功能应添加到Presenter类中,理想情况下:Java 拖动StackPane(由圆圈[]组成)并放置在GridPane上,java,javafx,Java,Javafx,我正在为学校学习JavaFX,并尝试使用JavaFX重新创建游戏《粉碎之环》(Android)。我们使用的是MVP模型(模型视图演示者)方法 目前,我一直在尝试获取我的StackPanes(类StackView)的拖动功能,该功能由一组圆组成,并将它们放置在GridPane(类GridPaneView)上。我试过很多东西,但似乎都没用 拖放功能应添加到Presenter类中,理想情况下: 可以将StackView拖放到GridPane 只有在GridPane的特定位置上没有其他内容时,才能删除
- 可以将StackView拖放到GridPane
- 只有在GridPane的特定位置上没有其他内容时,才能删除StackView
- 我将能够以数组的形式获得它们被放置的位置的坐标[]
public class StackView extends StackPane {
private static final int DEFAULT_STACK_SIZE = 3;
private Circle[] circles;
private UISettings uiSettings;
public StackView(UISettings uiSettings) {
this.uiSettings = uiSettings;
initialiseNodes();
layoutNodes();
}
private void initialiseNodes() {
circles = new Circle[DEFAULT_STACK_SIZE];
}
private void layoutNodes() {
// Generate circles per Stack in Array
circles[0] = new Circle(30);
circles[1] = new Circle(20);
circles[2] = new Circle(10);
for (int i = 0; i < DEFAULT_STACK_SIZE; i++) {
circles[i].setFill(Color.TRANSPARENT);
circles[i].setStrokeWidth(2);
this.getChildren().add(circles[i]);
}
}
public Circle[] getCircles() {
return circles;
}
}
公共类StackView扩展了StackPane{
私有静态最终int默认_堆栈_大小=3;
私人圈子[]圈子;
私人UISettings UISettings;
公共堆栈视图(UISettings UISettings){
this.uiSettings=uiSettings;
initialiseNodes();
layoutNodes();
}
私有void initialiseNodes(){
圆圈=新圆圈[默认的堆叠大小];
}
私有void layoutNodes(){
//在数组中为每个堆栈生成圆
圆圈[0]=新圆圈(30);
圆圈[1]=新圆圈(20);
圆圈[2]=新圆圈(10);
对于(int i=0;i
GameBoard的我的视图类,一个网格窗格:
public class GameboardView extends GridPane {
private static final int DEFAULT_SIZE = 3;
private UISettings uiSettings;
private Rectangle[] rectangles;
private RowConstraints r1;
private ColumnConstraints c1;
public GameboardView(UISettings uiSettings) {
this.uiSettings = uiSettings;
initialiseNodes();
layoutNodes();
}
private void initialiseNodes(){
rectangles = new Rectangle[DEFAULT_SIZE];
r1 = new RowConstraints();
c1 = new ColumnConstraints();
}
private void layoutNodes(){
for (int i = 0; i < DEFAULT_SIZE; i++) {
for (int j = 0; j < DEFAULT_SIZE; j++) {
rectangles[i] = new Rectangle();
this.add(rectangles[i], i,j);
}
}
this.setGridLinesVisible(true);
r1.setPercentHeight(33.3);
c1.setPercentWidth(33.3);
this.getRowConstraints().addAll(r1, r1, r1);
this.getColumnConstraints().addAll(c1, c1, c1);
}
}
公共类GameboardView扩展GridPane{
私有静态最终int默认值_SIZE=3;
私人UISettings UISettings;
私有矩形[]矩形;
私人行限制r1;
私人专栏;
公共游戏板视图(UISettings UISettings){
this.uiSettings=uiSettings;
initialiseNodes();
layoutNodes();
}
私有void initialiseNodes(){
矩形=新矩形[默认大小];
r1=新的行约束();
c1=新列约束();
}
私有void layoutNodes(){
对于(int i=0;i
GamescreenView类-由上述2个类组成:
public class GameScreenView extends BorderPane {
private GameboardView gameboardView;
private HBox hboxScore;
private HBox hboxStack;
private StackView[] stackViews;
private UISettings uiSettings;
private Label score;
private Label highscore;
private static final int NUMBER_OF_STACK_VIEWS = 3;
public GameScreenView(UISettings uiSettings) {
this.uiSettings = uiSettings;
this.initialiseNodes();
this.layoutNodes();
}
private void initialiseNodes(){
hboxScore = new HBox();
hboxStack = new HBox();
score = new Label("Score:");
highscore = new Label("HighScore");
stackViews = new StackView[NUMBER_OF_STACK_VIEWS];
gameboardView = new GameboardView(uiSettings);
for (int i = 0; i < NUMBER_OF_STACK_VIEWS; i++) {
stackViews[i] = new StackView(uiSettings);
}
}
private void layoutNodes() {
for (int i = 0; i < NUMBER_OF_STACK_VIEWS; i++) {
hboxStack.getChildren().add(stackViews[i]);
hboxStack.setHgrow(stackViews[i], Priority.ALWAYS);
}
hboxScore.getChildren().addAll(score,highscore);
hboxScore.setSpacing(120);
this.setTop(hboxScore);
this.setCenter(gameboardView);
this.setBottom(hboxStack);
setMargin(gameboardView, new Insets(10));
}
public StackView[] getStackViews() {
return stackViews;
}
public GameboardView getGameboardView() {
return gameboardView;
}
}
公共类游戏屏幕视图扩展边框窗格{
私有GameboardView GameboardView;
私人HBox评分;
专用HBox-hboxStack;
私有StackView[]StackView;
私人UISettings UISettings;
自有品牌分数;
自有品牌高分;
私有静态堆栈视图的最终整数=3;
公共游戏屏幕视图(UISettings UISettings){
this.uiSettings=uiSettings;
this.initialiseNodes();
这个.layoutNodes();
}
私有void initialiseNodes(){
hboxScore=新的HBox();
hboxStack=新的HBox();
分数=新标签(“分数:”);
highscore=新标签(“highscore”);
StackView=新StackView[堆叠视图的数量];
gameboardView=新gameboardView(uiSettings);
对于(int i=0;i<堆栈视图的数量;i++){
StackView[i]=新的StackView(UI设置);
}
}
私有void layoutNodes(){
对于(int i=0;i<堆栈视图的数量;i++){
hboxStack.getChildren().add(stackview[i]);
setHgrow(stackview[i],Priority.ALWAYS);
}
hboxScore.getChildren().addAll(score,highscore);
hboxScore.setSpacing(120);
这个.setTop(hboxScore);
此.setCenter(gameboardView);
这个.setBottom(hboxStack);
setMargin(游戏板视图,新插图(10));
}
public StackView[]getstackview(){
返回堆栈视图;
}
公共GameboardView getGameboardView(){
返回游戏板视图;
}
}
欢迎提供任何帮助/指导如何将其编码到GameScreenPresenter类中到底是什么问题?因为问题太广泛了:把它分成小批量,在做了彻底的研究后,试着用你所知道/学到的来解决问题,为那个孤立的部分写一封信,冲洗并重复,直到所有的垫脚石都解决了为止——然后再合并。当卡在任何一个步骤上时,请返回该示例,演示哪些步骤没有按预期工作:)