我如何测试当前在特定 ImageView 中的图像并为每个可能的图像定制输出?

How can I test which image is currently in a specific ImageView and have a tailored output for each possible image?

我们已经设法获取它,因此当单击第一个按钮时,图像将从该列表中正确设置。但是,现在我需要将该信息发送到 handleUSPKeep 方法,并且我需要将设置到 ImageView 中的图像的列表 ID 设置到 handleUSPKeep 方法中 if 语句之前的行中。我创建了一个名为 i 的全局 int 变量,并将 ID 放在应该去的地方。有没有办法可以在程序结束时设置七种方法中的每一种我是什么?

代码:

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
    }   
}

如果您可以设置这些图像中的每一个发生的时间(即设置一个标记来标记有一个图像切换),并且没有大量的图像那么我会使用枚举作为解释 here。然后很棒的是,您可以使用 switch 语句来遍历每个场景,而无需使用多个 "if" 语句! :)

我建议您使用 List 来列出您的图片。您不能使用它的路径比较图像。看看这个演示,

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
    }   
}