Javafx setOnMouseClicked根本不起作用-ImageView-没什么

时间:2019-06-16 09:34:15

标签: javafx imageview eventhandler gridpane

我正在尝试用JavaFX编写一个简单的益智游戏。我希望将图像切成整块,然后在单击它们时交换它们。问题是当一切都在Main中时我曾经使用的解决方案(因为我在main中创建了另一个场景,然后切换到它)。我试图使代码更简洁一些,所以我创建了另一个FXML文件和一个控制器。我以前也曾使用按钮代替ImageViewers。这可能是问题吗?

我可以看到有人说ImageViewer中没有OnAction属性。但是有些使它起作用。我应该放弃并使用按钮吗?

public class Grid  implements Initializable{


    public Image done;
    public BufferedImage orgin;
    public BufferedImage bwhite;
    public Image iwhite;
    public Image[][] arr =new Image[6][6];
    public ImageView [][]iv = new ImageView[6][6];

    public GridPane gridPane;
    public ImageView tmp1=new ImageView(iwhite);
    public ImageView special= tmp1;

    @FXML
    private AnchorPane Magic;

    private void switcher()
    {
        gridPane.getChildren().forEach(button-> button.setOnMouseClicked(
                event -> {
                    swap(button, special);
                    System.out.println("sth");

                }
        ));
    }

    public static void swap(Node n1, Node n2) {
        Integer temp = GridPane.getRowIndex(n1);
        GridPane.setRowIndex(n1, GridPane.getRowIndex(n2));
        GridPane.setRowIndex(n2, temp);

        temp = GridPane.getColumnIndex(n1);
        GridPane.setColumnIndex(n1, GridPane.getColumnIndex(n2));
        GridPane.setColumnIndex(n2, temp);
    }


    void imgloader() throws Exception
    {
        int rand = (int)(Math.random() * 0)+1;

        if (rand==1) orgin= ImageIO.read(new File("C:\\Users\\PC\\Desktop\\untitled\\image1.jpg"));

        bwhite= ImageIO.read(new File("C:\\Users\\PC\\Desktop\\untitled\\white.jpg"));
    }

    void imagesplitter()
    {
        int val=Difficoulty.diffval;
        int block = 300/val;
        for (int i=0; i<val; i++)
        {
            for (int j=0; j<val; j++)
            {
                BufferedImage tmp= orgin.getSubimage(j*block,i*block,block,block);
                BufferedImage tmpwhite= bwhite.getSubimage(j*block,i*block,block,block);

                done = SwingFXUtils.toFXImage(tmp, null );
                iwhite = SwingFXUtils.toFXImage(tmpwhite, null );
                arr[i][j]=done;
            }
        }
    }

    @FXML
    void initialize() {
        assert Magic != null : "fx:id=\"Magic\" was not injected: check your FXML file 'Grid.fxml'.";

    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {

        gridPane = new GridPane();
        switcher();

        try {
            imgloader();
        } catch (Exception e) {
            e.printStackTrace();
        }

        imagesplitter();
        gridPane.setMinSize(300, 300);
        gridPane.setPadding(new Insets(10, 10, 10, 10));

        int val = Difficoulty.diffval;
        for (int i=0; i<val; i++)
        {
            for (int j=0; j<val; j++)
            {
                ImageView iv1=new ImageView(arr[j][i]);
                iv[i][j] = iv1;

                if (i==val-1 && j==val-1)
                {
                }
                else
                {
                    gridPane.add(iv[i][j],j,i);
                }

            }
        }


        gridPane.add(special,val-1,val-1);

        Magic.getChildren().addAll(gridPane);

    }

    @FXML private void MainMenu(ActionEvent event) throws Exception{

        Parent newparent = FXMLLoader.load(getClass().getResource("sample.fxml"));
        Scene newscene = new Scene(newparent);

        Stage window = (Stage)((Node)event.getSource()).getScene().getWindow();
        window.setScene(newscene);
        window.show();
    }
}

0 个答案:

没有答案