0

I'm trying to write a simple puzzle game in JavaFX. I want the image to be cut int pieces and then I would like to swap them when I click on them. The problem is the solution I had used to work when everything was in Main (as I created another scene in main, then switched to it). I tried to make the code a bit cleaner, so I created another FXML file and a controller to it. I've also previously used buttons instead of ImageViewers. Could this be the problem?

I can see that some people say there is no OnAction property in ImageViewer. But some got it working. Should I just give up and use buttons?

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();
    }
}
6
  • 1
    A minimal reproducible example and a better description of what currently happens versus what should happen would help us better help you. Is the problem just that the handlers aren't being invoked? Are the handlers even added? Are you sure both initialize(URL,ResourceBundle) and initialize() are invoked (I don't know what happens when both are declared)? Also, please follow Java naming conventions—at least when sharing (Java) code in a public space.
    – Slaw
    Jun 16, 2019 at 11:34
  • 1
    Asside from the problem: I don't recommend getting the BufferedImage API involved here: It's not actually necessary. Just create a single instance of Image and use the ImageView.viewport property to display a part of the ImageView in each ImageView.
    – fabian
    Jun 16, 2019 at 14:45
  • Inteliji Idea added initialize(URL,ResourceBundle) when i implemented Initializable and Scene Builder added Initialize(). I want the handler to work for each elemet in gridpane. When i created everything in Main (without fxml file, juest created new Scene) that class extended aplication. And in there switcher worked like a charm. Now it does not do anything. Maybe I can iniialize it in fxml file but if I can then how? Jun 16, 2019 at 16:28
  • 1
    Try tracing how your code is executed. You can do this by adding breakpoints and stepping though the code (i.e. use a debugger) or adding simple print statements at various important places in your code. This will help you narrow down the problem and hopefully discover a solution. Also, try either implementing Initializable or using the initialize() method. Note the FXMLLoader documentation recommends the latter.
    – Slaw
    Jun 16, 2019 at 17:28
  • 1
    You call switcher i.e. the method registering the event handlers to children of the immediately after creating the GridPane. At that time no nodes have been added to the GridPane. Not a single one of those event handler is registered.
    – fabian
    Jun 16, 2019 at 19:43

0

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.