Reputation: 7
I am new to JAVAFX and I am creating a card game. I have a problem in displaying the pictures of the card on the screen.
I have card images in one folder in the JAVA Project and I am accessing it. But when I run the program I cannot see any images of the cards on the screen.
Here is my code.
public class Main extends Application
{
public static void main(String args[])
{
// launch the application
launch(args);
}
public void start(Stage s)
{
TilePane r = new TilePane();
Scene sc = new Scene(r);
List<Image>card = new ArrayList<>();
for(int i = 1; i < 4; i++)
{
card.add(new Image(getClass().getResource(i+".png").toExternalForm()));
}
ImageView view1 = new ImageView(card.get(1));
ImageView view2 = new ImageView(card.get(2));
ImageView view3 = new ImageView(card.get(3));
view1.setImage(card.get(1));
view2.setImage(card.get(2));
view3.setImage(card.get(3));
s.setScene(sc);
s.show();
}
}
Upvotes: 0
Views: 701
Reputation: 506
Add the ImageViews
to the TilePane
and the TilePane
to the Scene
TilePane r = new TilePane();
r.getChildren().addAll(view1,view2,view3);
Scene sc = new Scene(r);
s.setScene(sc);
s.show();
Upvotes: 3