0

We've managed to get it so the images will be properly set from this list when the first button is clicked. However, now I need to have that information sent to handleUSPKeep method and I need to set the list ID for the image that was set into the ImageView into the line that is before the if statements in the handleUSPKeep method. I created a global int variable named i and I put where the ID is supposed to go. Is there a way that I could set what i is in each of the seven methods at the end of the program?

CODE:

public class USPSCaseSpinController implements Initializable {

        private static List<Image> gunImages =  new ArrayList<>();

        public static int i;

        @FXML
        private static Label USPName;  

        @FXML
        private static Label USPWearName;

        @FXML
        private Button SpinUSPS;

        @FXML
        public static ImageView setUSPImage;

        @FXML
        private Button USPKeep;

        @FXML
        private Button USPDiscard;


        @FXML
        private void handleSpinMechBack(MouseEvent event) throws IOException{
        Parent handleInventoryBackParent = FXMLLoader.load(getClass().getResource("/csgocaseopener/OpenCase.fxml"));
        Scene OPBackScene = new Scene(handleInventoryBackParent);
        Stage handleInventoryBackStage = (Stage) ((Node) event.getSource()).getScene().getWindow();
        handleInventoryBackStage.setScene(OPBackScene);
        handleInventoryBackStage.show();
        }


        @FXML
        private void SpinUSPSCase(ActionEvent event) throws IOException{
        gunImages.add(new Image("GunImages/USPLead Conduit.png"));
        gunImages.add(new Image("GunImages/USPNight Ops.png"));
        gunImages.add(new Image("GunImages/USPTorque.png"));
        gunImages.add(new Image("GunImages/USPGaurdian.png"));
        gunImages.add(new Image("GunImages/USPCyrex.png"));
        gunImages.add(new Image("GunImages/USPCaiman.png"));
        gunImages.add(new Image("GunImages/USPKill Confirmed.png"));
            Random rand = new Random();
            int gunSelect = rand.nextInt(99)+1;
            if(gunSelect<=30){
            LeadConduitUSPS();
            USPName.setText("Lead Conduit");
            }
            else if(gunSelect>=31 && gunSelect<=60){
            NightOpsUSPS();
            USPName.setText("Night Ops");
            }
            else if(gunSelect>=61 && gunSelect<=90){
            TorqueUSPS(); 
            USPName.setText("Torque");
            }
            else if(gunSelect>=91 && gunSelect<=93.5){
            GuardianUSPS();
            USPName.setText("Guardian");
            }
            else if(gunSelect>=94.5 && gunSelect<=97){
            CyrexUSPS();
            USPName.setText("Cyrex");
            }
            else if(gunSelect>=98 && gunSelect<=99){
            CaimanUSPS();
            USPName.setText("Caiman");
            }
            else if(gunSelect==100){
            KillConfirmedUSPS();
            USPName.setText("Kill Confirmed");
            }
            SpinUSPS.setDisable(true);
            USPKeep.setVisible(true);
            USPDiscard.setVisible(true);
        }

        @FXML
        private void handleUSPDiscard(ActionEvent event) throws IOException{
        SpinUSPS.setDisable(false);
        USPKeep.setVisible(false);
        USPDiscard.setVisible(false);
        USPName.setText("");
        USPWearName.setText("");
        setUSPImage.setImage(null);
    }

        @FXML
        public void handleUSPKeep(ActionEvent event) throws IOException{
        SpinUSPS.setDisable(false);
        USPKeep.setVisible(false);
        USPDiscard.setVisible(false);
        USPName.setText("");
        USPWearName.setText("");
        setUSPImage.setImage(null);


        ImageView setUSPImage = new ImageView(gunImages.get(i));

        if(setUSPImage.getImage().equals(gunImages.get(0))){
            System.out.println("Lead Conduit");
        }
        else if(setUSPImage.getImage().equals(gunImages.get(1))){
            System.out.println("Night Ops");
        }
        else if(setUSPImage.getImage().equals(gunImages.get(2))){
            System.out.println("Torque");
        }
        else if(setUSPImage.getImage().equals(gunImages.get(3))){
            System.out.println("Guardian");
        }
        else if(setUSPImage.getImage().equals(gunImages.get(4))){
            System.out.println("Cyrex");
        }
        else if(setUSPImage.getImage().equals(gunImages.get(5))){
            System.out.println("Caiman");
        }
        else if(setUSPImage.getImage().equals(gunImages.get(6))){
            System.out.println("Kill Confirmed");
        }

    }

        public static void LeadConduitUSPS(){
        setUSPImage.setImage(gunImages.get(0));
        i =0;
        Random rand = new Random();
        int wareSelect = rand.nextInt(99)+1;

        if(wareSelect<=10){ //Calculates the ware the gun has
            USPWearName.setText("Factory New");
        }
        else if(wareSelect>=11 && wareSelect<=25){
            USPWearName.setText("Minimal Wear");
        }
        else if(wareSelect>=26 && wareSelect<=60){
            USPWearName.setText("Field Tested");
        }
        else if(wareSelect>=61 && wareSelect<=80){
            USPWearName.setText("Well Worn");
        }
        else if(wareSelect>=81){
            USPWearName.setText("Battle Scarred");
        }
    }
    public static void NightOpsUSPS(){
        setUSPImage.setImage(gunImages.get(1));
        i=1;
        Random rand = new Random();
        double wareSelect = rand.nextInt(99)+1;

        if(wareSelect<=10){ //Calculates the ware the gun has
            USPWearName.setText("Factory New");
        }
        else if(wareSelect>=11 && wareSelect<=25){
            USPWearName.setText("Minimal Wear");
        }
        else if(wareSelect>=26 && wareSelect<=60){
            USPWearName.setText("Field Tested");
        }
        else if(wareSelect>=61 && wareSelect<=80){
            USPWearName.setText("Well Worn");
        }
        else if(wareSelect>=81){
            USPWearName.setText("Battle Scarred");
        }
    }
    public static void TorqueUSPS(){
        setUSPImage.setImage(gunImages.get(2));
        i=2;
        Random rand = new Random();
        double wareSelect = rand.nextInt(99)+1;

        if(wareSelect<=10){ //Calculates the ware the gun has
            USPWearName.setText("Factory New");
        }
        else if(wareSelect>=11 && wareSelect<=25){
            USPWearName.setText("Minimal Wear");
        }
        else if(wareSelect>=26 && wareSelect<=60){
            USPWearName.setText("Field Tested");
        }
        else if(wareSelect>=61 && wareSelect<=80){
            USPWearName.setText("Well Worn");
        }
        else if(wareSelect>=81){
            USPWearName.setText("Battle Scarred");
        }
    }
    public static void GuardianUSPS(){
        setUSPImage.setImage(gunImages.get(3));    
        i=3;
        Random rand = new Random();
        double wareSelect = rand.nextInt(99)+1;

        if(wareSelect<=10){ //Calculates the ware the gun has
            USPWearName.setText("Factory New");
        }
        else if(wareSelect>=11 && wareSelect<=50){
            USPWearName.setText("Minimal Wear");
        }
        else if(wareSelect>=51){
            USPWearName.setText("Field Tested");
        }
    }
    public static void CyrexUSPS(){
        setUSPImage.setImage(gunImages.get(4));  
        i=4;
        Random rand = new Random();
        double wareSelect = rand.nextInt(99)+1;

        if(wareSelect<=10){ //Calculates the ware the gun has
            USPWearName.setText("Factory New");
        }
        else if(wareSelect>=11 && wareSelect<=25){
            USPWearName.setText("Minimal Wear");
        }
        else if(wareSelect>=26 && wareSelect<=60){
            USPWearName.setText("Field Tested");
        }
        else if(wareSelect>=61 && wareSelect<=80){
            USPWearName.setText("Well Worn");
        }
        else if(wareSelect>=81){
            USPWearName.setText("Battle Scarred");
        }
    }
    public static void CaimanUSPS(){
        setUSPImage.setImage(gunImages.get(5));
        i=5;
        Random rand = new Random();
        double wareSelect = rand.nextInt(99)+1;

        if(wareSelect<=10){ //Calculates the ware the gun has
            USPWearName.setText("Factory New");
        }
        else if(wareSelect>=11 && wareSelect<=35){
            USPWearName.setText("Minimal Wear");
        }
        else if(wareSelect>=36 && wareSelect<=75){
            USPWearName.setText("Field Tested");
        }
        else if(wareSelect>=76){
            USPWearName.setText("Well Worn");
        }
    }
    public static void KillConfirmedUSPS(){
        setUSPImage.setImage(gunImages.get(6));
        i=6;
        Random rand = new Random();
        double wareSelect = rand.nextInt(99)+1;

        if(wareSelect<=10){ //Calculates the ware the gun has
            USPWearName.setText("Factory New");
        }
        else if(wareSelect>=11 && wareSelect<=24){
            USPWearName.setText("Minimal Wear");
        }
        else if(wareSelect>=36 && wareSelect<=60){
            USPWearName.setText("Field Tested");
        }
        else if(wareSelect>=61 && wareSelect<=85){
            USPWearName.setText("Well Worn");
        }
        else if(wareSelect>=86){
            USPWearName.setText("Battle Scarred");
        }
    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }   
}
Tresk33
  • 61
  • 3
  • 9

2 Answers2

1

I would like to suggest you to use a List to list your images. You can't compare image using it's path. Look this demo,

public class USPSCaseSpinController implements Initializable {

        @FXML
        private static Label USPName;

        @FXML
        private static Label USPWearName;

        @FXML
        private Button SpinUSPS;

        @FXML
        public static ImageView setUSPImage;

        @FXML
        private Button USPKeep;

        private List<Image> gunImages =  new ArrayList<>();

        @FXML
        private Button USPDiscard;

        @FXML
        private void handleSpinMechBack(MouseEvent event) throws IOException{
            Parent handleInventoryBackParent = FXMLLoader.load(getClass().getResource("/csgocaseopener/OpenCase.fxml"));
            Scene OPBackScene = new Scene(handleInventoryBackParent);
            Stage handleInventoryBackStage = (Stage) ((Node) event.getSource()).getScene().getWindow();
            handleInventoryBackStage.setScene(OPBackScene);
            handleInventoryBackStage.show();
        }


        @FXML
        private void SpinUSPSCase(ActionEvent event) throws IOException{
            Random rand = new Random();
            int gunSelect = rand.nextInt(99)+1;
            if(gunSelect<=30){
                LeadConduitUSPS();
            USPName.setText("Lead Conduit");
            }
            else if(gunSelect>=31 && gunSelect<=60){
                NightOpsUSPS();
                USPName.setText("Night Ops");
            }
            else if(gunSelect>=61 && gunSelect<=90){
                TorqueUSPS(); 
                USPName.setText("Torque");
            }
            else if(gunSelect>=91 && gunSelect<=93.5){
                GuardianUSPS();
                USPName.setText("Guardian");
            }
            else if(gunSelect>=94.5 && gunSelect<=97){
                CyrexUSPS();
                USPName.setText("Cyrex");
            }
            else if(gunSelect>=98 && gunSelect<=99){
                CaimanUSPS();
                USPName.setText("Caiman");
            }
            else if(gunSelect==100){
                KillConfirmedUSPS();
                USPName.setText("Kill Confirmed");
            }
            SpinUSPS.setDisable(true);
            USPKeep.setVisible(true);
            USPDiscard.setVisible(true);
        }

        @FXML
        private void handleUSPDiscard(ActionEvent event) throws IOException{
            SpinUSPS.setDisable(false);
            USPKeep.setVisible(false);
            USPDiscard.setVisible(false);
            USPName.setText("");
            USPWearName.setText("");
            setUSPImage.setImage(null);
        }

        @FXML
        public void handleUSPKeep(ActionEvent event) throws IOException{
           SpinUSPS.setDisable(false);
           USPKeep.setVisible(false);
           USPDiscard.setVisible(false);
           USPName.setText("");
           USPWearName.setText("");
           setUSPImage.setImage(null);

           gunImages.add(0, new Image("GunImages/USPLead Conduit.png"));
           gunImages.add(1, new Image("GunImages/USPNight Ops.png"));
           gunImages.add(2, new Image("GunImages/USPTorque.png"));
           gunImages.add(3, new Image("GunImages/USPGaurdian.png"));
           gunImages.add(4, new Image("GunImages/USPCyrex.png"));
           gunImages.add(5, new Image("GunImages/USPCaiman.png"));
           gunImages.add(6, new Image("GunImages/USPKill Confirmed.png"));

           ImageView setUSPImage = new ImageView(gunImages.get(0));

           if(setUSPImage.getImage().equals(gunImages.get(0))){
              System.out.println("Lead Conduit");
           }
           else if(setUSPImage.getImage().equals(gunImages.get(1))){
              System.out.println("Night Ops");
           }
           else if(setUSPImage.getImage().equals(gunImages.get(2))){
               System.out.println("Torque");
           }
           else if(setUSPImage.getImage().equals(gunImages.get(3))){
               System.out.println("Guardian");
           }
           else if(setUSPImage.getImage().equals(gunImages.get(4))){
               System.out.println("Cyrex");
           }
           else if(setUSPImage.getImage().equals(gunImages.get(5))){
               System.out.println("Caiman");
           }
           else if(setUSPImage.getImage().equals(gunImages.get(6))){
              System.out.println("Kill Confirmed");
           }
        }

        public static void LeadConduitUSPS(){
            int x =0;
            setUSPImage.setImage(gunImages.get(0));
            Random rand = new Random();
            int wareSelect = rand.nextInt(99)+1;

            if(wareSelect<=10){ //Calculates the ware the gun has
                USPWearName.setText("Factory New");
            }
            else if(wareSelect>=11 && wareSelect<=25){
                USPWearName.setText("Minimal Wear");
            }
            else if(wareSelect>=26 && wareSelect<=60){
                USPWearName.setText("Field Tested");
            }
            else if(wareSelect>=61 && wareSelect<=80){
                USPWearName.setText("Well Worn");
            }
            else if(wareSelect>=81){
                USPWearName.setText("Battle Scarred");
            }
    }
    public static void NightOpsUSPS(){
        int x = 1;
        setUSPImage.setImage(gunImages.get(1));

        Random rand = new Random();
        double wareSelect = rand.nextInt(99)+1;

        if(wareSelect<=10){ //Calculates the ware the gun has
            USPWearName.setText("Factory New");
        }
        else if(wareSelect>=11 && wareSelect<=25){
            USPWearName.setText("Minimal Wear");
        }
        else if(wareSelect>=26 && wareSelect<=60){
            USPWearName.setText("Field Tested");
        }
        else if(wareSelect>=61 && wareSelect<=80){
            USPWearName.setText("Well Worn");
        }
        else if(wareSelect>=81){
            USPWearName.setText("Battle Scarred");
        }
    }
    public static void TorqueUSPS(){
        int x = 2;
        setUSPImage.setImage(gunImages.get(2));

        Random rand = new Random();
        double wareSelect = rand.nextInt(99)+1;

        if(wareSelect<=10){ //Calculates the ware the gun has
            USPWearName.setText("Factory New");
        }
        else if(wareSelect>=11 && wareSelect<=25){
            USPWearName.setText("Minimal Wear");
        }
        else if(wareSelect>=26 && wareSelect<=60){
            USPWearName.setText("Field Tested");
        }
        else if(wareSelect>=61 && wareSelect<=80){
            USPWearName.setText("Well Worn");
        }
        else if(wareSelect>=81){
            USPWearName.setText("Battle Scarred");
        }
    }
    public static void GuardianUSPS(){
        int x = 3;
        setUSPImage.setImage(gunImages.get(3));

        Random rand = new Random();
        double wareSelect = rand.nextInt(99)+1;

        if(wareSelect<=10){ //Calculates the ware the gun has
            USPWearName.setText("Factory New");
        }
        else if(wareSelect>=11 && wareSelect<=50){
            USPWearName.setText("Minimal Wear");
        }
        else if(wareSelect>=51){
            USPWearName.setText("Field Tested");
        }
    }
    public static void CyrexUSPS(){
        int x = 4;
        setUSPImage.setImage(gunImages.get(4));

        Random rand = new Random();
        double wareSelect = rand.nextInt(99)+1;

        if(wareSelect<=10){ //Calculates the ware the gun has
            USPWearName.setText("Factory New");
        }
        else if(wareSelect>=11 && wareSelect<=25){
            USPWearName.setText("Minimal Wear");
        }
        else if(wareSelect>=26 && wareSelect<=60){
            USPWearName.setText("Field Tested");
        }
        else if(wareSelect>=61 && wareSelect<=80){
            USPWearName.setText("Well Worn");
        }
        else if(wareSelect>=81){
            USPWearName.setText("Battle Scarred");
        }
    }
    public static void CaimanUSPS(){
        int x = 5;
        setUSPImage.setImage(gunImages.get(5));

        Random rand = new Random();
        double wareSelect = rand.nextInt(99)+1;

        if(wareSelect<=10){ //Calculates the ware the gun has
            USPWearName.setText("Factory New");
        }
        else if(wareSelect>=11 && wareSelect<=35){
            USPWearName.setText("Minimal Wear");
        }
        else if(wareSelect>=36 && wareSelect<=75){
            USPWearName.setText("Field Tested");
        }
        else if(wareSelect>=76){
            USPWearName.setText("Well Worn");
        }
    }
    public static void KillConfirmedUSPS(){
        int x = 6;
        setUSPImage.setImage(gunImages.get(6));

        Random rand = new Random();
        double wareSelect = rand.nextInt(99)+1;

        if(wareSelect<=10){ //Calculates the ware the gun has
            USPWearName.setText("Factory New");
        }
        else if(wareSelect>=11 && wareSelect<=24){
            USPWearName.setText("Minimal Wear");
        }
        else if(wareSelect>=36 && wareSelect<=60){
            USPWearName.setText("Field Tested");
        }
        else if(wareSelect>=61 && wareSelect<=85){
            USPWearName.setText("Well Worn");
        }
        else if(wareSelect>=86){
            USPWearName.setText("Battle Scarred");
        }
    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }   
}
Shekhar Rai
  • 2,008
  • 2
  • 22
  • 25
  • I seem to be getting a nullPointerException when I try to run this code, I added the error and my entire class that im doing this in, the code I added from your suggestion is at the bottom, the error occurs at the first if statement. Any ideas? – Tresk33 Feb 21 '17 at 18:19
  • Okay so I found I was getting the error because I left out a line that was in your suggestion. Now it runs properly but it always gives me the first if statement but thats because that is what that line is telling it to do that. – Tresk33 Feb 21 '17 at 18:44
  • I was actually wondering how I could get the seven methods below this method to properly change the ImageView setUSPImage = new ImageView(gunImages.get(0)); line to the right number? what would be the best way to go about doing that? Thanks again! – Tresk33 Feb 22 '17 at 17:54
  • In your previous post,you're trying to select image randomly. Have you done that part? – Shekhar Rai Feb 23 '17 at 07:12
  • yes that was done successfully, when one of the seven methods is called, the image is set at the top of each method. – Tresk33 Feb 23 '17 at 14:07
  • Then what's your problem? i can't get it...please explain more or update your question... – Shekhar Rai Feb 26 '17 at 05:16
  • I'm sorry, i can't understand the state `I need each of these if statements to change the zero in the ImageView setUSPImage = new ImageView(gunImages.get(0)); line to a different number.`... – Shekhar Rai Feb 27 '17 at 07:15
  • Okay let me try to explain better, so in your explaination you solved this by using a list of images and an if statement, the line before the if statements chooses the first item in the list which is 0 which allows it to pass the first if statement. But now I need the if statements in the LeadConduitUSPS method and a few others to be able to set that zero to a different number so it can pass a different if statement as opposed to just the first one. – Tresk33 Feb 27 '17 at 17:28
  • I updated my answer, I made `List gunImages` as a Global variable to set different images within different methods. Hope it will solve your problem. – Shekhar Rai Feb 28 '17 at 05:48
  • This is so close, I seem to be getting an error when I run this however, I updated the code along with the error. I'm not sure why this is happening, do you have any ideas? thank you so much! – Tresk33 Feb 28 '17 at 16:09
  • I fixed that error by moving where we add the items to the list but I have one small issue that needs to be addressed. I will be updating the question to ask it, this is so close to working properly. Thanks again! – Tresk33 Feb 28 '17 at 20:06
  • Never mind I actually managed to solve that one, it finally works now! Thank you for all your help you have been fantastic. I apologize for spamming your inbox for the last week but thanks again, this has been a massive help. – Tresk33 Feb 28 '17 at 20:17
0

If it's possible for you to set when each of these images happen (i.e. set a marker to mark that there's an image switch), and there isn't a horrible ton of images then I'd use an enumeration as explained here. Then the great thing is that you can use a switch statement to go through each scenario, eliminating the need to have multiple "if" statements! :)

Community
  • 1
  • 1
rb612
  • 5,280
  • 3
  • 30
  • 68