'Trying to pull an image from an ArrayList and assigning a value
I am creating a matching card game and am trying to assign a value to the cards themselves so that way when they are pulled into the eventhandler I can compare their value to another card that has also been flipped to see if they match and also trying to pull the image from the arrayList and display it when the card is clicked. I am fairly new to code so a dumbed down solution would be greatly appreciated
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.layout.GridPane;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import java.io.File;
import java.io.IOException;
import javafx.scene.image.ImageView;
import java.util.Random;
import java.util.ArrayList;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import java.util.*;
import javafx.geometry.Insets;
public class FinalProject extends Application
{
private Button button = new Button();
private Button button1 = new Button();
private Button button2 = new Button();
private Button button3 = new Button();
private Button button4 = new Button();
private Label label;
private int value;
private ImageView Clubs2IView;
private ImageView Diamonds2IView;
private ImageView Clubs3IView;
private ImageView Diamonds3IView;
private ArrayList<String> Cards;
private Image backCard;
private ImageView backCardIView;
private ImageView backCardIView1;
private ImageView backCardIView2;
private ImageView backCardIView3;
private int ran = rng();
private int ran1 = rng();
private int ran2 = rng();
private int ran3 = rng();
private int counter = 0;
private int counter1 = 0;
public static void main(String[] args)
{
launch(args);
}
public static int rng()
{
Random random = new Random();
int ran = random.nextInt(4);
return ran;
}
@Override
public void start(Stage primaryStage)
{
GridPane gridpane = new GridPane();
gridpane.setAlignment(Pos.CENTER);
gridpane.add(button, 0, 0);
gridpane.add(button1, 2, 0);
gridpane.add(button2, 0, 1);
gridpane.add(button3, 2, 1);
backCard = new Image("file:backCard.jpg");
backCardIView = new ImageView(backCard);
backCardIView1 = new ImageView(backCard);
backCardIView2 = new ImageView(backCard);
backCardIView3 = new ImageView(backCard);
backCardIView.setFitWidth(100);
backCardIView.setPreserveRatio(true);
backCardIView1.setFitWidth(100);
backCardIView1.setPreserveRatio(true);
backCardIView2.setFitWidth(100);
backCardIView2.setPreserveRatio(true);
backCardIView3.setFitWidth(100);
backCardIView3.setPreserveRatio(true);
button.setGraphic(backCardIView);
button1.setGraphic(backCardIView1);
button2.setGraphic(backCardIView2);
button3.setGraphic(backCardIView3);
Cards = new ArrayList<String>();
Cards.add("2_Clubs.jpg");
Cards.add("2_Diamonds.jpg");
Cards.add("3_Clubs.jpg");
Cards.add("3_Diamonds.jpg");
java.util.Collections.shuffle(Cards);
button.setOnAction(new EventHandler<ActionEvent>()
{
@Override
public void handle(ActionEvent event)
{
Cards.get(0);
Cards.remove(0);
counter++;
if(counter == 2)
{
}
}
});
button1.setOnAction(new EventHandler<ActionEvent>()
{
@Override
public void handle(ActionEvent event)
{
Cards.get(1);
Cards.remove(1);
}
});
button2.setOnAction(new EventHandler<ActionEvent>()
{
@Override
public void handle(ActionEvent event)
{
Cards.get(2);
Cards.remove(2);
}
});
button3.setOnAction(new EventHandler<ActionEvent>()
{
@Override
public void handle(ActionEvent event)
{
Cards.get(3);
Cards.remove(3);
}
});
Scene scene = new Scene(gridpane);
primaryStage.setScene(scene);
primaryStage.setTitle("Matching");
primaryStage.show();
}
}```
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
