Connect4, как выделять строки и столбцы в действии с помощью мыши - PullRequest
1 голос
/ 01 июля 2019

Я новичок в Java, и у меня проблемы с настройкой графического интерфейса для Connect4, как задумано. Поэтому я хочу добавить диски, но не могу сказать программе, как разделять строки и столбцы. Если я создаю класс диска и просто добавляю диски, он помещается в первый ряд [0]. Если я создаю список, в который я помещаю прямоугольники (столбцы), он просто вытаскивает это ==> Я действительно не знаю, что делать.

Ошибка: дети: добавлены дубликаты детей: родитель = сетка

Как добавить событие мыши, чтобы мое приложение знало, в каком столбце я щелкнул, и вставило в него диск?

класс fxml, созданный с помощью построителя сцен:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.Insets?>
<?import javafx.scene.Cursor?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.shape.Circle?>
<?import javafx.scene.shape.Rectangle?>
<?import javafx.scene.text.Font?>

<Pane fx:id="discRoot" prefHeight="350.0" prefWidth="350.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
   <children>
      <GridPane fx:id="gridPane" gridLinesVisible="true" layoutY="50.0" prefHeight="300.0" prefWidth="350.0" style="-fx-background-color: thistle;">
         <columnConstraints>
            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
         </columnConstraints>
         <rowConstraints>
            <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
            <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
            <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
            <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
            <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
            <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
         </rowConstraints>
         <children>
            <Circle fill="WHITE" radius="23.0" stroke="BLACK" strokeLineCap="ROUND" strokeType="INSIDE" strokeWidth="2.0" GridPane.halignment="CENTER" GridPane.valignment="CENTER" />
            <Circle fill="WHITE" radius="23.0" stroke="BLACK" strokeLineCap="ROUND" strokeType="INSIDE" strokeWidth="2.0" GridPane.columnIndex="1" GridPane.halignment="CENTER" GridPane.valignment="CENTER" />
            <Circle fill="WHITE" radius="23.0" stroke="BLACK" strokeLineCap="ROUND" strokeType="INSIDE" strokeWidth="2.0" GridPane.columnIndex="2" GridPane.halignment="CENTER" GridPane.valignment="CENTER" />
            <Circle fill="WHITE" radius="23.0" stroke="BLACK" strokeLineCap="ROUND" strokeType="INSIDE" strokeWidth="2.0" GridPane.columnIndex="3" GridPane.halignment="CENTER" GridPane.valignment="CENTER" />
            <Circle fill="WHITE" radius="23.0" stroke="BLACK" strokeLineCap="ROUND" strokeType="INSIDE" strokeWidth="2.0" GridPane.columnIndex="4" GridPane.halignment="CENTER" GridPane.valignment="CENTER" />
            <Circle fill="WHITE" radius="23.0" stroke="BLACK" strokeLineCap="ROUND" strokeType="INSIDE" strokeWidth="2.0" GridPane.columnIndex="3" GridPane.halignment="CENTER" GridPane.rowIndex="1" GridPane.valignment="CENTER" />
            <Circle fill="WHITE" radius="23.0" stroke="BLACK" strokeLineCap="ROUND" strokeType="INSIDE" strokeWidth="2.0" GridPane.columnIndex="6" GridPane.halignment="CENTER" GridPane.valignment="CENTER" />
            <Circle fill="WHITE" radius="23.0" stroke="BLACK" strokeLineCap="ROUND" strokeType="INSIDE" strokeWidth="2.0" GridPane.halignment="CENTER" GridPane.rowIndex="1" GridPane.valignment="CENTER" />
            <Circle fill="WHITE" radius="23.0" stroke="BLACK" strokeLineCap="ROUND" strokeType="INSIDE" strokeWidth="2.0" GridPane.columnIndex="1" GridPane.halignment="CENTER" GridPane.rowIndex="1" GridPane.valignment="CENTER" />
            <Circle fill="WHITE" radius="23.0" stroke="BLACK" strokeLineCap="ROUND" strokeType="INSIDE" strokeWidth="2.0" GridPane.columnIndex="2" GridPane.halignment="CENTER" GridPane.rowIndex="1" GridPane.valignment="CENTER" />
            <Circle fill="WHITE" radius="23.0" stroke="BLACK" strokeLineCap="ROUND" strokeType="INSIDE" strokeWidth="2.0" GridPane.columnIndex="5" GridPane.halignment="CENTER" GridPane.valignment="CENTER" />
            <Circle fill="WHITE" radius="23.0" stroke="BLACK" strokeLineCap="ROUND" strokeType="INSIDE" strokeWidth="2.0" GridPane.columnIndex="4" GridPane.halignment="CENTER" GridPane.rowIndex="1" GridPane.valignment="CENTER" />
            <Circle fill="WHITE" radius="23.0" stroke="BLACK" strokeLineCap="ROUND" strokeType="INSIDE" strokeWidth="2.0" GridPane.columnIndex="5" GridPane.halignment="CENTER" GridPane.rowIndex="1" GridPane.valignment="CENTER" />
            <Circle fill="WHITE" radius="23.0" stroke="BLACK" strokeLineCap="ROUND" strokeType="INSIDE" strokeWidth="2.0" GridPane.columnIndex="6" GridPane.halignment="CENTER" GridPane.rowIndex="1" GridPane.valignment="CENTER" />
            <Circle fill="WHITE" radius="23.0" stroke="BLACK" strokeLineCap="ROUND" strokeType="INSIDE" strokeWidth="2.0" GridPane.halignment="CENTER" GridPane.rowIndex="2" GridPane.valignment="CENTER" />
            <Circle fill="WHITE" radius="23.0" stroke="BLACK" strokeLineCap="ROUND" strokeType="INSIDE" strokeWidth="2.0" GridPane.columnIndex="1" GridPane.halignment="CENTER" GridPane.rowIndex="2" GridPane.valignment="CENTER" />
            <Circle fill="WHITE" radius="23.0" stroke="BLACK" strokeLineCap="ROUND" strokeType="INSIDE" strokeWidth="2.0" GridPane.columnIndex="2" GridPane.halignment="CENTER" GridPane.rowIndex="2" GridPane.valignment="CENTER" />
            <Circle fill="WHITE" radius="23.0" stroke="BLACK" strokeLineCap="ROUND" strokeType="INSIDE" strokeWidth="2.0" GridPane.columnIndex="4" GridPane.halignment="CENTER" GridPane.rowIndex="3" GridPane.valignment="CENTER" />
            <Circle fill="WHITE" radius="23.0" stroke="BLACK" strokeLineCap="ROUND" strokeType="INSIDE" strokeWidth="2.0" GridPane.columnIndex="4" GridPane.halignment="CENTER" GridPane.rowIndex="2" GridPane.valignment="CENTER" />
            <Circle fill="WHITE" radius="23.0" stroke="BLACK" strokeLineCap="ROUND" strokeType="INSIDE" strokeWidth="2.0" GridPane.columnIndex="5" GridPane.halignment="CENTER" GridPane.rowIndex="2" GridPane.valignment="CENTER" />
            <Circle fill="WHITE" radius="23.0" stroke="BLACK" strokeLineCap="ROUND" strokeType="INSIDE" strokeWidth="2.0" GridPane.columnIndex="6" GridPane.halignment="CENTER" GridPane.rowIndex="2" GridPane.valignment="CENTER" />
            <Circle fill="WHITE" radius="23.0" stroke="BLACK" strokeLineCap="ROUND" strokeType="INSIDE" strokeWidth="2.0" GridPane.halignment="CENTER" GridPane.rowIndex="3" GridPane.valignment="CENTER" />
            <Circle fill="WHITE" radius="23.0" stroke="BLACK" strokeLineCap="ROUND" strokeType="INSIDE" strokeWidth="2.0" GridPane.columnIndex="1" GridPane.halignment="CENTER" GridPane.rowIndex="3" GridPane.valignment="CENTER" />
            <Circle fill="WHITE" radius="23.0" stroke="BLACK" strokeLineCap="ROUND" strokeType="INSIDE" strokeWidth="2.0" GridPane.columnIndex="2" GridPane.halignment="CENTER" GridPane.rowIndex="3" GridPane.valignment="CENTER" />
            <Circle fill="WHITE" radius="23.0" stroke="BLACK" strokeLineCap="ROUND" strokeType="INSIDE" strokeWidth="2.0" GridPane.columnIndex="3" GridPane.halignment="CENTER" GridPane.rowIndex="3" GridPane.valignment="CENTER" />
            <Circle fill="WHITE" radius="23.0" stroke="BLACK" strokeLineCap="ROUND" strokeType="INSIDE" strokeWidth="2.0" GridPane.halignment="CENTER" GridPane.valignment="CENTER" />
            <Circle fill="WHITE" radius="23.0" stroke="BLACK" strokeLineCap="ROUND" strokeType="INSIDE" strokeWidth="2.0" GridPane.columnIndex="3" GridPane.halignment="CENTER" GridPane.rowIndex="2" GridPane.valignment="CENTER" />
            <Circle fill="WHITE" radius="23.0" stroke="BLACK" strokeLineCap="ROUND" strokeType="INSIDE" strokeWidth="2.0" GridPane.columnIndex="5" GridPane.halignment="CENTER" GridPane.rowIndex="3" GridPane.valignment="CENTER" />
            <Circle fill="WHITE" radius="23.0" stroke="BLACK" strokeLineCap="ROUND" strokeType="INSIDE" strokeWidth="2.0" GridPane.columnIndex="6" GridPane.halignment="CENTER" GridPane.rowIndex="3" GridPane.valignment="CENTER" />
            <Circle fill="WHITE" radius="23.0" stroke="BLACK" strokeLineCap="ROUND" strokeType="INSIDE" strokeWidth="2.0" GridPane.halignment="CENTER" GridPane.rowIndex="4" GridPane.valignment="CENTER" />
            <Circle fill="WHITE" radius="23.0" stroke="BLACK" strokeLineCap="ROUND" strokeType="INSIDE" strokeWidth="2.0" GridPane.columnIndex="1" GridPane.halignment="CENTER" GridPane.rowIndex="4" GridPane.valignment="CENTER" />
            <Circle fill="WHITE" radius="23.0" stroke="BLACK" strokeLineCap="ROUND" strokeType="INSIDE" strokeWidth="2.0" GridPane.columnIndex="3" GridPane.halignment="CENTER" GridPane.rowIndex="4" GridPane.valignment="CENTER" />
            <Circle fill="WHITE" radius="23.0" stroke="BLACK" strokeLineCap="ROUND" strokeType="INSIDE" strokeWidth="2.0" GridPane.columnIndex="2" GridPane.halignment="CENTER" GridPane.rowIndex="4" GridPane.valignment="CENTER" />
            <Circle fill="WHITE" radius="23.0" stroke="BLACK" strokeLineCap="ROUND" strokeType="INSIDE" strokeWidth="2.0" GridPane.columnIndex="4" GridPane.halignment="CENTER" GridPane.rowIndex="4" GridPane.valignment="CENTER" />
            <Circle fill="WHITE" radius="23.0" stroke="BLACK" strokeLineCap="ROUND" strokeType="INSIDE" strokeWidth="2.0" GridPane.columnIndex="5" GridPane.halignment="CENTER" GridPane.rowIndex="4" GridPane.valignment="CENTER" />
            <Circle fill="WHITE" radius="23.0" stroke="BLACK" strokeLineCap="ROUND" strokeType="INSIDE" strokeWidth="2.0" GridPane.columnIndex="6" GridPane.halignment="CENTER" GridPane.rowIndex="4" GridPane.valignment="CENTER" />
            <Circle fill="WHITE" radius="23.0" stroke="BLACK" strokeLineCap="ROUND" strokeType="INSIDE" strokeWidth="2.0" GridPane.halignment="CENTER" GridPane.rowIndex="5" GridPane.valignment="CENTER" />
            <Circle fill="WHITE" radius="23.0" stroke="BLACK" strokeLineCap="ROUND" strokeType="INSIDE" strokeWidth="2.0" GridPane.columnIndex="1" GridPane.halignment="CENTER" GridPane.rowIndex="5" GridPane.valignment="CENTER" />
            <Circle fill="WHITE" radius="23.0" stroke="BLACK" strokeLineCap="ROUND" strokeType="INSIDE" strokeWidth="2.0" GridPane.columnIndex="2" GridPane.halignment="CENTER" GridPane.rowIndex="5" GridPane.valignment="CENTER" />
            <Circle fill="WHITE" radius="23.0" stroke="BLACK" strokeLineCap="ROUND" strokeType="INSIDE" strokeWidth="2.0" GridPane.columnIndex="3" GridPane.halignment="CENTER" GridPane.rowIndex="5" GridPane.valignment="CENTER" />
            <Circle fill="WHITE" radius="23.0" stroke="BLACK" strokeLineCap="ROUND" strokeType="INSIDE" strokeWidth="2.0" GridPane.columnIndex="4" GridPane.halignment="CENTER" GridPane.rowIndex="5" GridPane.valignment="CENTER" />
            <Circle fill="WHITE" radius="23.0" stroke="BLACK" strokeLineCap="ROUND" strokeType="INSIDE" strokeWidth="2.0" GridPane.columnIndex="5" GridPane.halignment="CENTER" GridPane.rowIndex="5" GridPane.valignment="CENTER" />
            <Circle fill="WHITE" radius="23.0" stroke="BLACK" strokeLineCap="ROUND" strokeType="INSIDE" strokeWidth="2.0" GridPane.columnIndex="6" GridPane.halignment="CENTER" GridPane.rowIndex="5" GridPane.valignment="CENTER" />
            <Rectangle fx:id="rect" arcHeight="5.0" arcWidth="5.0" fill="THISTLE" height="300.0" onMouseClicked="#setDiscOnClick" onMouseEntered="#setRectangleOnMenter" onMouseExited="#setRectangleOnMexit" opacity="0.1" stroke="BLACK" strokeType="INSIDE" width="50.0" GridPane.valignment="TOP">
               <cursor>
                  <Cursor fx:constant="CROSSHAIR" />
               </cursor>
            </Rectangle>
            <Rectangle fx:id="rect1" arcHeight="5.0" arcWidth="5.0" fill="THISTLE" height="300.0" onMouseEntered="#setRectangleOnMenter1" onMouseExited="#setRectangleOnMexit1" opacity="0.1" stroke="BLACK" strokeType="INSIDE" width="50.0" GridPane.columnIndex="1" GridPane.valignment="TOP">
               <cursor>
                  <Cursor fx:constant="CROSSHAIR" />
               </cursor>
            </Rectangle>
            <Rectangle fx:id="rect2" arcHeight="5.0" arcWidth="5.0" fill="THISTLE" height="300.0" onMouseEntered="#setRectangleOnMenter2" onMouseExited="#setRectangleOnMexit2" opacity="0.1" stroke="BLACK" strokeType="INSIDE" width="50.0" GridPane.columnIndex="2" GridPane.valignment="TOP">
               <cursor>
                  <Cursor fx:constant="CROSSHAIR" />
               </cursor>
            </Rectangle>
            <Rectangle fx:id="rect3" arcHeight="5.0" arcWidth="5.0" fill="THISTLE" height="300.0" onMouseEntered="#setRectangleOnMenter3" onMouseExited="#setRectangleOnMexit3" opacity="0.1" stroke="BLACK" strokeType="INSIDE" width="50.0" GridPane.columnIndex="3" GridPane.valignment="TOP">
               <cursor>
                  <Cursor fx:constant="CROSSHAIR" />
               </cursor>
            </Rectangle>
            <Rectangle fx:id="rect4" arcHeight="5.0" arcWidth="5.0" fill="THISTLE" height="300.0" onMouseEntered="#setRectangleOnMenter4" onMouseExited="#setRectangleOnMexit4" opacity="0.1" stroke="BLACK" strokeType="INSIDE" width="50.0" GridPane.columnIndex="4" GridPane.valignment="TOP">
               <cursor>
                  <Cursor fx:constant="CROSSHAIR" />
               </cursor>
            </Rectangle>
            <Rectangle fx:id="rect5" arcHeight="5.0" arcWidth="5.0" fill="THISTLE" height="300.0" onMouseEntered="#setRectangleOnMenter5" onMouseExited="#setRectangleOnMexit5" opacity="0.1" stroke="BLACK" strokeType="INSIDE" width="50.0" GridPane.columnIndex="5" GridPane.valignment="TOP">
               <cursor>
                  <Cursor fx:constant="CROSSHAIR" />
               </cursor>
            </Rectangle>
            <Rectangle fx:id="rect6" arcHeight="5.0" arcWidth="5.0" fill="THISTLE" height="300.0" onMouseEntered="#setRectangleOnMenter6" onMouseExited="#setRectangleOnMexit6" opacity="0.1" stroke="BLACK" strokeType="INSIDE" width="50.0" GridPane.columnIndex="6" GridPane.valignment="TOP">
               <cursor>
                  <Cursor fx:constant="CROSSHAIR" />
               </cursor>
            </Rectangle>
         </children>
      </GridPane>
      <HBox prefHeight="25.0" prefWidth="350.0" style="-fx-background-color: lightsteelblue;">
         <children>
            <Label alignment="CENTER" prefHeight="25.0" prefWidth="100.0" text="Broj poteza crvenog:">
               <font>
                  <Font size="11.0" />
               </font>
            </Label>
            <Label alignment="CENTER" prefHeight="150.0" prefWidth="150.0" text="Na potezu:" textAlignment="CENTER">
               <font>
                  <Font size="11.0" />
               </font>
            </Label>
            <Label alignment="CENTER" prefHeight="25.0" prefWidth="100.0" text="Broj poteza žutog:" textAlignment="CENTER">
               <font>
                  <Font size="11.0" />
               </font>
            </Label>
         </children>
      </HBox>
      <HBox layoutY="25.0" prefHeight="25.0" prefWidth="350.0" style="-fx-background-color: lightsteelblue;">
         <children>
            <Label alignment="CENTER" prefHeight="100.0" prefWidth="100.0" text="Label">
               <font>
                  <Font size="11.0" />
               </font>
               <HBox.margin>
                  <Insets left="-1.0" />
               </HBox.margin>
            </Label>
            <Label alignment="CENTER" prefHeight="150.0" prefWidth="150.0" text="Label">
               <font>
                  <Font size="11.0" />
               </font>
            </Label>
            <Label alignment="CENTER" prefHeight="25.0" prefWidth="100.0" text="Label">
               <font>
                  <Font size="11.0" />
               </font>
            </Label>
         </children>
      </HBox>
   </children>
</Pane>

Это мой класс контроллера:

package sample;

import javafx.animation.TranslateTransition;
import javafx.fxml.FXML;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.Shape;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

public class Controller  {

    private static final int COLUMNS = 7;
    private  static final int ROWS = 6;
    private static final int TILE_SIZE = 50;
    private boolean redMove = true;

    @FXML
    Rectangle rect = new Rectangle();
    @FXML
    Rectangle rect1 = new Rectangle();
    @FXML
    Rectangle rect2 = new Rectangle();
    @FXML
    Rectangle rect3 = new Rectangle();
    @FXML
    Rectangle rect4 = new Rectangle();
    @FXML
    Rectangle rect5 = new Rectangle();
    @FXML
    Rectangle rect6 = new Rectangle();

    @FXML
    GridPane gridPane = new GridPane();


    @FXML
    private Pane discRoot = new Pane();


    @FXML
    public void setDiscOnClick() {
        System.out.println(gridPane.getChildren().add(new Disc(redMove)));
    }

    @FXML
    public void setRectangleOnMenter() {
        rect.setFill(Color.AQUA);
    }
    @FXML
    public void setRectangleOnMexit() {
        rect.setFill(Color.THISTLE);
    }

    @FXML
    public void setRectangleOnMenter1() {
        rect1.setFill(Color.AQUA);
    }

    @FXML
    public void setRectangleOnMexit1() {
        rect1.setFill(Color.THISTLE);
    }

    @FXML
    public void setRectangleOnMenter2() {
        rect2.setFill(Color.AQUA);
    }

    @FXML
    public void setRectangleOnMexit2() {
        rect2.setFill(Color.THISTLE);
    }

    @FXML
    public void setRectangleOnMenter3() {
        rect3.setFill(Color.AQUA);
    }
    @FXML
    public void setRectangleOnMexit3() {
        rect3.setFill(Color.THISTLE);
    }


    public void setRectangleOnMenter4() {
        rect4.setFill(Color.AQUA);
    }

    public void setRectangleOnMexit4() {
        rect4.setFill(Color.THISTLE);
    }


    public void setRectangleOnMenter5() {
        rect5.setFill(Color.AQUA);
    }

    public void setRectangleOnMexit5() {
        rect5.setFill(Color.THISTLE);
    }


    public void setRectangleOnMenter6() {
        rect6.setFill(Color.AQUA);
    }

    public void setRectangleOnMexit6() {
        rect6.setFill(Color.THISTLE);
    }

И мой основной класс:

package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {


    @Override
    public void start(Stage primaryStage) throws Exception{


        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        primaryStage.setTitle("Connect4");
        primaryStage.setScene(new Scene(root));
        primaryStage.show();
    }


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