Reputation: 25
Whenever I click refresh in my program here, it will generate 5 random numbers from between 1 to 13 and display it in the form of cards on screen. I'm using JavaFx for that. I cannot figure out the second thing, which is when I click the Sort button then the program should take the current set of random numbers and show the cards from lowest to highest.
public class Cards extends Application {
public static void main(String[] args) {
Application.launch(args);
}
public static int mycard() {
Random rand = new Random();
int n = rand.nextInt(13) + 1;
return n;
}
public void start(Stage primaryStage) {
primaryStage.setTitle("Cards Layout");
GridPane pane = new GridPane();
pane.setAlignment(Pos.CENTER);
pane.setHgap(10);
pane.setVgap(5);
// creates button
Button refresh = new Button("Refresh");
HBox button = new HBox(10);
button.setAlignment(Pos.BOTTOM_LEFT);
button.getChildren().add(refresh);
pane.getChildren().add(refresh);
//Create sort button
Button sort = new Button("Sort");
HBox button1 = new HBox(10);
button1.setAlignment(Pos.BOTTOM_RIGHT);
button1.getChildren().add(sort);
pane.getChildren().add(sort);
// creates random card images and adds them to pane
Image img = new Image("image/card/"+mycard()+".png");
ImageView imgView = new ImageView(img);
pane.add(imgView, 1, 0);
Image img2 = new Image("image/card/"+mycard() +".png");
ImageView imgView2 = new ImageView(img2);
pane.add(imgView2, 2, 0);
Image img3 = new Image("image/card/" +mycard()+ ".png");
ImageView imgView3 = new ImageView(img3);
pane.add(imgView3, 3, 0);
Image img4 = new Image("image/card/" +mycard()+".png");
ImageView imgView4 = new ImageView(img4);
pane.add(imgView4, 4, 0);
Image img5 = new Image("image/card/" +mycard()+".png");
ImageView imgView5 = new ImageView(img5);
pane.add(imgView5, 5, 0);
Scene scene = new Scene(pane, 600, 200);
primaryStage.setScene(scene);
primaryStage.show();
refresh.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent e) {
imgView.setImage(new Image("image/card/" +mycard()+".png"));
imgView2.setImage(new Image("image/card/" +mycard()+".png"));
imgView3.setImage(new Image("image/card/" +mycard()+".png"));
imgView4.setImage(new Image("image/card/" +mycard()+".png"));
imgView5.setImage(new Image("image/card/" +mycard()+".png"));
}
});
sort.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent e) {
imgView.setImage(new Image("image/card/" + "1.png"));
imgView2.setImage(new Image("image/card/" + "2.png"));
imgView3.setImage(new Image("image/card/" + "3.png"));
imgView4.setImage(new Image("image/card/" + "4.png"));
imgView5.setImage(new Image("image/card/" + "5.png"));
//Cannot figure out what to do here ^
}
});
}
}
Upvotes: 1
Views: 107
Reputation: 778
When you call myCard()
you need to add the randomly generated number to a List
. When you click on refresh you need to clear that list (myList.clear()
). When you click on sort you need to first sort that list and then access the items in the list by index from 0
to myList.size()
.
If you add your images to a List
, too, you can use the same index to match image with number / imageResource.
Since you have a fixed number of cards you could also use an Array (but if you plan on reusing code for implementations with more cards I'd use a List)
Upvotes: 1