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();
}
}
initialize(URL,ResourceBundle)
andinitialize()
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.BufferedImage
API involved here: It's not actually necessary. Just create a single instance ofImage
and use theImageView.viewport
property to display a part of theImageView
in eachImageView
.Initializable
or using theinitialize()
method. Note theFXMLLoader
documentation recommends the latter.switcher
i.e. the method registering the event handlers to children of the immediately after creating theGridPane
. At that time no nodes have been added to theGridPane
. Not a single one of those event handler is registered.