2017-02-21 50 views
-1

Нам удалось получить его, чтобы изображения были правильно настроены из этого списка при нажатии первой кнопки. Тем не менее, теперь мне нужно, чтобы эта информация была отправлена ​​для обработки метода USPKeep, и мне нужно установить идентификатор списка для образа, который был установлен в ImageView, в строку, которая находится перед операциями if в методе handleUSPKeep. Я создал глобальную переменную int с именем i и I, где должен идти идентификатор. Есть ли способ, которым я мог бы установить, что я есть в каждом из семи методов в конце программы?Как проверить, какое изображение в данный момент находится в определенном ImageView, и иметь индивидуальный вывод для каждого возможного изображения?

КОД:

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

ответ

1

Я хочу предложить вам использовать 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 
    } 
} 
+0

Это именно то, что я искал! – Tresk33

+0

О, я рад, что смог помочь! удачи! –

+0

Кажется, я получаю исключение nullPointerException, когда пытаюсь запустить этот код, я добавил ошибку и весь мой класс, в которой я делаю это, код, который я добавил из вашего предложения, внизу, ошибка возникает в первом случае if , Есть идеи? – Tresk33

0

Если это возможно для вас, чтобы установить, когда каждый из этих образов происходит (то есть установить маркер, чтобы отметить, что есть переключатель изображения), и не попало тонны изображений то я буду использовать перечисление, как описано here. Тогда самое замечательное, что вы можете использовать инструкцию switch для прохождения каждого сценария, устраняя необходимость иметь несколько операторов «if»! :)

 Смежные вопросы

  • Нет связанных вопросов^_^